#!/bin/bash
# Make a gpu-enabled docker image and push it to quay
# Todo: it'd be nice to get travis to do this to be more consistent with normal docker
# Note: must be run for cactus/ directory

set -x
set -beEu -o pipefail
mydir=$(dirname $(which $0))
source ${mydir}/releaseLib.sh

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

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

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

CFLAGS="" CXXFLAGS="" docker build . -f Dockerfile.kegalign -t kegalign:local --progress=plain
# switch the runtime image to kegalign, and the build image to the build image from Dockerfile.kegalign
# important, if the build image in Dockerfile.segaling changes, the line below needs to be updated too
sed '0,/FROM/! s/FROM.*/FROM kegalign:local/' Dockerfile  | sed -e '0,/FROM/s/FROM.*/FROM nvidia\/cuda:12.6.2-devel-ubuntu22.04 as builder/g' > Dockerfile.gpu
# enable gpu by default
sed -i src/cactus/cactus_progressive_config.xml -e 's/gpu="0"/gpu="all"/g' -e 's/realign="1"/realign="0"/'
docker build . -f Dockerfile.gpu -t ${dockname}:${REL_TAG}-gpu --progress=plain
# disable it again
sed -i src/cactus/cactus_progressive_config.xml -e 's/gpu="all"/gpu="0"/g' -e 's/realign="0"/realign="1"/'
read -p "Are you sure you want to push ${dockname}:${REL_TAG}-gpu to quay?" yn
case $yn in
    [Yy]* ) docker push ${dockname}:${REL_TAG}-gpu; break;;
    [Nn]* ) exit;;
    * ) echo "Please answer yes or no.";;
esac
popd
