#!/usr/bin/env bash

# bash strict mode (http://redsymbol.net/articles/unofficial-bash-strict-mode)
set -euo pipefail; IFS=$'\n\t'
trap 'echo "$(date "+[%H:%M:%S]") ERROR >> errexit on line $LINENO" >&2' ERR

ME="${0}"

# Conda functions are actually shell functions that don't get exported to
# subshells (by default), this is one way of re-sourcing those functions in the
# subshell, there may be other ways, but this seems to work for now, and should
# be portable? (https://github.com/conda/conda/issues/7980)
source "${CONDA_PREFIX%/*/*}/etc/profile.d/conda.sh"

# UTILS ------------------------------------------------------------------------

# info <message>; Print info message.
info() { printf "$(date "+[%H:%M:%S]") ${@}"; }

# abspath <directory>; Get the absolute path to a directory.
abspath() { [[ "${1}" = /* ]] && echo "${1}" || echo "$PWD/${1#./}"; }

# MAIN -------------------------------------------------------------------------

SRC_DIR="$(dirname ${ME})"
CONDA_SPEC=$(abspath "${SRC_DIR%/bin}/env/env-infernal.yml")

info "Creating conda environment\n"
CMD_CONDA="conda env create -f ${CONDA_SPEC}"
info "$ ${CMD_CONDA}\n"
eval "${CMD_CONDA}"

info "Testing Infernal\n"
CMD_CD="conda run --live-stream -n infernal cmsearch -h > /dev/null"
info "$ ${CMD_CD}\n"
eval "${CMD_CD}"

info "Infernal installed successfully\n"
