| #!/bin/bash |
|
|
| |
| if [[ -z "$1" ]]; then |
| echo "Usage: $0 <input_file>" |
| exit 1 |
| fi |
|
|
| |
| INPUT_FILE="$1" |
| CHECKSUM_FILE=".checksums" |
|
|
| |
| generate_checksum() { |
| local filepath="$1" |
| sha256sum "$filepath" | awk '{print $1}' |
| } |
|
|
| |
| get_remote_checksum() { |
| local url="$1" |
| local checksum_url="${url}.sha256" |
|
|
| |
| remote_checksum=$(curl -s "$checksum_url") |
| if [[ -z "$remote_checksum" ]]; then |
| echo "no_remote_checksum" |
| else |
| echo "$remote_checksum" |
| fi |
| } |
|
|
| |
| download_file() { |
| local filepath="$1" |
| local url="$2" |
|
|
| echo -e "\nChecking file: $filepath" |
|
|
| |
| if [[ -f "$filepath" ]]; then |
| local_checksum=$(generate_checksum "$filepath") |
| stored_checksum=$(grep "$filepath" "$CHECKSUM_FILE" | awk '{print $1}') |
|
|
| |
| if [[ "$local_checksum" == "$stored_checksum" ]]; then |
| echo -e "[SKIP] $filepath already exists and matches the stored checksum." |
| return 0 |
| fi |
| fi |
|
|
| |
| remote_checksum=$(get_remote_checksum "$url") |
| if [[ "$remote_checksum" != "no_remote_checksum" ]]; then |
| echo "Remote checksum found: $remote_checksum" |
| |
| 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 |
|
|
| |
| 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)}' |
|
|
| |
| echo -e "\n[COMPLETE] Downloaded $filepath" |
|
|
| |
| new_checksum=$(generate_checksum "$filepath") |
| echo "$new_checksum $filepath" >> "$CHECKSUM_FILE" |
| } |
|
|
| |
| while IFS= read -r line; do |
| |
| filepath=$(echo "$line" | awk '{print $1}') |
| url=$(echo "$line" | awk '{print $2}') |
|
|
| |
| download_file "$filepath" "$url" & |
| done < "$INPUT_FILE" |
|
|
| |
| wait |
| echo -e "\nAll downloads are completed." |
|
|