#!/bin/bash +H
set +H

main() {
	declare orig_iso="serenelinux_18q4.3.4_beta1.iso"
	declare new_files="${PWD}/iso"
	declare new_iso="${PWD}/SereneLinux_18Q4.3.4_Beta1_Fix.iso"
	declare mbr_template="isohdpfx.bin"
	declare _ERR_CODE=0

	echo "$(tput sgr0)Creating ISO..."
	xorriso -as mkisofs \
		-r -J --joliet-long \
		-V 'SereneLinux_18Q4.3.4_Beta1' \
		-o "${new_iso}" \
		-isohybrid-mbr "${mbr_template}" \
		-partition_offset 16 \
		-c isolinux/boot.cat \
		-b isolinux/isolinux.bin \
		-no-emul-boot -boot-load-size 4 -boot-info-table \
		-eltorito-alt-boot \
		-e 'boot/grub/efi.img' \
		-no-emul-boot \
		-boot-load-size 4672 \
		-isohybrid-gpt-basdat \
		-isohybrid-apm-hfsplus \
		"$new_files" && echo "$(tput bold)$(tput setaf 2)Successfully created ISO!$(tput sgr0)" || { _ERR_CODE=${?}; echo "$(tput bold)$(tput setaf 1)Failed to create ISO!$(tput sgr0)"; exit ${_ERR_CODE}; }

	# ^ Reference: https://wiki.debian.org/RepackBootableISO#What_to_do_if_no_file_.2F.disk.2Fmkisofs_exists
	cd $(dirname "$new_iso")
	echo -n "Calculating MD5 checksum... "
	(md5sum $(basename "$new_iso") > "$new_iso".md5) && echo "$(tput bold)$(tput setaf 2)DONE!$(tput sgr0)" || echo "$(tput bold)$(tput setaf 1)FAILED!$(tput sgr0)"
	echo -n "Calculating SHA256 checksum... "
	(sha256sum $(basename "$new_iso") > "$new_iso".sha256) && echo "$(tput bold)$(tput setaf 2)DONE!$(tput sgr0)" || echo "$(tput bold)$(tput setaf 1)FAILED!$(tput sgr0)"

	# v OUTDATED
	#cd iso && mkisofs -o ../patched.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -J -R -V "SereneLinux_18Q4.3.4_Beta1_Fix" .
}

main ${@}
