#!/usr/bin/env bash
set -Eeuo pipefail
set -x

LOGS_PREFIX="/data/static/ci-logs"
LOGS_DIR="${DOLLAR}{LOGS_PREFIX}/$PROJECT"
mkdir -p "${DOLLAR}LOGS_DIR"
LOGFILE="${DOLLAR}{LOGS_DIR}/${DOLLAR}(date -Is)-${DOLLAR}(git rev-parse master).log"
exec &> >(tee -a "${DOLLAR}{LOGFILE}")

#
# Run tests and publish the generated site on the VPS server,
# where this hook is installed.
#

unset GIT_DIR
CLONE="${DOLLAR}(mktemp -d)"
git clone . "${DOLLAR}CLONE"
pushd "${DOLLAR}CLONE"

"${DOLLAR}{LOGS_PREFIX}"/ci-gen-index.sh

finish() {
  printf "\n\n>>> exit status was %s\n" "${DOLLAR}?"
  "${DOLLAR}{LOGS_PREFIX}"/ci-gen-index.sh
}
trap finish EXIT

nix-build -A test
nix-build -A site
"$(nix-build -A publishScript)/bin/publish.sh"
