#!/usr/bin/env zsh
# shellcheck shell=bash
# Copyright (c) 2016-2021 Ivan J. <parazyd@dyne.org>
# This file is part of libdevuansdk
#
# This source code 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 3 of the License, or
# (at your option) any later version.
#
# This software 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 source code. If not, see <http://www.gnu.org/licenses/>.

vars+=(MKEFI)

iso_prepare_strap()
{
	fn iso_prepare_strap
	req=(strapdir)
	ckreq || return 1

	notice "Preparing strapdir for Live CD"

	cat <<EOF | sudo tee "$strapdir/isoprep" >/dev/null
#!/bin/sh
apt-get update
apt-get --yes --force-yes install dialog live-boot live-boot-initramfs-tools || exit 1
apt-get --yes --force-yes --purge autoremove || exit 1
EOF

	chroot-script -d isoprep || { zerr; return 1; }
}

iso_setup_isolinux()
{
	fn iso_setup_isolinux
	req=(workdir strapdir)
	ckreq || return 1

	notice "Setting up isolinux"

	pushd "$workdir" || { zerr; return 1; }
	sudo mkdir -p binary/{live,isolinux}
	act "Copyring kernel and initrd"
	sudo cp "$strapdir/boot/"vmlinuz* binary/live/vmlinuz || { zerr; return 1; }
	sudo cp "$strapdir/boot/"initrd*  binary/live/initrd.img || { zerr; return 1; }
	#sudo cp "$strapdir/boot/memtest86+.bin binary/live/memtest || { zerr; return 1; }

	sudo cp "$R/extra/syslinux/isolinux.bin" binary/isolinux || { zerr; return 1; }
	sudo cp "$R"/extra/syslinux/*.c32 binary/isolinux || { zerr; return 1; }
	popd
}

iso_write_isolinux_cfg()
{
	fn iso_write_isolinux_cfg
	req=(workdir arch os)
	ckreq || return 1

	notice "Writing isolinux configuration"

	cat <<EOF | sudo tee "$workdir/binary/isolinux/isolinux.cfg" >/dev/null
ui vesamenu.c32
prompt 0
menu title ${os} boot menu
timeout 300

label live-${arch}
menu label ^${os} live (${arch})
menu default
linux /live/vmlinuz
append initrd=/live/initrd.img boot=live

endtext
EOF
}

iso_squash_strap()
{
	fn iso_squash_strap
	req=(workdir strapdir)
	ckreq || return 1

	notice "Creating squashfs out of strapdir"

	case "$arch" in
	amd64|i386)
		_compfilt="-Xbcj x86"
		;;
	arm*)
		_compfilt="-Xbcj arm"
		;;
	*)
		_compfilt=""
		;;
	esac

	pushd "$workdir" || { zerr; return 1; }
	sudo mksquashfs "$strapdir" binary/live/filesystem.squashfs \
		-comp xz ${=_compfilt} -noappend || { zerr; return 1; }
	popd
}

iso_xorriso_build()
{
	fn iso_xorriso_build
	req=(workdir image_name)
	ckreq || return 1

	notice "Building iso..."
	isoname="${image_name}-live.iso"

	if [[ -n "$MKEFI" ]]; then
		uefi_opt="-eltorito-alt-boot -e boot/grub/efiboot.img -isohybrid-gpt-basdat -no-emul-boot"
	fi

	isohybrid="$R/extra/syslinux/isohdpfx.bin"

	mkdir -p "$R/dist"
	pushd "$workdir"
	sudo xorriso -as mkisofs -r -J -joliet-long -l \
		-isohybrid-mbr "$isohybrid" \
		-partition_offset 16 \
		-A "${os} Live - ${arch}" \
		-b isolinux/isolinux.bin \
		-c isolinux/boot.cat \
		-no-emul-boot \
		-boot-load-size 4 \
		-boot-info-table \
		${=uefi_opt} \
		-o "$R/dist/$isoname" \
		binary || { zerr; return 1; }
	popd

	act "Calculating sha256 checksums"
	pushd "$R/dist" || { zerr; return 1; }
	sha256sum "$isoname" > "${isoname}.sha256"
	popd

	if [[ "$DEBUG" = 1 ]]; then
		return
	fi

	sudo rm -rf "$workdir"

	notice "Done! Thanks for being patient!"
}
