#!/usr/bin/env bash url2murl() { local x x=$1 x=${x//'^'/'^5E'} x=${x//':'/'^3A'} x=${x//'%'/'^25'} printf '%s' "$x" } main() { set -euE -o pipefail shopt -s nullglob echo '# Pass 1' while read -r snap name date time size; do dirpart="${name%/*}" filepart="${name##*/}" filedir=dat/pools/files/"${date//-/}${time//:/}-${name//\//_}" snapdir=dat/pools/snaps/"${snap}-${dirpart//\//_}" mkdir -p -- "$filedir" "$snapdir" ln -sr "$filedir/$filepart" "$snapdir" done < "$1" echo '# Pass 1.5' # Looking at the data, there are 3 revisions that we DON'T # have directory listings for. So we need to synthesize # those. # # I created the list of listings to synthesize by not # synthesizing anything, then looking for files ending in # ".1". They are created during pass 2 if we have a file with # no matching listing. while read -r datetime dirpart newfiles; do # We need to figure out which files to put in the # directory listing. We're going to do that by # mimicking the previous listing with that dirpart. prevsnap='' for isnap in dat/pools/snaps/*-"${dirpart//\//_}"; do isnap=${isnap##*/} if [[ "${isnap%%-*}" -lt "${datetime}00" ]]; then prevsnap=$isnap fi done if [[ -z "$prevsnap" ]]; then >& printf 'Could not find listing of %s before %s\n' "$dirpart" "$datetime" false fi # Ok, now copy that snapshot snapdir=dat/pools/snaps/"${datetime}00-${dirpart//\//_}" cp -aT dat/pools/snaps/"$prevsnap" "$snapdir" # And touch file files we need to change for filepart in $newfiles; do name="$dirpart/$filepart" filedir=dat/pools/files/"${datetime}-${name//\//_}" mkdir -p -- "$filedir" rm -- "$snapdir/$filepart" ln -sr "$filedir/$filepart" "$snapdir" done done < <(printf '%s\n' \ '200303310700 PROGRAMS/CVTUTF ConvertUTF.c ConvertUTF.h harness.c' \ '200307291500 ALPHA/CVTUTF-1-1 ExpectedOutput.txt readme.txt' \ '200412210100 PROGRAMS/CVTUTF.OLD CVTUTF7.C CVTUTF7.H' \ ) echo '# Pass 2' while read -r time url; do name="${url##*/Public/}" dirpart="${name%/*}" filepart="${name##*/}" if [[ -z "$filepart" ]]; then continue fi pools=(dat/pools/files/*-"${name//\//_}") pools=("${pools[@]##*/}") mypool='' for pool in "${pools[@]}"; do pooltime="${pool%%-*}" if [[ "${pooltime}00" -le "$time" ]]; then mypool=$pool fi done if [[ -z "$mypool" ]]; then >&2 printf 'Could not find pool for %s %s\n' "$time" "$url" false fi file="dat/content-file/$time/$(url2murl $url)" declare -i i=0 while true; do link="dat/pools/files/$mypool/$filepart.$i" link="${link%.0}" a="$(readlink -f "$link")" || true b="$(readlink -f "$file")" if cmp -s -- "$a" "$b"; then break fi if ln -sr "$b" "$link"; then break fi i+=1 done done < "$2" echo '# Pass 3' while read -r missing; do if [[ -f "${missing/.OLD}/${missing##*_}" ]]; then ln -sr "${missing/.OLD}/${missing##*_}" "$missing" fi done < <(find dat/pools/files/*-PROGRAMS_CVTUTF.OLD_* -type d -empty) } main "$@"