#!/usr/bin/env bash
# it is assumed the script runs in the directory from the orthologer root
srcdir=$(dirname ${BASH_SOURCE[0]})
srcdir=$(cd $srcdir && pwd)
homedir=$(cd ~/ && pwd)

PKG=""
find_pkg() {
    local label=$1
    all=($(find "$srcdir" -maxdepth 1 -type d -name "${label}*" 2>/dev/null))
    if [[ ${#all[@]} -eq 0 ]]; then
        echo "error: no $label package found in $srcdir"
        return 255
    fi

    PKG=${all[0]}
    if [[ ${#all[@]} -gt 1 ]]; then
        PKG=${all[-1]}
        echo "WARNING: multiple $label packages found in $srcdir"
        echo "selecting ${PKG}"
    fi
}

# find BRHCLUS
find_pkg BRHCLUS || exit 255
brhclus=$PKG

# find ORTHOLOGER
find_pkg ORTHOLOGER || exit 255
orthologer=$PKG

if [[ ! -f "${brhclus}/bin/brhclus" ]]; then
    echo "-------------------------------------"
    echo "WARNING: BRHCLUS has not been compiled"
    echo "-------------------------------------"
    echo "In order to compile proceed as follows:"
    echo
    echo "cd $brhclus"
    echo "./create_cmake_build_env.sh"
    echo "cd build/release"
    echo "cmake ../"
    echo "make install"
    echo
fi

# set pipeline root
export DIR_ORTHOLOGER=$orthologer

# set exe path to be used in setup
# for further options see the file orthologer/bin/setup_project_defaults_prot.sh
export q_exepath="${brhclus}/bin:/usr/bin:/usr/local/bin:${homedir}/bin"
export q_path_brhclus="${brhclus}/bin"

# run setup script for an empty pipeline
$DIR_ORTHOLOGER/bin/setup_empty_prot.sh
