#!/bin/bash

#the dir containing our debian source data
SRCDIR=$(pwd)
REPODIR="$(dirname "${SRCDIR}")"

#target dir where to put build packages
DOWNLOAD=${HOME}/bffh-debian
mkdir -p ${DOWNLOAD}/

# architecture mapping array linux <-> rust
declare -A ARCHES_LR=(
	["armv7"]=armv7-unknown-linux-gnueabihf
	["aarch64"]=aarch64-unknown-linux-gnu
	["amd64"]=x86_64-unknown-linux-gnu
	)

#architecture mapping array linux <-> docker ubuntu:noble
# - https://hub.docker.com/_/ubuntu
# - https://github.com/docker-library/official-images#architectures-other-than-amd64
declare -A ARCHES_LD=(
	["armv7"]=arm/v7 #it's NOT arm32/v7
	["aarch64"]=arm64/v8
	["amd64"]=NATIVE #just some pseudo
	)

#architecture mapping array linux <-> debian package control file
declare -A ARCHES_DP=(
	["armv7"]=armhf
	["aarch64"]=arm64
	["amd64"]=amd64
	)

echo -e "SRCDIR: ${SRCDIR}"
BFFHV=$(grep "Standards-Version: " fabaccess-bffh-src/debian/control | awk -F ' ' '{print $2}')
echo -e "BFFH target build version: ${BFFHV}"
echo -e "BFFH packages (*.deb/*.rpm) will be put here: ${DOWNLOAD}"

# https://www.cyberciti.biz/faq/bash-for-loop-array/
for ARCH_L in "${!ARCHES_LR[@]}"; do

    ARCH_R=${ARCHES_LR[$ARCH_L]}
    ARCH_D=${ARCHES_LD[$ARCH_L]}
    ARCH_P=${ARCHES_DP[$ARCH_L]}

	echo -e "\n+++++++++++++++++++++++++++++++++++++++++++"
	echo -e "ARCH_L (Linux)           : ${ARCH_L}"
	echo -e "ARCH_R (Rust)            : ${ARCH_R}"
	echo -e "ARCH_D (Docker Registry) : ${ARCH_D}"
	echo -e "ARCH_P (Debian Package)  : ${ARCH_P}"
	echo -e "+++++++++++++++++++++++++++++++++++++++++++\n"

	# remove existing target dir if exists (from possible previous builds)
	if [ -d fabaccess-bffh-${BFFHV}/ ]; then
		sudo rm -rf fabaccess-bffh-${BFFHV}/ #we use sudo because podman will use root permissions
	fi

	# fix possible permission issues from previous builds
	sudo chown -R $USER:$USER ${SRCDIR}/

	# create a working dir copy which has the correct name (required by dpkg-buildpackage command)
	cp -R fabaccess-bffh-src/ fabaccess-bffh-${BFFHV}/

	# replace target architecture in control file
	sed "s/Architecture: {{REPLACE_ME}}/Architecture: any/" -i fabaccess-bffh-${BFFHV}/debian/control

	# declare required compiled binaries and check for existence
	BIN_BFFHD=${REPODIR}/target/${ARCH_R}/release/bffhd
	BIN_FABFIRE_PROVISION=${REPODIR}/target/${ARCH_R}/release/fabfire_provision

	if [ -f ${BIN_BFFHD} ]; then
		cp ${BIN_BFFHD} fabaccess-bffh-${BFFHV}/usr/bin/
	else
		echo -e "Error: ${BIN_BFFHD} does not exist!"
		exit 1
	fi
	if [ -f ${BIN_FABFIRE_PROVISION} ]; then
		cp ${BIN_FABFIRE_PROVISION} fabaccess-bffh-${BFFHV}/usr/bin/
	else
		echo -e "Error: ${BIN_FABFIRE_PROVISION} does not exist!"
		exit 1
	fi


	if [[ "${ARCH_L}" == "amd64" ]]; then
		echo -e "native architecture. no podman required. Building deb + rpm"
		cd ${SRCDIR}/fabaccess-bffh-${BFFHV}/
		#dpkg-buildpackage -us -uc -k=B8F5D56C64A6161B35FB4892188C8D1E501EBD41 --host-arch "${ARCH_L}" --target-arch "${ARCH_L}"
		dpkg-buildpackage -us -uc -k=B8F5D56C64A6161B35FB4892188C8D1E501EBD41
		cd ${SRCDIR}/
		cp fabaccess-bffh_${BFFHV}_${ARCH_L}.deb /tmp/
		cd /tmp/
		sudo alien --keep-version --verbose --scripts --to-rpm /tmp/fabaccess-bffh_${BFFHV}_${ARCH_L}.deb
		cp /tmp/fabaccess-bffh_${BFFHV}_*.deb ${DOWNLOAD}/
		cp /tmp/fabaccess-bffh-${BFFHV}-*.rpm ${DOWNLOAD}/

	else
		echo -e "using podman. Building deb + rpm"
		#podman ps -a

		# create fresh podman container with ...
		# - a name using --name fabinfra_deb_arm64
		# - fitting target architecture using --platform
		# - starting detached (-d) to run in background (for attaching later on)
		# - start to operate on it with interactive shell --interactive --tty (or short: -it)
		# - overwrite existing container if existent by using --replace
		# - adding the src and download directories to pass it into container by -v <host_dir>:<container_dir>
		# - some environment vars (ARCH_LINUX, ARCH_RUST, required by the debian packager)
		sudo podman run --replace -d --tty --interactive -v ${SRCDIR}:/srcdir -v ${DOWNLOAD}:/download -e ARCH_LINUX=${ARCH_L} -e ARCH_RUST=${ARCH_R} --name fabinfra_deb_${ARCH_L} --platform linux/${ARCH_D} localhost/fabinfra/debianpackage_${ARCH_D}

		# now attach to that container and start building the packages
		#sudo podman exec -u 0 --tty --interactive fabinfra_deb_${ARCH_L} bash
		sudo podman exec -u 0 --tty --interactive fabinfra_deb_${ARCH_L} bash -c "
            apt update && apt upgrade;
        	cd /srcdir/fabaccess-bffh-${BFFHV}/;
			dpkg-buildpackage -us -uc -k=B8F5D56C64A6161B35FB4892188C8D1E501EBD41;
			cd /srcdir/;
			cp fabaccess-bffh_${BFFHV}_${ARCH_P}.deb /tmp/;
			cd /tmp/;
			alien --keep-version --verbose --scripts --to-rpm /tmp/fabaccess-bffh_${BFFHV}_${ARCH_P}.deb;
			cp /tmp/fabaccess-bffh_${BFFHV}_*.deb /download/;
			cp /tmp/fabaccess-bffh-${BFFHV}-*.rpm /download/
		"
	fi
done

echo -e "\n+++++++++++++++++++++++++++++++++++++++++++"
echo -e "Everything done. Built BFFH packages (*.deb/*.rpm) moved to: ${DOWNLOAD}"
echo -e "+++++++++++++++++++++++++++++++++++++++++++\n"