#!/bin/bash
# Download and statically build tools needed for cactus-refmap and cactus-graphmap.  They are
# - minimap2
# - minigraph
# - gfatools
# - dna-brnn
# - cactus-gfa-tools
# - samtools
# - bedtools
# The following tools are included to export and work with pangenome graph formats
# - hal2vg
# - vg
# If, updating any versions here, update the release notes as well
#
# 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

pangenomeBuildDir=$(realpath -m build-pangenome-tools)
binDir=$(pwd)/bin
libDir=$(pwd)/lib
# just use cactusRootPath for now
dataDir=$(pwd)/src/cactus
CWD=$(pwd)
# works on MacOS and Linux
if [ -z ${numcpu+x} ]; then
	 numcpu=$(getconf _NPROCESSORS_ONLN)
fi

set -x
rm -rf ${pangenomeBuildDir}
mkdir -p ${pangenomeBuildDir}
mkdir -p ${binDir}

# minimap2
if [ -z ${arm+x} ]
then
    MINI_ARM_FLAGS=""
else
    MINI_ARM_FLAGS="arm_neon=1 aarch64=1"
fi
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/minimap2.git
cd minimap2
git checkout 581f2d7123f651d04c9e103b1c7ea8f7051e909a
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/'
make -j ${numcpu} ${MINI_ARM_FLAGS}
if [[ $STATIC_CHECK -ne 1 || $(ldd minimap2 | grep so | wc -l) -eq 0 ]]
then
	 mv minimap2 ${binDir}
else
	 exit 1
fi

# minigraph
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/minigraph.git
pushd minigraph
git checkout v0.20
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/'
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd minigraph | grep so | wc -l) -eq 0 ]]
then
	 mv minigraph ${binDir}
else
	 exit 1
fi

# gfatools
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/gfatools.git
cd gfatools
git checkout v0.5
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/'
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd gfatools | grep so | wc -l) -eq 0 ]]
then
	 mv gfatools ${binDir}
else
	 exit 1
fi

#samtools
cd ${pangenomeBuildDir}
wget -q https://github.com/samtools/samtools/releases/download/1.11/samtools-1.11.tar.bz2
tar -xf samtools-1.11.tar.bz2
cd samtools-1.11
SAMTOOLS_CONFIG_OPTS=""
if [[ $STATIC_CHECK -eq 1 ]]
then
	 SAMTOOLS_CONFIG_OPTS="--disable-shared --enable-static"
fi
./configure --without-curses --disable-libcurl --enable-configure-htslib $SAMTOOLS_CONFIG_OPTS
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd samtools | grep so | wc -l) -eq 0 ]]
then
	 mv samtools ${binDir}
else
	 exit 1
fi
cd htslib-1.11
make -j ${numcpu} tabix
make -j ${numcpu} bgzip
if [[ $STATIC_CHECK -ne 1 || $(ldd tabix | grep so | wc -l) -eq 0 ]]
then
	 mv tabix ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd bgzip | grep so | wc -l) -eq 0 ]]
then
	 mv bgzip ${binDir}
else
	 exit 1
fi

#bcftools
cd ${pangenomeBuildDir}
wget -q https://github.com/samtools/bcftools/releases/download/1.19/bcftools-1.19.tar.bz2
tar -xf bcftools-1.19.tar.bz2
cd bcftools-1.19
SAMTOOLS_CONFIG_OPTS=""
if [[ $STATIC_CHECK -eq 1 ]]
then
	 SAMTOOLS_CONFIG_OPTS="--disable-bcftools-plugins"
fi
./configure $SAMTOOLS_CONFIG_OPTS
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd bcftools | grep so | wc -l) -eq 0 ]]
then
	 mv bcftools ${binDir}
else
	 exit 1
fi

#bedtools
cd ${pangenomeBuildDir}
wget -q https://github.com/arq5x/bedtools2/releases/download/v2.30.0/bedtools.static.binary
mv bedtools.static.binary bedtools
chmod +x bedtools
if [[ $STATIC_CHECK -ne 1 || $(ldd bedtools | grep so | wc -l) -eq 0 ]]
then
	 mv bedtools ${binDir}
