#!/usr/bin/env bash
# Thu 22 Sep 2016 13:05:54 CEST

# Build utilities

# Determines the architecture we're using
arch() {
  [[ "$(uname -s)" == "Darwin" ]] && echo "macosx" || echo "linux"
}


# Functions for coloring echo commands
log_info() {
  echo -e "(`date +%T`) \033[1;34m${@}\033[0m"
}


log_warn() {
  echo -e "(`date +%T`) \033[1;35mWarning: ${@}\033[0m"
}


log_error() {
  echo -e "(`date +%T`) \033[1;31mError: ${@}\033[0m"
}


# Checks a given environment variable is set (non-zero size)
check_env() {
  if [ -z "${!1}" ]; then
    log_error "Variable ${1} is undefined - aborting...";
    exit 1
  else
    log_info "\${${1}}=${!1}";
  fi
}


# Checks a given environment variable is set (non-zero size)
check_pass() {
  if [ -z "${!1}" ]; then
    log_error "Variable ${1} is undefined - aborting...";
    exit 1
  else
    log_info "\${${1}}=********";
  fi
}


# Function for running command and echoing results
run_cmd() {
  log_info "$ ${@}"
  ${@}
  local status=$?
  if [ ${status} != 0 ]; then
    log_error "Command Failed \"${@}\""
    exit ${status}
  fi
}


# Function for running command and echoing results, with LD_LIBRARY_PATH set
run_cmd_ld() {
  log_info "$ ${@}"
  LD_LIBRARY_PATH=${BOB_PREFIX_PATH}/lib ${@}
  local status=$?
  if [ ${status} != 0 ]; then
    log_error "Command Failed \"${@}\""
    exit ${status}
  fi
}


# Runs setup.py
setup() {
  run_cmd_ld ${PREFIX}/bin/python setup.py ${@}
}


# Prepares ~/.pypirc
dot_pypirc() {
  local rc=${HOME}/.pypirc
  log_info "Creating ${rc}..."
  if [ -e ${rc} ]; then
    run_cmd rm -f ${rc}
  fi
  cat <<EOT >> ${rc}
[distutils]
index-servers =
    production
    staging

[production]
repository: ${PYPISERVER}
username: ${PYPIUSER}
password: ${PYPIPASS}

[staging]
repository: ${TESTSERVER}
username: ${PYPIUSER}
password: ${PYPIPASS}
EOT
  run_cmd chmod 600 ${rc}
}


# Fixes the python prompt by setting LD_LIBRARY_PATH permanently
fix_venv() {

  local py=${PREFIX}/bin/python

  case ${PYVER} in
    py3*) py=${py}3 ;;
  esac

  log_info "Hot-fixing virtualenv provided by ${py}..."

  mv ${py} ${py}.orig
  cat <<EOT >> ${py}
#!/usr/bin/env bash
LD_LIBRARY_PATH=${BOB_PREFIX_PATH}/lib exec ${py}.orig "\${@}"
EOT
  run_cmd chmod 755 ${py}
}


# Uploads a file to our intranet location via curl
doc_upload() {
  log_info "curl: ${1} -> ${DOCSERVER}/${2}..."
  curl --location --silent --show-error --user "${DOCUSER}:${DOCPASS}" --upload-file ${1} ${DOCSERVER}/${2}
  local status=$?
  if [ ${status} != 0 ]; then
    log_error "Curl command finished with an error condition (status=${status})"
    exit ${status}
  fi
  log_info "Successfully uploaded ${1} with curl"
}

check_env PYTHON_VERSION
check_env CI_PROJECT_DIR
check_env CI_PROJECT_PATH
check_env CI_PROJECT_NAME
check_env CI_BUILD_REF
check_env CI_BUILD_REF_NAME
check_pass PYPIUSER
check_pass PYPIPASS
check_pass DOCUSER
check_pass DOCPASS

# Sets up variables
ARCH=`arch`

if [ -z "${CONDA_FOLDER}" ]; then
  if [ "${ARCH}" == "linux" ]; then
    CONDA_FOLDER=/local/conda
  else
    CONDA_FOLDER=/opt/conda
  fi
fi

if [ -z "${CONDA_PREFIX}" ]; then
  CONDA_PREFIX=env
fi

if [ -z "${DOCSERVER}" ]; then
  DOCSERVER=https://www.idiap.ch
fi

PREFIX=${CI_PROJECT_DIR}/${CONDA_PREFIX}

# Add "-coverage" support
if [ "${ARCH}" == "linux" ]; then
  # Temporary hack to get building done right with gcc-5 compilers
  if [ -z "${CFLAGS}" ]; then
    CFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0 -coverage"
  else
    CFLAGS="${CFLAGS} -D_GLIBCXX_USE_CXX11_ABI=0 -coverage"
  fi
  CXXFLAGS=${CFLAGS}
else
  if [ -z "${CFLAGS}" ]; then
    CFLAGS="-coverage"
  else
    CFLAGS="${CFLAGS} -coverage"
  fi
  CXXFLAGS=${CFLAGS}
fi

PYVER=py$(echo ${PYTHON_VERSION} | tr -d '.')
BOB_DOCUMENTATION_SERVER="${DOCSERVER}/software/bob/docs/latest/bob/%s/master/"
BOB_PREFIX_PATH=${CONDA_FOLDER}/envs/bob-devel-${PYVER}

if [ -z "${PYPISERVER}" ]; then
  PYPISERVER="https://pypi.python.org/pypi"
fi

TESTSERVER=https://testpypi.python.org/pypi

check_env ARCH
check_env PYVER
check_env PREFIX
check_env CFLAGS
check_env CXXFLAGS
check_env DOCSERVER
check_env PYPISERVER
check_env TESTSERVER
check_env CONDA_FOLDER
check_env CONDA_PREFIX
check_env BOB_PREFIX_PATH
check_env BOB_DOCUMENTATION_SERVER
