#!/usr/bin/sh

# Common functions
# LHASA_MAIN_DIR should already be set by this point

setcolor() {
    if [ -t 1 ]; then
        case $1 in
            red) echo -n "$(tput setaf 1)" ;;
            green) echo -n "$(tput setaf 2)" ;;
            yellow) echo -n "$(tput setaf 3)" ;;
            blue) echo -n "$(tput setaf 4)" ;;
            magenta) echo -n "$(tput setaf 5)" ;;
            cyan) echo -n "$(tput setaf 6)" ;;
            white) echo -n "$(tput setaf 7)" ;;
            reset) echo -n "$(tput sgr0)" ;;
        esac
    fi
}

fail() {
    setcolor red
    echo "Error: $1"
    setcolor reset
    exit 1
}

# Download dir for the dependencies.
: "${DEPENDENCY_DIR:=${LHASA_MAIN_DIR}/checkout}"

# Install dir for the dependencies.
: "${INSTALL_DIR:=${LHASA_MAIN_DIR}/prefix}"

# Build dir for the dependencies.
: "${DEPENDENCY_BUILD_DIR:=${LHASA_MAIN_DIR}/build}"

# Cmake build dir for Lhasa itself
: "${LHASA_CMAKE_BUILD_DIR:=${LHASA_MAIN_DIR}/lhbuild}"

if [ -z "$NUMPROCS" ]; then
  if [ x`uname -s` = x"Darwin" ]; then
      NUMPROCS=`sysctl -n hw.ncpu`
  else
      NUMPROCS=`nproc --all`
  fi
fi
  
cdcheckout() {
    mkdir -p "$DEPENDENCY_DIR" &&\
    cd "$DEPENDENCY_DIR" || fail "Failed to enter/setup the directory withd dependencies."
}

if [ -e "$LHASA_MAIN_DIR/VERSIONS" ]; then
    . "$LHASA_MAIN_DIR/VERSIONS"
else
    fail "Cannot find VERSIONS file. Exiting."
fi

if [ -e "$LHASA_MAIN_DIR/EMSCRIPTEN_CONFIG" ]; then
    . "$LHASA_MAIN_DIR/EMSCRIPTEN_CONFIG"
else
    fail "Cannot find EMSCRIPTEN_CONFIG file. Exiting."
fi


getrdkit() {
    cdcheckout
    if [ -r rdkit-$rdkit_release ]; then
        echo "Using existing rdkit"
    else
        echo "Downloading RDKit source"
        curl -fL https://github.com/rdkit/rdkit/archive/refs/tags/$rdkit_release.tar.gz -o "$rdkit_release.tar.gz" || fail "Failed to download RDKit source"
        echo
        echo "Unpacking RDKit source"
        tar xf "$rdkit_release.tar.gz" || fail "Failed to unpack RDKit source"
        echo
        echo "Getting git version of rapidjson for RDKit"
        cd rdkit-$rdkit_release/External &&\
        git clone https://github.com/Tencent/rapidjson.git rapidjson-1.1.0 || fail "Failed to clone rapidjson"
    fi
    echo
}

getgraphene() {
    cdcheckout
    if [ -r graphene-$graphene_release ]; then
        echo "Using existing graphene"
    else
        echo "Downloading graphene source"
        curl -fL https://github.com/ebassi/graphene/archive/refs/tags/$graphene_release.tar.gz -o graphene_$graphene_release.tar.gz || fail "Failed to download graphene source"
        echo
        echo "Unpacking graphene source"
        tar xf "graphene_$graphene_release.tar.gz" || fail "Failed to unpack graphene source"
    fi
    echo
}


getsigcpp() {
    cdcheckout
    if [ -r libsigcplusplus-$libsigcpp_release ]; then
        echo "Using existing libsigcplusplus"
    else
        echo "Downloading libsigc++ source"
        curl -fL https://github.com/libsigcplusplus/libsigcplusplus/archive/refs/tags/$libsigcpp_release.tar.gz -o libsigcplusplus_$libsigcpp_release.tar.gz || fail "Failed to download libsigc++ source"
        echo
        echo "Unpacking libsigc++ source"
        tar xf "libsigcplusplus_$libsigcpp_release.tar.gz" || fail "Failed to unpack libsigc++ source"
    fi
    echo
}

getboost() {
    cdcheckout
    if [ -r boost-$boost_release ]; then
        echo "Using existing boost"
    else
        echo "Checking out boost"
        curl -fL https://github.com/boostorg/boost/releases/download/boost-$boost_release/boost-$boost_release-cmake.tar.gz -o boost_$boost_release.tar.gz || fail "Failed to download boost source"
        tar xf boost_$boost_release.tar.gz || fail "Failed to unpack boost source"
    fi
    echo
}

getgemmi() {
    cdcheckout
    if [ -r gemmi-$gemmi_release ]; then
        echo "Using existing gemmi"
    else
        echo "Downloading gemmi source"
        curl -fL https://github.com/project-gemmi/gemmi/archive/refs/tags/v$gemmi_release.tar.gz -o gemmi_$gemmi_release.tar.gz || fail "Failed to download gemmi source"
        echo
        echo "Unpacking gemmi source"
        tar xf "gemmi_$gemmi_release.tar.gz" || fail "Failed to unpack gemmi source"
        echo
        echo "Patching gemmi..."
        cd gemmi-$gemmi_release &&\
        patch -p1 < "$LHASA_MAIN_DIR/patches/gemmi-emscripten-64.patch" || fail "Failed to patch gemmi"
    fi
    echo
}

getmaeparser() {
    cdcheckout
    if [ -r maeparser-$maeparser_release ]; then
        echo "Using existing maeparser"
    else
        echo "Downloading maeparser source"
        curl -fL https://github.com/schrodinger/maeparser/archive/refs/tags/v$maeparser_release.tar.gz -o maeparser_$maeparser_release.tar.gz || fail "Failed to download maeparser source"
        echo
        echo "Unpacking maeparser source"
        tar xf "maeparser_$maeparser_release.tar.gz" || fail "Failed to unpack maeparser source"
    fi
    echo
}

getcoordgen() {
    cdcheckout
    if [ -r coordgenlibs-$coordgen_release ]; then
        echo "Using existing coordgen"
    else
        echo "Downloading coordgen source"
        curl -fL https://github.com/schrodinger/coordgenlibs/archive/refs/tags/v$coordgen_release.tar.gz -o coordgen_$coordgen_release.tar.gz || fail "Failed to download coordgen source"
        echo
        echo "Unpacking coordgen source"
        tar xf "coordgen_$coordgen_release.tar.gz" || fail "Failed to unpack coordgen source"
    fi
    echo
}

geteigen() {
    cdcheckout
    if [ -r eigen-$eigen_release ]; then
        echo "Using existing eigen"
    else
        echo "Downloading Eigen source"
        curl -fL https://gitlab.com/libeigen/eigen/-/archive/$eigen_release/eigen-$eigen_release.tar.gz -o eigen_$eigen_release.tar.gz || fail "Failed to download Eigen source"
        echo
        echo "Unpacking Eigen source"
        tar xf "eigen_$eigen_release.tar.gz" || fail "Failed to unpack Eigen source"
    fi
    echo
}