else
	 exit 1
fi

# dna-brnn
cd ${pangenomeBuildDir}
git clone https://github.com/lh3/dna-nn.git
cd dna-nn
git checkout 2e6d242ae339457b985f50086e85194c3ce418b1
# hack in flags support
sed -i Makefile -e 's/CFLAGS=/CFLAGS+=/' -e 's/LIBS=/LIBS+=/'
if [ -z ${CFLAGS+x} ]
then
	 make -j ${numcpu}
else
	 LIBS="${CFLAGS}" make -j ${numcpu}
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd dna-brnn | grep so | wc -l) -eq 0 ]]
then
	 mv dna-brnn ${binDir}
	 cp models/attcc-alpha.knm ${dataDir}
else
	 exit 1
fi

# cactus-gfa-tools
cd ${pangenomeBuildDir}
git clone https://github.com/ComparativeGenomicsToolkit/cactus-gfa-tools.git
cd cactus-gfa-tools
git checkout 0c17bc4ae9a7cf174fa40805cde7f8f1f6de8225
make -j ${numcpu}
if [[ $STATIC_CHECK -ne 1 || $(ldd paf2lastz | grep so | wc -l) -eq 0 ]]
then
	 mv paf2lastz ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd gaf2paf | grep so | wc -l) -eq 0 ]]
then
	 mv gaf2paf ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd gaf2unstable | grep so | wc -l) -eq 0 ]]
then
	 mv gaf2unstable ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd gaffilter | grep so | wc -l) -eq 0 ]]
then
	 mv gaffilter ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd rgfa-split | grep so | wc -l) -eq 0 ]]
then
	 mv rgfa-split ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd rgfa2paf | grep so | wc -l) -eq 0 ]]
then
	 mv rgfa2paf ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd pafcoverage | grep so | wc -l) -eq 0 ]]
then
	 mv pafcoverage ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd pafmask | grep so | wc -l) -eq 0 ]]
then
	 mv pafmask ${binDir}
else
	 exit 1
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd paf2stable | grep so | wc -l) -eq 0 ]]
then
	 mv paf2stable ${binDir}
else
	 exit 1
fi

# hal2vg
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.1.7/hal2vg
chmod +x hal2vg
if [[ $STATIC_CHECK -ne 1 || $(ldd hal2vg | grep so | wc -l) -eq 0 ]]
then
	 mv hal2vg ${binDir}
else
	 exit 1
fi
# clip-vg
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.1.7/clip-vg
chmod +x clip-vg
if [[ $STATIC_CHECK -ne 1 || $(ldd clip-vg | grep so | wc -l) -eq 0 ]]
then
	 mv clip-vg ${binDir}
else
	 exit 1
fi
# halRemoveDupes
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.1.7/halRemoveDupes
chmod +x halRemoveDupes
if [[ $STATIC_CHECK -ne 1 || $(ldd halRemoveDupes | grep so | wc -l) -eq 0 ]]
then
	 mv halRemoveDupes ${binDir}
else
	 exit 1
fi
# halMergeChroms
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.1.7/halMergeChroms
chmod +x halMergeChroms
if [[ $STATIC_CHECK -ne 1 || $(ldd halMergeChroms | grep so | wc -l) -eq 0 ]]
then
	 mv halMergeChroms ${binDir}
else
	 exit 1
fi

# halUnclip
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.1.7/halUnclip
chmod +x halUnclip
if [[ $STATIC_CHECK -ne 1 || $(ldd halUnclip | grep so | wc -l) -eq 0 ]]
then
	 mv halUnclip ${binDir}
else
	 exit 1
fi

# filter-paf-deletions
cd ${pangenomeBuildDir}
wget -q https://github.com/ComparativeGenomicsToolkit/hal2vg/releases/download/v1.1.7/filter-paf-deletions
chmod +x filter-paf-deletions
if [[ $STATIC_CHECK -ne 1 || $(ldd filter-paf-deletions | grep so | wc -l) -eq 0 ]]
then
	 mv filter-paf-deletions ${binDir}
