File size: 2,919 Bytes
ea64a92
 
 
 
 
 
 
 
 
 
12b2337
ea64a92
12b2337
 
 
 
 
 
 
 
33e21bc
12b2337
33e21bc
12b2337
 
 
 
 
 
33e21bc
 
 
12b2337
ea64a92
 
 
 
8b71bd0
 
12b2337
ea64a92
12b2337
 
e389e40
12b2337
 
 
d970238
 
12b2337
d970238
12b2337
 
 
 
 
 
 
ea64a92
 
daf39e0
12b2337
ea64a92
 
12b2337
 
8b71bd0
 
 
 
 
12b2337
8b71bd0
12b2337
 
 
 
ea64a92
 
62c1d37
8b71bd0
ea64a92
 
 
 
12b2337
ea64a92
8b71bd0
ea64a92
33e21bc
ea64a92
8b71bd0
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash

# Check if a file argument is provided
if [[ -z "$1" ]]; then
    echo "Usage: $0 <input_file>"
    exit 1
fi

# Set the input file from the first argument
INPUT_FILE="$1"
CHECKSUM_FILE=".checksums"

# Function to generate SHA-256 checksum for a given file
generate_checksum() {
    local filepath="$1"
    sha256sum "$filepath" | awk '{print $1}'
}

# Function to retrieve the remote checksum if available
get_remote_checksum() {
    local url="$1"
    local checksum_url="${url}.sha256"  # Assuming the checksum URL is <file_url>.sha256

    # Attempt to fetch the remote checksum
    remote_checksum=$(curl -s "$checksum_url")
    if [[ -z "$remote_checksum" ]]; then
        echo "no_remote_checksum"
    else
        echo "$remote_checksum"
    fi
}

# Function to download the file and verify its integrity
download_file() {
    local filepath="$1"
    local url="$2"

    echo -e "\nChecking file: $filepath"

    # Check if file exists locally and get its checksum
    if [[ -f "$filepath" ]]; then
        local_checksum=$(generate_checksum "$filepath")
        stored_checksum=$(grep "$filepath" "$CHECKSUM_FILE" | awk '{print $1}')

        # Check if local file's checksum matches stored checksum
        if [[ "$local_checksum" == "$stored_checksum" ]]; then
            echo -e "[SKIP] $filepath already exists and matches the stored checksum."
            return 0
        fi
    fi

    # Check remote checksum if available
    remote_checksum=$(get_remote_checksum "$url")
    if [[ "$remote_checksum" != "no_remote_checksum" ]]; then
        echo "Remote checksum found: $remote_checksum"
        # Compare with local file's checksum if it exists
        if [[ -f "$filepath" && "$remote_checksum" == "$local_checksum" ]]; then
            echo -e "[SKIP] $filepath already matches the remote checksum."
            return 0
        fi
    else
        echo "[INFO] No remote checksum available for $url. Proceeding with download."
    fi

    # Download the file with wget, showing progress
    echo -e "[DOWNLOAD] Downloading $filepath..."
    wget -c --progress=dot -O "$filepath" "$url" 2>&1 | \
    grep --line-buffered "%" | \
    sed -u -e "s,\.,,g" | \
    awk '{printf("\r[DOWNLOADING] %s - %s", "'$filepath'", $2)}'

    # Confirm completion
    echo -e "\n[COMPLETE] Downloaded $filepath"

    # Generate and store checksum after download
    new_checksum=$(generate_checksum "$filepath")
    echo "$new_checksum $filepath" >> "$CHECKSUM_FILE"
}

# Read and process each line in the specified input file
while IFS= read -r line; do
    # Split each line into filepath and URL
    filepath=$(echo "$line" | awk '{print $1}')
    url=$(echo "$line" | awk '{print $2}')

    # Call the download function for each file
    download_file "$filepath" "$url" &
done < "$INPUT_FILE"

# Wait for all background download processes to finish
wait
echo -e "\nAll downloads are completed."