#!/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 [ x`uname -s` = x"Darwin" ]; then
    NUMPROCS=`sysctl -n hw.ncpu`
else
    NUMPROCS=`nproc --all`
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
}