#!/usr/bin/env bash

# standard invocation of sitegen.py

### <CONFIGURATION> ###
VENV_VERSION="15.1.0"
URL="https://pypi.python.org/packages/d4/0c/9840c08189e030873387a73b90ada981885010dd9aea134d6de30cd24cb8/virtualenv-$VENV_VERSION.tar.gz"
### </CONFIGURATION> ###

set -e

source "$(dirname "$0")/common"
cd "$AFP_ROOT"

USE_PYTHON_3=false

while getopts "t:r:p" OPT
do
    case "$OPT" in
      r)
        VERSION="$OPTARG"
        ;;
      t)
        ISABELLE_TOOL="$OPTARG"
        ;;
      p)
        USE_PYTHON_3=true
        ;;
    esac
done
shift $((OPTIND-1))

set_isabelle_tool

DEPENDENCIES_FILE="$(mktemp /tmp/afp.XXX)"

echo "Obtaining dependency information ..."
"$ISABELLE_TOOL" afp_dependencies > "$DEPENDENCIES_FILE" || fail "Could not obtain dependency information"

echo "Checking presence of Python ..."

case "$(uname -s)" in
  Darwin)
    echo "Running on macOS, using system python"
    PYTHON="/usr/bin/python"
    ;;
  *)
    if [ "$USE_PYTHON_3" = false ]; then
      echo "Running on Linux, trying to find Python 2.x"
      PYTHON="$(which python2 2> /dev/null)"
      if [ -z "$PYTHON" ]; then
        PYTHON="$(which python 2> /dev/null)"
      fi
    else
      echo "Running on Linux, trying to find Python 3.x"
      PYTHON="$(which python3 2> /dev/null)"
    fi
    ;;
esac

if [ ! -f "$PYTHON" ]; then
  fail "No suitable Python found"
else
  echo "Found Python at '$PYTHON'"
fi

echo "Checking presence of bootstrapping ..."

BOOTSTRAP_DIR="$AFP_ROOT/admin/py-bootstrap"
export PYTHONPATH="$BOOTSTRAP_DIR/lib/python"

if [ ! -d "$BOOTSTRAP_DIR" ]; then
  echo "Bootstrapping ..."
  mkdir -p "$BOOTSTRAP_DIR/bin"
  mkdir -p "$BOOTSTRAP_DIR/lib/python"
  (
    cd "$BOOTSTRAP_DIR"
    curl -sS "$URL" | tar xzf -
    cd "virtualenv-$VENV_VERSION"
    "$PYTHON" setup.py install --home "$BOOTSTRAP_DIR"
  )
fi

echo "Bootstrapped."

VENV_DIR="$AFP_ROOT/admin/venv"

if [ ! -d "$VENV_DIR" ]; then
  echo "Creating venv ..."
  "$PYTHON" "$BOOTSTRAP_DIR/bin/virtualenv" "$VENV_DIR"
fi

echo "Activating venv ..."
source "$VENV_DIR/bin/activate"

PYTHON="$VENV_DIR/bin/python"
PIP="$VENV_DIR/bin/pip"

echo "Checking Python version in venv ..."
PYTHON_VERSION="$("$PYTHON" --version 2>&1)"
case "${PYTHON_VERSION//Python /}" in
  2*)
     [ "$USE_PYTHON_3" = false ] || fail "Bootstrapped with $PYTHON_VERSION, but Python 3 requested"
     ;;
  3*)
     [ "$USE_PYTHON_3" = true ] || fail "Bootstrapped with $PYTHON_VERSION, but Python 2 requested"
     ;;
esac

echo "Installing dependencies ..."
"$PIP" install -q -r "$AFP_ROOT/admin/sitegen-req.txt"

echo "Running sitegen ..."
"$PYTHON" admin/sitegen-lib/sitegen.py --dest=web \
                                       --templates=admin/sitegen-lib/templates \
                                       --deps="$DEPENDENCIES_FILE" \
                                       metadata thys "$@"
