#!/bin/sh

# Copyright (C) 2025 Pädagogisches Landesinstitut Rheinland-Pfalz
# Copyright (C) 2025 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

# Release generated ISO / SquashFS images.
#
# This script copies the latest ISO image / SquashFS image / FAI config space
# tarball from the staging area at
# /var/lib/<product>-fai/{iso-images,squashfs-images,config},
# to
# /var/lib/<product>-fai/{iso-images,squashfs-images,config}.production,
# adds a timestamp to the names of the released files and updates the symlinks
# to the latest versions of those files.

set -e

LC_ALL=C
export LC_ALL

# Make sure the created directories and files are readable by tfptd
# run as user nobody.
umask 022

product="$(echo $(basename $0) | sed -Ee "s/(.+)-fai_release/\1/")"

###
### FAI Setup
###

[ "$archs" ]            || archs=$(command -V dpkg 1>/dev/null 2>/dev/null && dpkg --print-architecture || uname -m)
[ "$codenames" ]        || codenames=$(cat /etc/os-release | grep VERSION_CODENAME | cut -d "=" -f2)

# source debian-edu-fai's config file
# Allow site specific overrides to the variables
if [ -f /etc/debian-edu/${product}-fai.conf ] ; then
	. /etc/debian-edu/${product}-fai.conf
fi

release_timestamp=$(date +%Y%m%d%H%M%S)

for codename in ${codenames}; do

	# skip codenames that don't sound like Debian suites...
	if ! echo "bullseye bookworm trixie forky sid unstable" | grep -q "${codename}"; then
		echo "WARNING: The name '${codename}' is not a known and recent Debian distribution codename. Skipping..."
		continue
	fi

	# iterate over configured FAI client architectures...
	for arch in ${archs}; do

		for what in iso-images squashfs-images config; do

			case ${what} in

				"iso-images")
					file_suffix="iso"
					extra=""
					;;
				"squashfs-images")
					file_suffix="img"
					extra=""
					;;
				"config")
					file_suffix="tar.gz"
					extra="_fai-configspace"
					;;
				*)
					echo "SOMETHING WENT REALLY WRONG, THIS SHOULD NOT HAPPEN!!!"
					exit -1
			esac

			mkdir -p /var/lib/${product}-fai/${what}.production/

			# create backup of previous ISO image
			if [ -e "/var/lib/${product}-fai/${what}/${product}${extra}.${arch}+${codename}.${file_suffix}" ]; then
				cp -v "/var/lib/${product}-fai/${what}/${product}${extra}.${arch}+${codename}.${file_suffix}" \
				      "/var/lib/${product}-fai/${what}.production/${product}-${release_timestamp}${extra}.${arch}+${codename}.${file_suffix}"
				ln -vsf "/var/lib/${product}-fai/${what}.production/${product}-${release_timestamp}${extra}.${arch}+${codename}.${file_suffix}" \
				        "/var/lib/${product}-fai/${what}.production/${product}-latest${extra}.${arch}+${codename}.${file_suffix}"
			fi
		done

	done

done
