#!/bin/bash
# Download and statically build Phast, which is required for halPhyloP
# all binaries get copied into cactus/bin

# set this to one to make sure everything gets built statically (necessary for binary release)
STATIC_CHECK=$1

set -beEu -o pipefail
gitrel=85f7ed179dd097a86ba4added22d571785cc3e1d

binDir=$(pwd)/bin

# works on MacOS and Linux
numcpu=$(getconf _NPROCESSORS_ONLN)

# hal expects phast as a sister directory, so we stick it there
submodulesDir=$(pwd)/submodules
CWD=$(pwd)

set -x

mkdir -p ${binDir}

# build clapack
cd ${submodulesDir}
rm -rf clapack
wget -q http://www.netlib.org/clapack/clapack.tgz
tar -xvzf clapack.tgz
mv CLAPACK-3.2.1 clapack
cd clapack
cp make.inc.example make.inc && make -j ${numcpu} f2clib && make -j ${numcpu} blaslib && make -j ${numcpu} lib
export CLAPACKPATH=$(pwd)
cd ..

# build phast
cd ${submodulesDir}
rm -rf phast
git clone https://github.com/CshlSiepelLab/phast.git
cd phast
git checkout ${gitrel}
# hack in flags support
sed -i src/make-include.mk -e 's/CFLAGS =/CFLAGS +=/' -e 's/LIBS =/LIBS +=/' 
# note: phast's makefile doesn't support -j (somehow this only came up when upgrading to ubunut 22.04)
cd src && make

# copy over the binaries
for PHASTBIN in ../bin/*
do
	 if [[ $STATIC_CHECK -ne 1 || $(ldd ${PHASTBIN} | grep so | wc -l) -eq 0 ]]
	 then
		  cp ${PHASTBIN} ${binDir}
	 else
		  exit 1;
	 fi
done

cd ${CWD}

set +x
