#! /bin/bash

set -e

source ./info.sh

if [[ -z $ROOTFS_PATH ]]
then
	echo "Error: ROOTFS PATH is not set!"
	exit 1
fi

# Mount

mount --bind /dev "$ROOTFS_PATH/dev"
mount -t tmpfs run "$ROOTFS_PATH/run" -o mode=0755,nosuid,nodev
mount -t proc proc "$ROOTFS_PATH/proc" -o nosuid,nodev,noexec
mount -t sysfs sys "$ROOTFS_PATH/sys" -o nosuid,nodev,noexec,ro

# Setup Chroot scripts

cp -rvf ./chroot_scripts "$ROOTFS_PATH/"
cp -rvf ./hooks "$ROOTFS_PATH/chroot_scripts/"
cp -rvf ./live-lists "$ROOTFS_PATH/chroot_scripts/"
cp -rvf ./pool-lists "$ROOTFS_PATH/chroot_scripts/"
cp -rvf ./rem-lists "$ROOTFS_PATH/chroot_scripts/"
cp -rvf ./info.sh "$ROOTFS_PATH/chroot_scripts/"

# Run chroot_script inside ROOTFS

chroot "$ROOTFS_PATH" bash -c "/chroot_scripts/0-chroot.sh"
rm -rfv "$ROOTFS_PATH/chroot_scripts"

# Unmount

umount "$ROOTFS_PATH/dev" || umount -lf "$ROOTFS_PATH/dev" || true
umount "$ROOTFS_PATH/run" || umount -lf "$ROOTFS_PATH/run" || true
umount "$ROOTFS_PATH/proc" || umount -lf "$ROOTFS_PATH/proc" || true
umount "$ROOTFS_PATH/sys" || umount -lf "$ROOTFS_PATH/sys" || true

# Generate Squashfs image

mksquashfs \
    "$ROOTFS_PATH" \
    "$LIVE_BOOT_LIVE_PATH/filesystem.squashfs" \
    -noappend \
    -comp zstd \
    -Xcompression-level 19 \
    -b 1M
    
# Copy Kernel to live (Disabled, Copy Kernels to refind instead)
#cp "$ROOTFS_PATH/boot"/vmlinuz-* \
#    "$LIVE_BOOT_LIVE_PATH/vmlinuz" && \
#cp "$ROOTFS_PATH/boot"/initrd.img-* \
#    "$LIVE_BOOT_LIVE_PATH/initrd" && \
#cp ./data/refind/refind_linux.conf \
#    "$LIVE_BOOT_LIVE_PATH/refind_linux.conf"

# Generate bootable EFI Image from refind

# Copy kernel to refind
mkdir -p ./data/refind/EFI
cp -vf "$ROOTFS_PATH/boot"/vmlinuz-"$ISO_KERNEL" "./data/refind/EFI/vmlinuz"
#cp -vf "$ROOTFS_PATH/boot"/initrd.img-"$ISO_KERNEL" "./data/refind/EFI/initrd"
cp -vf "$ROOTFS_PATH/boot"/booster.img-"$ISO_KERNEL" "./data/refind/EFI/initrd"

EFI_BOOT_IMAGE_SIZE=$(($(du -s -B1048576 ./data/refind | cut -f1) + 10))
EFI_BOOT_IMAGE="$LIVE_BOOT_PATH/efiboot.img"

# Define VTOY_ISO_NAME in refind
sed -i "s#THE_NAME_OF_CURRENT_ISO_FOR_VENTOY#$ISO_IMAGE".iso"#g" ./data/refind/refind_linux.conf
sed -i "s#THE_NAME_OF_CURRENT_ISO_FOR_VENTOY#$ISO_IMAGE".iso"#g" ./data/refind/EFI/boot/refind.conf

# Create Refind Boot Image
dd if=/dev/zero of="$EFI_BOOT_IMAGE" bs=1M count=$EFI_BOOT_IMAGE_SIZE
mkfs.vfat -F 32 "$EFI_BOOT_IMAGE"

for directory in $(find ./data/refind/EFI/ -type d | cut -d'/' -f4-)
do
    echo "creating directory $directory in $EFI_BOOT_IMAGE"
    mmd -i "$EFI_BOOT_IMAGE" ::"$(echo $directory | tr '[:lower:]' '[:upper:]' | sed 's:/*$::')"
done
    
for file in $(find ./data/refind/EFI/ -type f)
do
    echo "copying file $file to $EFI_BOOT_IMAGE"
    mcopy -i "$EFI_BOOT_IMAGE" $file ::"$(echo $file | cut -d'/' -f4- | tr '[:lower:]' '[:upper:]')"
done

# Create ISO Image
mkdir -p ./output

xorriso \
    -as mkisofs \
    -iso-level 3 \
    -V "PikaOS 4" \
    -partition_offset 16 \
    -appended_part_as_gpt \
    -no-pad \
    -no-emul-boot \
    -append_partition 2 0xef "$EFI_BOOT_IMAGE" \
    --efi-boot --interval:appended_partition_2:all:: \
    -o "./output/$ISO_IMAGE".iso \
    "$LIVE_BOOT_DATA_PATH"

# Generate an integrity sum
touch "./output/$ISO_IMAGE".md5
md5sum "./output/$ISO_IMAGE".iso > "./output/$ISO_IMAGE".md5