#!/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
# Todo: It would be more portable to use "sse41", but that leads to segfaults in rare cases
# https://github.com/yangao07/abPOA/issues/26
	 export avx2=1
else
	 export sse2=1
	 # haven't tested abpoa on older architectures, turn it off by default
	 sed -i src/cactus/cactus_progressive_config.xml -e 's/partialOrderAlignment="1"/partialOrderAlignment="0"/g'
fi

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

# 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_2.tar.gz
tar -zxf hdf5-1_10_2.tar.gz
cd  hdf5-hdf5-1_10_2
./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.7.2.tar.gz
tar -zxf libxml2-2.7.2.tar.gz
cd libxml2-2.7.2
./configure --enable-static --disable-shared --prefix=${DEP_PREFIX}
make -j $(nproc)
make install
export CACTUS_STATIC_LINK_FLAGS="-L${DEP_PREFIX}/lib -lz -lpthread -lm -llzma"
export CACTUS_LIBXML2_INCLUDE_PATH=${DEP_PREFIX}/include/libxml2
popd

# install Phast and enable halPhyloP compilation
build-tools/downloadPhast
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
# (only works for newer architectures as vg and some other binaries are built with -march=nehalem)
# todo: should try to just use nocona for everything and make one release 
if [ -z ${CACTUS_LEGACY_ARCH+x} ]	
then
	 build-tools/downloadPangenomeTools 1
fi

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
# make binaries smaller, but leave debug info in cactus_consolidated
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
