#!/usr/bin/env bash empty() { [[ $(stat -c %s "$1") -eq 0 ]] } url2murl() { local x x=$1 x=${x//'^'/'^5E'} x=${x//':'/'^3A'} x=${x//'%'/'^25'} printf '%s' "$x" } murl2url() { local x x=$1 x=${x//'^25'/'%'} x=${x//'^3A'/':'} x=${x//'^5E'/'^'} printf '%s' "$x" } main() { set -euE -o pipefail shopt -s nullglob shopt -s globstar top=$PWD mkdir -p "$1" cd "$1" git init echo 'ref: refs/heads/PROGRAMS/CVTUTF' > .git/HEAD lastbranch=PROGRAMS/CVTUTF git commit --allow-empty -m 'initial commit' for snapshot in "$top"/dat/pools/snaps/*; do snapname="${snapshot##*/}" time="${snapname%%-*}" dirpart="${snapname#*-}" dirpart="${dirpart//_/\/}" branch=$dirpart newbranch=true git checkout "$lastbranch" git checkout -b "$branch" || newbranch=false git checkout "$branch" rm -f -- * .metadata.txt cp -- "$snapshot"/* . if [[ "$time" != *99 ]]; then listingdir=("$top/dat/content-dir/$time/"**"/Public/$dirpart") if [[ ${#listingdir[@]} != 1 ]]; then >&2 printf 'Cannot find wayback listing for %s' "$snapname" false fi if ! [[ -f readme.txt ]] && ! empty "$listingdir/readme.txt"; then cp "$listingdir/readme.txt" . fi cp "$listingdir/metadata.txt" .metadata.txt fi if $newbranch || [[ -n "$(git status -s .)" ]]; then if [[ "$time" = *99 ]]; then msg="Synthesized listing: ${time} ${dirpart}" time="${time%99}00" else waurl="http://web.archive.org/web/$(murl2url "${listingdir##*/content-dir/}/")" msg="$waurl" fi gitdate="$(sed -r 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3T\4:\5:\6/' <<<"$time")" git add . export GIT_AUTHOR_DATE=$gitdate export GIT_COMMITTER_DATE=$gitdate git commit --allow-empty -m "$msg" if [[ "$branch" != *.OLD ]]; then lastbranch="$branch" fi if [[ "$branch" == PROGRAMS/CVTUTF ]] && git log -n1 --stat|grep -qF 'ExpectedOutput.txt'; then git filter-branch -f --parent-filter 'cat; echo " -p BETA/CVTUTF-1-3"' HEAD^..HEAD git update-ref -d refs/original/refs/heads/"$branch" fi if [[ "$branch" == PROGRAMS/CVTUTF.OLD ]] && git log -n1 --stat|grep -qi '.*\.c\s'; then git filter-branch -f --parent-filter 'cat; echo " -p PROGRAMS/CVTUTF^"' HEAD^..HEAD git update-ref -d refs/original/refs/heads/"$branch" fi fi done } main "$@"