aboutsummaryrefslogtreecommitdiffstats
path: root/tar_fix.sh
blob: 7450d57d8482ef3be7b46fc4229dd9bc64e78010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash

set -e

usage() {
    echo "Uso: bash $0 --input=<archivo_entrada> --output=<archivo_salida>"
    exit 1
}

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' 'Get the current directory path...' '\e[m'
current_dir=$(pwd)

# Check arguments
while [[ $# -gt 0 ]]; do
    case "$1" in
        --input=*)
            input_file="${1#*=}"
            shift
            ;;
        --output=*)
            output_file="${1#*=}"
            shift
            ;;
        *)
            usage
            ;;
    esac
done

if [ -z "$input_file" ] || [ -z "$output_file" ]; then
    usage
fi

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' 'Unzip the input file to a temporary directory...' '\e[m'
temp_dir=$(mktemp -d)

tar -xzvf "$input_file" -C "$temp_dir" || echo "Info: some tar failed, but the script will continue."

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' 'Rename files ending in .pacnew...' '\e[m'
find "$temp_dir" -type f -name "*.pacnew" -exec sh -c 'mv -f "$1" "${1%.pacnew}"' _ {} \;

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' 'Copy the contents of 'x86_64' to the root...' '\e[m'
rsync -a "$temp_dir/x86_64/" "$temp_dir/"

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' 'Delete the x86_64 directory using doas (requires privileges)...'  '\e[m'
doas rm -rf "$temp_dir/x86_64"

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' "Make $output_file..." '\e[m'
(cd "$temp_dir" && tar -czvf "$current_dir/$output_file" *)

printf '%b%s%b%s%b\n' '\e[1;32m' '==> ' '\e[0m\033[1m' 'Clean the temporary directory using doas (requires privileges)...' '\e[m'
doas rm -rf "$temp_dir"

echo "The file $output_file has been successfully created! <3"