#!/usr/bin/env bash
TS="${TS:-2020-11-30T00-18-34}"
T="packaged-$TS-dropbear.tar"
D="${TGTDIR:-$TS-dropbear}"
R="./build-dropbear"
U="unpack-$TS-dropbear"
echo "Unpacking $T into ./$D $( [[ "x$1" == "x-c" ]] && echo "and removing archive plus unpacker upon success" )"
test -d "./$D" || mkdir -p "./$D" || { echo -e "ERROR: failed to create target directory ($D)"; exit 1; }
tar -C "./$D" -xf "./$T" || { echo -e "ERROR: failed to unpack tarball ($T)"; exit 1; }
( cd "./$D"; sha256sum -c SHA256SUMS || exit $?; ) || { echo -e "ERROR: failed to validate hashes in target directory ($D)"; exit 1; }
if [[ -x "./$D/$R" ]]; then
( set -x; cd "$D"; $R -r "$TS" ) || { echo -e "ERROR: failed to revive build package ($D)"; exit 1; }
fi
if [[ "x$1" == "x-c" ]]; then
echo "Also removing $U and $T"
rm -f "./$U" "./$T"
fi
