#!/bin/bash

# User should create virutal env to install nplinker and dependencies
# do not use pip install --user nplinker
#

#------------------------------------------------------------------------------
# Creat installation path
#------------------------------------------------------------------------------

# Get python install prefix
if command -v python &> /dev/null; then
    PY_PATH=$(python  -c "import sys; print(sys.prefix)")
elif command -v python3 &> /dev/null; then
    PY_PATH=$(python3 -c "import sys; print(sys.prefix)")
else
    echo "❌ Error: python NOT FOUND"
    exit
fi

# check the path of this script
if [[ $(command -v install-nplinker-deps) != $PY_PATH/bin/install-nplinker-deps ]]; then
    echo "❌ Error: command `install-nplinker-deps` invoked from bad path;"
    echo "Please install nplinker with `pip install nplinker` and re-run the command."
    exit
fi

# create dependecy install path
cd $PY_PATH
mkdir nplinker_lib
LIB_PATH=$PY_PATH/nplinker_lib
cd $LIB_PATH
echo -e "✅ NPLinker dependecies and databases will be installed in: $LIB_PATH\n"


#------------------------------------------------------------------------------
# Check OS system and package manager
# Not support Windows
#------------------------------------------------------------------------------

if [[ "$OSTYPE" == "linux"* ]]; then
    # detect package manager for Linux
    if command -v apt &> /dev/null; then
        apt update &> /dev/null
        do_install="apt install -y"
    elif command -v dnf &> /dev/null; then
        do_install="dnf install -y"
    elif command -v yum &> /dev/null; then
        do_install="yum install -y"
    else
        echo "Package manager command 'apt' or 'dnf' or 'yum' NOT found."
        exit
    fi

elif [[ "$OSTYPE" == "darwin"* ]]; then
    # install package manager brew for MacOS if not exist
    ! command -v brew &> /dev/null && /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    do_install="brew install"

else
    echo "Error: NPLinker is not supported on $OSTYPE system, use linux or MacOS system please."
    exit 0
fi

install_it(){
    echo -e "🔥 Start installing $1 ..."
    $do_install $1
    echo -e "✅ $1 installed successfully\n"
}
#------------------------------------------------------------------------------
# Install base dependencies if not exist
#------------------------------------------------------------------------------
BASE_DEPS="
git
wget
gunzip
unzip
"

for cmd in $BASE_DEPS; do
    if ! command -v $cmd &> /dev/null; then
        install_it $cmd
    fi
done

# install gcc compiler (required by FastTree)
if command -v gcc &> /dev/null; then
    gcc="gcc"
elif ! command -v gcc-10 &> /dev/null; then
    brew install gcc@10
    gcc="gcc-10"
fi

#------------------------------------------------------------------------------
# Install NPLinker dependencies
#------------------------------------------------------------------------------

#--- Install BigScape
## Note: DO NOT pip install bigscape until its modular version
echo "🔥 Start installing BigScape ..."
    git clone https://github.com/medema-group/BiG-SCAPE.git
    pip install -r BiG-SCAPE/requirements.txt
    chmod 754 BiG-SCAPE/bigscape.py
    chmod 664 BiG-SCAPE/domains_color_file.tsv
    chmod 775 BiG-SCAPE/Annotated_MIBiG_reference
    ln -s $LIB_PATH/BiG-SCAPE/bigscape.py $PY_PATH/bin
echo -e "✅ BigScape installed successfully\n"

#--- Install FastTree (not support Windows, required by BigScape)
# http://www.microbesonline.org/fasttree/
echo "🔥 Start installing FastTree ..."
    if [[ "$OSTYPE" == "linux"* ]]; then
        $do_install fasttree
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        wget http://www.microbesonline.org/fasttree/FastTree.c
        $gcc -DNO_SSE -O3 -finline-functions -funroll-loops -Wall -o $PY_PATH/bin/fasttree FastTree.c -lm
        rm FastTree.c
    fi
echo -e "✅ FastTree installed successfully\n"

#--- Install Hmmer (not support Windows, required by BigScape)
install_it hmmer

#--- Prepare Pfam database (required by BigScape)
echo "🔥 Start installing Pfam database ..."
    wget ftp://ftp.ebi.ac.uk/pub/databases/Pfam/releases/Pfam34.0/Pfam-A.hmm.gz
    gunzip Pfam-A.hmm.gz
    hmmpress Pfam-A.hmm
echo -e "✅ Pfam database installed successfully\n"


#--- Install canopus_treemap
echo "🔥 Start installing Canopus"
    pip install canopus@git+https://github.com/kaibioinfo/canopus_treemap.git
echo -e "✅ Canopus installed successfully\n"


#--- Install SIRIUS
echo "🔥 Start installing Sirius"
    if [[ "$OSTYPE" == "linux"* ]]; then
        wget https://github.com/boecker-lab/sirius/releases/download/v5.5.5/sirius-5.5.5-linux64-headless.zip
        unzip sirius-5.5.5-linux64-headless.zip
        ln -s $LIB_PATH/sirius/bin/sirius $PY_PATH/bin
        rm sirius-5.5.5-linux64-headless.zip
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        wget https://github.com/boecker-lab/sirius/releases/download/v5.5.5/sirius-5.5.5-osx64-headless.zip
        unzip sirius-5.5.5-osx64-headless.zip
        ln -s $LIB_PATH/sirius.app/Contents/MacOS/sirius $PY_PATH/bin
        rm sirius-5.5.5-osx64-headless.zip
    fi
echo -e "✅ Sirius installed successfully\n"


echo -e "✅ 🎉 Successfully installed all NPLinker dependencies and databases!\n"
