#!/bin/bash

export CPATH="${PREFIX}/include"
export LDFLAGS="${LDFLAGS} -L${PREFIX}/lib"
export CPPFLAGS="${CPPFLAGS} -I${PREFIX}/include"
export CXXFLAGS="${CXXFLAGS} -O3 -std=c++14"

cd "${SRC_DIR}"/gargammel || { echo "Folder ${SRC_DIR}/gargammel not found"; exit 1; }

# Avoid conflicts with C++20
mv "${SRC_DIR}"/gargammel/libgab/gzstream/version "${SRC_DIR}"/gargammel/libgab/gzstream/version.txt

mkdir -p "${SRC_DIR}"/gargammel/bamtools/{lib,api,shared}
ln -s "${PREFIX}"/lib/libbamtools* "${SRC_DIR}"/gargammel/bamtools/lib/
ln -s "${PREFIX}"/include/bamtools/api/* "${SRC_DIR}"/gargammel/bamtools/api/
ln -s "${PREFIX}"/include/bamtools/shared/* "${SRC_DIR}"/gargammel/bamtools/shared/

## Following https://bioconda.github.io/contributor/troubleshooting.html#g-or-gcc-not-found
binaries=(src/fragSim src/deamSim src/adptSim src/fasta2fastas src/damage_patterns2prof src/mapDamage2prof)
# We list the targets explicitly to prevent the Makefile from downloading and
# building the external art_illumina.o dependency (added as a runtime
# requirement instead)
make CC="${CXX}" BAMTOOLSINC="${PREFIX}/include/bamtools" BAMTOOLSLIB="${PREFIX}/lib" "${binaries[@]}"
mkdir -p "${PREFIX}/bin"
install -v -m 0755 "${binaries[@]}" "${PREFIX}/bin"

# Fix hard-coded paths to external tools
sed -i.bak '1 s|^.*$|#!/usr/bin/env perl|g' src/addMethyl.pl
rm -rf src/*.bak
sed \
  -e 's|$pathdir."/src/|"|' \
  -e 's|$pathdir."/art_src_MountRainier/|"|' \
  -e 's|^fileExists(.*);||' \
  -e 's|#!/usr/bin/perl|#!/usr/bin/env perl|' \
  gargammel.pl > "${PREFIX}"/bin/gargammel
