#! /bin/sh

set -o pipefail
set -e
set -u

if tty && ! which firebase; then
   f="$(mktemp)"
   wget https://firebase.tools -O - > "$f"
   export upgrade=true
   sudo bash "$f"
fi
which firebase

outdir=$(mktemp -d)
cleanup() {
   rm -rf "$outdir"
}
trap cleanup EXIT

rm -rf $outdir
mkdir -p $outdir/public
cp ./open-resume.com.d/resume.html.2 $outdir/public/index.html
cp ./open-resume.com.d/resume.pdf $outdir/public/BreeLaPointeResume.pdf
cp -r ./open-resume.com.d/fonts $outdir/public/

cp -r ./firebase.json ./.firebaserc ./.firebase $outdir/
sed -i "s#deploy/public#public#" $outdir/firebase.json
cd $outdir

echo firebase uses squeaky2x3@gmail.com
! tty || firebase login
echo firebase init
args=()
log_args=()
if ! tty; then
   args+=("--token" "$1")
   log_args+=("--token" "${#1}")
fi
echo firebase deploy "${log_args[@]}"
firebase deploy "${args[@]}"
