pthbs-download (1211B)
1 #!/bin/sh 2 if test -n "$pthbs_xtrace"; then 3 set -x 4 fi 5 test $# -gt 3 || exit 2 6 checksum_format=$1 7 checksum=$2 8 size=$3 9 shift 3 || exit 10 if test -z "$pthbs_cache"; then 11 printf '%s\n' >&2 "$0: fatal: pthbs_cache env var undefined or empty" 12 exit 100 13 fi 14 filename=$pthbs_cache/downloads/$checksum_format/$checksum 15 16 case $checksum_format in 17 (md5);; 18 (sha1);; 19 (sha256);; 20 (sha3);; 21 (sha512);; 22 (*) 23 printf>&2 "FATAL: unrecognized checksum format '%s'\n" "$checksum_format" 24 exit 1 25 ;; 26 esac 27 28 check() { 29 test -e "$filename" || return 1 30 ls -l "$filename" 31 find "$filename" -type f -size +"$size"c -delete 32 case $(find "$filename" -type f -size "$size"c -print) in 33 ($filename);; 34 (*) return 1;; 35 esac 36 if printf '%s %s\n' "$checksum" "$filename" | ${checksum_format}sum -c -; then 37 ln -sf "../../downloads/$checksum_format/$checksum" "$pthbs_cache/link/file-$checksum_format/" || exit 1 38 return 0 39 fi 40 rm -v "$filename" 41 exit 1 42 } 43 44 test -z "$pthbs_xtrace" || set -x 45 46 mkdir -p "$pthbs_cache/downloads/$checksum_format" || exit $? 47 48 if check; then 49 exit 0 50 fi 51 52 while test $# -gt 0; do 53 wget -cqO "$filename" -- "$1" 54 if check; then 55 exit 0 56 fi 57 shift 58 done 59 60 printf>&2 "FATAL: could not download file\n" 61 exit 1