#!/bin/bash
# Generate release tar file static-compiled binaries 
# Must be run after tree is tagged and pushed to master.
# Use --keep to keep working directory for debugging.

mydir=$(dirname $(which $0))
source ${mydir}/releaseLib.sh

keep=no
if [ $1 -eq '--keep' ] ; then
    keep=yes
fi
set -beEu -o pipefail

buildDir=$(realpath -m build)
binBuildDir="${buildDir}/bin-tmp"

set -x
rm -rf ${binBuildDir}
mkdir -p ${binBuildDir}
cd ${binBuildDir}
git clone https://github.com/ComparativeGenomicsToolkit/cactus.git
cd cactus
git fetch --tags origin

REL_TAG=$(getLatestReleaseTag)
git checkout "${REL_TAG}"
git submodule update --init --recursive

if [ -z ${CACTUS_LEGACY_ARCH+x} ]
then
# Make sure abpoa doesn't build with -march=native, but something more portable
# Using avx2 in normal release and sse41 (which can be much slower on big problems) for legacy release	 
	 export avx2=1
else
	 export sse41=1
fi

# hack lastz, FASTGA, FASTAN and alntools to build static.
# todo: it'd be nice to have this done in cactus's "make static"
sed -i submodules/lastz/src/Makefile -e 's/CFLAGS = /CFLAGS = -static /g'
sed -i submodules/lastz/src/Makefile -e 's/-lm/-lm -static/g'
sed -i submodules/FASTGA/Makefile -e 's/CFLAGS = /CFLAGS = -static /g'
sed -i submodules/FASTAN/Makefile -e 's/CFLAGS = /CFLAGS = -static /g'
sed -i submodules/alntools/Makefile -e 's/CFLAGS = /CFLAGS = -static /g'

export CFLAGS="-static"
export CXXFLAGS="-static"
export LDFLAGS="-static"
export LIBS="-static"

# throw in gnu parallel since cactus-hal2maf depends on it
mkdir -p bin
wget -q http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel -O bin/parallel
chmod +x bin/parallel

# build a couple dependencies from source because versions from awk don't support static linking
pushd .
cd ${binBuildDir}
mkdir -p deps ; cd deps
DEP_PREFIX=$(pwd)
# hdf5
wget -q https://github.com/HDFGroup/hdf5/archive/refs/tags/hdf5-1_10_9.tar.gz
tar -zxf hdf5-1_10_9.tar.gz
cd  hdf5-hdf5-1_10_9
./configure --enable-cxx --enable-static --disable-parallel --disable-shared --prefix=${DEP_PREFIX}
make -j $(nproc)
make install
export PATH=${DEP_PREFIX}/bin:$PATH
# libxml2
cd ${DEP_PREFIX}
wget -q http://xmlsoft.org/sources/libxml2-2.9.12.tar.gz
tar -zxf libxml2-2.9.12.tar.gz
cd libxml2-2.9.12
./configure --enable-static --disable-shared --prefix=${DEP_PREFIX} --with-python=no
make -j $(nproc)
make install
export CACTUS_STATIC_LINK_FLAGS="-L${DEP_PREFIX}/lib -lz -lpthread -lm -llzma -llz4 -lzstd"
export CACTUS_LIBXML2_INCLUDE_PATH=${DEP_PREFIX}/include/libxml2
popd

# install Phast and enable halPhyloP compilation and copy the phast binaries
build-tools/downloadPhast 1
cp submodules/phast/LICENSE ./PHAST.LICENSE.txt
export ENABLE_PHYLOP=1

# install UCSC browser libraries to compile UDC
# remote access
# Disabled until we figure out static build
# ./build-tools/downloadUcscLib
# export ENABLE_UDC=1
# export KENTSRC=$(pwd)/submodules/kent/src

make -j $(nproc) check-static

# download all external tools used for pangenome pipeline
build-tools/downloadPangenomeTools 1

# download tools for working with MAF
build-tools/downloadMafTools 1

binPackageDir=cactus-bin-${REL_TAG}
rm -rf ${binPackageDir}
mkdir ${binPackageDir}
cp -r bin src examples ${binPackageDir}
cp *.md *.py *.txt ${binPackageDir}
# sonLib needed for pip install
find submodules/sonLib -name '*.py' | cpio -pdum ${binPackageDir}
# install the hal python libraries, requires PYTHONPATH set by user.
# todo: probably a better way
mkdir -p $(binPackageDir)/lib
rsync -avm --include='*.py' -f 'hide,! */' ./submodules/hal ${binPackageDir}/lib
# need .git dir for pip install -U ., but don't need everything
cp -r .git ${binPackageDir}
rm -rf ${binPackageDir}/.git/modules
# remove test executables
rm -f ${binPackageDir}/bin/*test ${binPackageDir}/bin/*tests ${binPackageDir}/bin/*Test ${binPackageDir}/bin/*Tests
# we now mention running downloadVCFWave in the BIN-INSTALL.md, so it should be included too
mkdir ${binPackageDir}/build-tools
cp build-tools/downloadVCFWave  ${binPackageDir}/build-tools
# make binaries smaller, but leave debug info in cactus_consolidated
bash -O extglob -c "strip -d ${binPackageDir}/bin/!(cactus_consolidated) 2> /dev/null || true"
if [ -z ${CACTUS_LEGACY_ARCH+x} ]	
then
	 outArchive=${buildDir}/cactus-bin-${REL_TAG}.tar.gz
else
	 outArchive=${buildDir}/cactus-bin-legacy-${REL_TAG}.tar.gz
fi
tar -czf ${outArchive} ${binPackageDir}
if [ "$keep" = "no" ] ; then
    rm -Rf ${binBuildDir} ${binPackageDir}
fi
