#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-05-05; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.live.builder.git
# SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency
# SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0
# SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework.
# SPDX-PackageName: CISS.debian.live.builder
# SPDX-Security-Contact: security@coresecret.eu
set -Ceuo pipefail

# The example names get mapped to their roles here
declare timestamp
timestamp=$(date +"%Y%m%d%H%M")
declare -r LABEL="${timestamp:0:4}_${timestamp:4:2}_${timestamp:6:2}-${timestamp:8:2}_${timestamp:10:2}"
declare -r SEQNO="${timestamp:0:4}.${timestamp:4:2}.${timestamp:6:2}-${timestamp:8:2}:${timestamp:10:2}"
declare -r ISO_ORIGINAL="/opt/netinstaller/debian-12.8.0-amd64-netinst.iso"
declare -r IMGCENTURION="/mnt/debian-original"
declare -r ISO_MODIFIED="/root/${LABEL}-CISS-12.8.0-amd64-netinst.iso"
declare -r MBR_TEMPLATE="isohdpfx.bin"
declare size
size=$(xorriso -as mkisofs -print-size "${IMGCENTURION}" | tail -n 1 | awk '{print $1}')
clear

echo "Sequence No.   : ${SEQNO}"
echo "Estimated Size : ${size}"

# Extract MBR template file to disk
dd if="${ISO_ORIGINAL}" bs=1 count=432 of="${MBR_TEMPLATE}"

# Create the new ISO image
xorriso -as mkisofs \
  -r \
  -volid 'CISS Debian 12.8.0 x86_64' \
  -appid 'Centurion Debian Installer' \
  -volset 'CISS.hardened.bookworm' \
  -volset-seqno "${SEQNO}" \
  -volset-size "${size}" \
  -publisher 'Centurion Intelligence Consulting Agency' \
  -sysid 'GNU/Linux amd64' \
  -copyright 'COPYRIGHT' \
  -o "${ISO_MODIFIED}" \
  -J -J -joliet-long -cache-inodes \
  -isohybrid-mbr "${MBR_TEMPLATE}" \
  -b isolinux/isolinux.bin \
  -c isolinux/boot.cat \
  -boot-load-size 4 -boot-info-table -no-emul-boot \
  -eltorito-alt-boot \
  -e boot/grub/efi.img \
  -no-emul-boot -isohybrid-gpt-basdat -isohybrid-apm-hfsplus \
  "${IMGCENTURION}"

# Check output of new ISO image
echo ""
file "${ISO_MODIFIED}"
echo ""
isoinfo -d -i "${ISO_MODIFIED}"
echo ""
file "${ISO_MODIFIED}"
echo ""

exit 0
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
