Ticket #55552: port-mirror

File port-mirror, 1.8 KB (added by ccorn, 7 years ago)

Shell script that should mostly work like "port mirror". Needs gpg.

Line 
1#! /bin/bash
2# Like "port mirror ...", but working where HTTPS URLs or redirects are involved
3
4gpg=gpg
5
6verify() {
7  $verified && return 0
8  [ -f "$file" ] || return 1
9  if [ ${#md[@]} = 0 ]; then
10    echo >&2 "$file: No checksums specified"
11  fi
12  for mdi in "${md[@]}"; do
13    mdalgo=${mdi%% *}
14    mdvalue=${mdi#* }
15    case $mdalgo in
16      (size)
17        mdactual=$(ls -l "$file" | awk '{print $5}');;
18      (*)
19        gpgmd=$mdalgo
20        case $gpgmd in (rmd160) gpgmd=ripemd160;; esac
21        mdactual=$($gpg --print-md "$gpgmd" <"$file" | tr -d \  | tr A-Z a-z) \
22        || return $?;;
23    esac
24    if [ "$mdactual" != "$mdvalue" ]; then
25      echo >&2 "$mdalgo($file) mismatch"
26      return 1
27    else
28      echo >&2 "$mdalgo($file) OK"
29    fi
30  done
31  verified=true
32  $verified
33}
34
35skip=false
36verified=false
37port distfiles "$@" | while read line; do
38  case $line in
39  ("")
40    ;;
41  ("--->  Distfiles for "*)
42    skip=false
43    pkg=${line#"--->  Distfiles for "}
44    file=
45    dest=
46    base=
47    dir=
48    md=()
49    verified=false
50    echo >&2 "Mirroring distfiles for package $pkg"
51    ;;
52  (\[*\]\ *)
53    skip=false
54    verified=false
55    file=${line%%\] *}
56    file=${file#\[}
57    dest=${line#*\] }
58    dir=${dest%/*}
59    base=${dest##*/}
60    md=()
61    if [ "$base" != "$file" ]; then
62      echo >&2 "distname $file != basename $base, skipping"
63      skip=true
64      continue
65    fi
66    if test ! -d "$dir"; then
67      (set -x; sudo install -d -o macports -g admin -m 755 "$dir") || { skip=true; continue; }
68    fi
69    echo >&2 + cd "$dir"
70    cd "$dir" || { skip=true; continue; }
71    ;;
72  (*:\ *)
73    $skip && continue
74    md[${#md[@]}]=${line/:/}
75    ;;
76  (http://*|https://*|ftp://*|ftps://*)
77    $skip && continue
78    verify && continue
79    url=$line
80    (set -x; sudo -u macports curl -L -O "$url")
81    verify
82    ;;
83  (*)
84    echo >&2 "Unknown output from port distfiles: $line"
85    ;;
86  esac
87done