#!/bin/bash

# Prikaz pro stazeni baliku
# wget -O build.zip "https://gitlab.cesnet.cz/api/v4/projects/586/jobs/artifacts/master/download?job=build:bullseye"
# unzip -j build.zip -d /storage/software/repository/packages/bullseye/main/
# rm build.zip

set -x

# Nastaveni promennych, pokud je SINVERSION zakomentovana, pokusime se odhadnout nejnovejsi
export DEBIAN_FRONTEND=noninteractive
#export GOVERSION=go1.17.6
export OS=linux ARCH=amd64
#export SINVERSION=3.9.2

# Nacteni promennych o bezicim systemu vcetne verzi
. /etc/os-release

# Od Debian 11 Bullseye ma debian-security trochu jinou cestu, abychom mohli pouzit heredoc, rozskocime si pres funkci
function get_debian_security() {
    if [ "${VERSION_ID}" -lt "11" ]; then echo "${VERSION_CODENAME}/updates"; else echo "${VERSION_CODENAME}-security"; fi
}

# Vygenerovani sources.list podle verze Debianu
cat > /etc/apt/sources.list << EOSOURCES
deb http://ftp.zcu.cz/pub/linux/debian ${VERSION_CODENAME} main contrib non-free
deb http://ftp.zcu.cz/pub/linux/debian ${VERSION_CODENAME}-updates main contrib non-free
deb http://ftp.zcu.cz/pub/linux/debian-security $(get_debian_security) main contrib non-free

deb-src http://ftp.zcu.cz/pub/linux/debian ${VERSION_CODENAME} main contrib non-free
deb-src http://ftp.zcu.cz/pub/linux/debian ${VERSION_CODENAME}-updates main contrib non-free
deb-src http://ftp.zcu.cz/pub/linux/debian-security $(get_debian_security) main contrib non-free
EOSOURCES

# Instalace buildovacich veci a zavislosti
apt update -qqq
apt install -yqqq build-essential dpkg-dev debhelper build-essential libssl-dev uuid-dev libseccomp-dev pkg-config squashfs-tools cryptsetup wget curl git devscripts dh-golang libglib2.0-dev libfuse-dev dh-apparmor libfuse3-dev

# Automaticka detekce posledni releasnute verze
SINVERSION=${SINVERSION:-$(curl -L -s https://github.com/sylabs/singularity/releases/latest | grep -Po "\/v\K\d+\.\d+\.\d+" | head -1)}
GOVERSION=${GOVERSION:-$(curl -s https://go.dev/VERSION?m=text | head -1)}

# Priprava adresare pro hotove balicky a zapamatovani cesty
mkdir build
export BUILD=${PWD}/build
export ROOTPWD=${PWD}

# Stazeni a instalace GO
wget -q -O /tmp/${GOVERSION}.${OS}-${ARCH}.tar.gz https://dl.google.com/go/${GOVERSION}.${OS}-${ARCH}.tar.gz && tar -C /usr/local -xzf /tmp/${GOVERSION}.${OS}-${ARCH}.tar.gz
rm /tmp/${GOVERSION}.${OS}-${ARCH}.tar.gz

# Nastaveni promennych GO
echo 'export GOPATH=${HOME}/go' >> ~/.bashrc
echo 'export PATH=/usr/local/go/bin:${PATH}:${GOPATH}/bin' >> ~/.bashrc
source ~/.bashrc

# Stazeni a rozbaleni zdrojovych kodu
mkdir -p ${GOPATH}/src/github.com/sylabs && cd ${GOPATH}/src/github.com/sylabs && wget -q -O singularity.tar.gz https://github.com/sylabs/singularity/releases/download/v${SINVERSION}/singularity-ce-${SINVERSION}.tar.gz && tar xvf singularity.tar.gz && cd singularity-ce-${SINVERSION}

#sed 's#github.com/containers/image/v5 .*#github.com/containers/image/v5 v5.31.0#g' go.mod | diff go.mod -
#sed 's#github.com/containers/image/v5 .*#github.com/containers/image/v5 v5.31.0#g' go.mod -i
#
#sed 's#github.com/containers/image/v5 .*#github.com/containers/image/v5 v5.31.0#g' vendor/modules.txt | diff vendor/modules.txt -
#sed 's#github.com/containers/image/v5 .*#github.com/containers/image/v5 v5.31.0#g' vendor/modules.txt -i
#
#sed 's#\(.*\)#\1\+1#g' VERSION | diff VERSION -
#sed 's#\(.*\)#\1\+1#g' VERSION -i

# Stazeni PRoot a pridani do .install aby se includoval do balicku
curl -LO https://proot.gitlab.io/proot/bin/proot
chmod +x ./proot
echo "proot usr/bin" >> debian/singularity-ce.install

# Patch upraveneho cli.go kvuli logovani pouziti do syslogu
#cp -v ${ROOTPWD}/cli.go cmd/singularity/cli.go
cp -vr ${ROOTPWD}/debian/patches debian/

# Build balicku
dpkg-buildpackage -uc -us -b

cd ..

# Presunuti balicku do spravneho adresare, aby ho gitlab vystavil jako artifact
mv *.deb ${BUILD}/
