#!/bin/bash

set -e
set -o pipefail # Bashism

if [ -e .mirror ]; then
    mirror=$(cat .mirror)
else
    mirror=https://4n0nym0u5.mirror.rfc2549.network/direct/parrot/
fi

temp=""
arch=${ARCH:-$(dpkg --print-architecture)}
dist=${DIST:-"echo"}
variant=${VARIANT:-"kde"}
ISO_VERSION=${ISO_VERSION:-"weekly"}

if [ ! -e ${LIVE_BUILD:-/usr/share/live/build}/data/debian-cd/$dist ]; then
    if [ -w ${LIVE_BUILD:-/usr/share/live/build}/data/debian-cd ]; then
        ln -sf sid ${LIVE_BUILD:-/usr/share/live/build}/data/debian-cd/$dist
    fi
fi

if [ ! -e /usr/share/debootstrap/scripts/$dist ]; then
    ln -sf /usr/share/debootstrap/scripts/sid /usr/share/debootstrap/scripts/$dist
fi

# Define options that vary across architectures
case "$arch" in
    amd64)
	#lb_opts="$lb_opts --debian-installer live"
    ;;
    arm64)
	lb_opts="$lb_opts --bootstrap-qemu-arch aarch64 --bootloaders grub-efi --uefi-secure-boot disable"
    ;;
    riscv64)
	lb_opts="$lb_opts --bootstrap-qemu-arch riscv --bootloaders grub-efi --uefi-secure-boot disable"
    ;;
    *)
	echo "WARNING: configuration not tested on arch $arch" >&2
    ;;
esac

# Drop all files that a former run might have installed
for file in $(cd templates && find ./* -type f); do
    file=${file#./*/}
    rm -fv config/$file
done

# Copy configuration
#rsync -az templates/common/ config/
#[ ! -d templates/$variant ] || rsync -az templates/$variant/ config/

[ ! $arch ] || sed -i "s/ISO_ARCH/$arch/g" config/includes.binary/isolinux/menu.cfg
[ ! $arch ] || sed -i "s/ISO_ARCH/$arch/g" config/bootloaders/grub-pc/live-theme/theme.txt
[ ! $ISO_VERSION ] || sed -i "s/ISO_VERSION/$ISO_VERSION/g" config/includes.binary/isolinux/menu.cfg
[ ! $ISO_VERSION ] || sed -i "s/ISO_VERSION/$ISO_VERSION/g" config/bootloaders/grub-pc/live-theme/theme.txt
[ ! $variant ] || sed -i "s/ISO_TITLE/Parrot $variant/g" config/includes.binary/isolinux/menu.cfg
[ ! $variant ] || sed -i "s/ISO_TITLE/Parrot $variant/g" config/bootloaders/grub-pc/live-theme/theme.txt

# live-build uses `umount chroot/sys` during the cleanup phase.
# In environments where "sysfs" is mounted more than once, "umount" may fail. 
# To avoid errors, we replace this command with `umount -dfR chroot/sys`, 
# which forces recursive unmounting and decouples the mounts.
chroot_sysfs_path="/usr/lib/live/build/chroot_sysfs"
if [ -f "$chroot_sysfs_path" ] && grep -q 'umount chroot/sys' "$chroot_sysfs_path"; then
    sed -i 's|umount chroot/sys|umount -dfR chroot/sys|' "$chroot_sysfs_path"
fi

lb config noauto \
	--architecture $arch \
	--distribution $dist \
	--parent-distribution $dist \
	--distribution-binary $dist \
	--parent-distribution-binary $dist \
	--distribution-chroot $dist \
	--parent-distribution-chroot $dist \
	--parent-archive-areas "main contrib non-free non-free-firmware" \
	--archive-areas "main contrib non-free non-free-firmware" \
	--binary-image iso-hybrid \
	--debootstrap-options "--keyring=/usr/share/keyrings/parrot-archive-keyring.gpg --include=gnupg2,base-files,ca-certificates" \
	--keyring-packages parrot-archive-keyring \
	--updates false \
	--backports false \
	--source false \
	--firmware-binary false \
	--firmware-chroot false \
	--mirror-bootstrap "$mirror" \
	--parent-mirror-bootstrap "$mirror" \
	--mirror-debian-installer "$mirror" \
	--parent-mirror-debian-installer "$mirror" \
	--mirror-binary "$mirror" \
	--parent-mirror-binary "$mirror" \
	--mirror-chroot "$mirror" \
	--parent-mirror-chroot "$mirror" \
	--mirror-binary-security "$mirror" \
	--mirror-chroot-security "$mirror" \
	--iso-application "Drater OS" \
	--iso-publisher "Drater Anonymous OS" \
	--iso-volume "Drater $ISO_VERSION" \
	--linux-packages linux-image \
	--memtest memtest86+ \
	--security false \
	--zsync false \
	$lb_opts \
	"$@"