else
	 exit 1
fi

# vg
cd ${pangenomeBuildDir}
#wget -q https://github.com/vgteam/vg/releases/download/v1.59.0/vg
wget -q http://public.gi.ucsc.edu/~hickey/vg-patch/vg.c56049888367a34de69d1d467e9757efbb84b618 -O vg
chmod +x vg
if [[ $STATIC_CHECK -ne 1 || $(ldd vg | grep so | wc -l) -eq 0 ]]
then
	 mv vg ${binDir}
else
	 exit 1
fi	 

# gfaffix
cd ${pangenomeBuildDir}
wget -q https://github.com/marschall-lab/GFAffix/releases/download/0.1.5b/GFAffix-0.1.5b_linux_x86_64.tar.gz
tar xzf GFAffix-0.1.5b_linux_x86_64.tar.gz
chmod +x GFAffix-0.1.5b_linux_x86_64/gfaffix
if [[ $STATIC_CHECK -ne 1 || $(ldd GFAffix-0.1.5b_linux_x86_64/gfaffix | grep so | wc -l) -eq 0 ]]
then
	 mv GFAffix-0.1.5b_linux_x86_64/gfaffix ${binDir}
else
	 exit 1
fi

# vcfbub
cd ${pangenomeBuildDir}
wget -q https://github.com/pangenome/vcfbub/releases/download/v0.1.1/vcfbub
chmod +x vcfbub
if [[ $STATIC_CHECK -ne 1 || $(ldd vcfbub | grep so | wc -l) -eq 0 ]]
then
	 mv vcfbub ${binDir}
else
	 exit 1
fi

# mash
cd ${pangenomeBuildDir}
wget -q https://github.com/marbl/Mash/releases/download/v2.3/mash-Linux64-v2.3.tar
tar -xf mash-Linux64-v2.3.tar
cd mash-Linux64-v2.3
mv mash ${binDir}
# note:
# disabling the static check as this binary is not fully static.
# but.. I can't figure out how to build proper static binary from
# source and this one seems broadly compatible with many systems...

# odgi
cd ${pangenomeBuildDir}
wget -q https://github.com/pangenome/odgi/releases/download/v0.8.6/odgi-v0.8.6.tar.gz
tar zxf odgi-v0.8.6.tar.gz
cd odgi-v0.8.6
if [[ $STATIC_CHECK -eq 1 ]]
then
    CXXFLAGS="" CFLAGS="" LDFLAGS="" LIBS="" cmake -DBUILD_STATIC=1 -DCMAKE_BUILD_TYPE=Generic -H. -Bbuild && CXXFLAGS="" CFLAGS="" LDFLAGS="" LIBS="" cmake --build build -- -j ${numcpu}
else
    cmake -H. -Bbuild -DCMAKE_BUILD_TYPE=Generic && cmake --build build -- -j ${numcpu}
fi
if [[ $STATIC_CHECK -ne 1 || $(ldd bin/odgi | grep so | wc -l) -eq 0 ]]
then
    mv bin/odgi ${binDir}
else
    exit 1
fi

# vcfwave
cd ${pangenomeBuildDir}
# todo: it's be nice to figure out a static build. for now we skip entirely if static enabled.
if [[ $STATIC_CHECK -ne 1 ]]
then
    git clone --recursive https://github.com/vcflib/vcflib.git
    cd vcflib
    git checkout 7c1a31a430d339adcb9a0c2fd3fd02d3b30e3549
    mkdir build
    cd build
    cmake -DZIG=OFF -DCMAKE_BUILD_TYPE=Debug ..
    cmake --build . -- -j ${numcpu}
    mv vcfwave vcfcreatemulti vcfbreakmulti vcfuniq ${binDir}
    mv ./contrib/WFA2-lib/libwfa2.so.0 ${libDir}
fi
    
cd ${CWD}
rm -rf ${pangenomeBuildDir}


set +x
echo ""
echo "--------------------------------------------------"
echo "(re)run pip install -U . to install dna-brnn model"
echo "--------------------------------------------------"