#!/bin/bash

# enable strict mode
set -euo pipefail
IFS=$'\n\t'

# defaults if not set
: "${BUILD_TYPE:=build-pr}"

# convert to all lowercase
BUILD_TYPE=$(echo $BUILD_TYPE | tr '[:upper:]' '[:lower:]')

ant -DtestFilter=${TEST_FILTER} \
    -DskipLint=${SKIP_LINT} \
    -DskipCoverage=${SKIP_COVERAGE} \
    -buildfile $APP_HOME/build.xml $BUILD_TYPE

# attempt to fix permissions on the git directory
if [ "$BUILD_TYPE" == "build-static-files" ]; then
  # check git status for debugging purposes
  git status

  if [[ -d ".git" ]];
  then

    if [ -n "$JENKINS_UID" ] && [ -n "$JENKINS_GID" ]; then
      chown -R ${JENKINS_UID}:${JENKINS_GID} .git
    fi
  fi
fi

echo 'Done!'
