#!/usr/bin/env bash

set -o errexit
set -o nounset

if (( $# != 1 )); then
  echo "Usage: $0 REPO_NAME" >&2
  exit 1
fi

# REPO_NAME is the name of the repository to run tests for
REPO_NAME="$1"

NOW=$(date +'%Y-%m-%dT%H-%M-%S')
LOG_NAME="$REPO_NAME.$NOW.log"
LOG_PATH="$TMPDIR/$LOG_NAME"

touch $LOG_PATH

echo "Running tests"
echo "Follow logs: tail -f \$TMPDIR/$LOG_NAME"

if bash ~/repos/scripts/ci/post_receive_tests.sh "$REPO_NAME" > $LOG_PATH 2>&1; then
  print_success "Post-receive tests passed!"
else
  print_error "Post-receive tests failed!"
  cp "$LOG_PATH" ~/Desktop/$REPO_NAME.$NOW.failed.log
  print_error "See logs: cat ~/Desktop/$REPO_NAME.$NOW.failed.log"
  exit 1
fi
