#!/bin/bash

#set -x

## remaster-compose
## Copyright (C) 2019-2020 Richard Nelson <unixabg@gmail.com>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.
############################
# Dependencies
############################
if [[ $EUID -ne 0 ]]; then
	echo "This script must be run as root"
	exit 1
fi

if [ ! -e /usr/lib/ISOLINUX/isohdpfx.bin ]; then
	echo "No /usr/lib/ISOLINUX/isohdpfx.bin file found!"
	echo "Exiting ..."
	echo "On Debian based systems, isolinux can be installed with:"
	echo "  apt install isolinux"
	exit 1
fi

if [ ! -e /usr/bin/xorriso ]; then
	echo "No /usr/bin/xorriso file found!"
	echo "Exiting ..."
	echo "On Debian based systems, xorriso can be installed with:"
	echo "  apt install xorriso"
	exit 1
fi

# Source in config
echo "Loading remaster-iso.conf ..."
if [ -f remaster-iso.conf ]; then
	echo "Found ./remaster-iso.conf"
	echo "Sourcing in ./remaster-iso.conf"
	. remaster-iso.conf
elif [ -f /usr/share/remaster-iso/remaster-iso.conf ]; then
	echo "Found /usr/share/remaster-iso/remaster-iso.conf"
	echo "Sourcing in /usr/share/remaster-iso/remaster-iso.conf"
	. /usr/share/remaster-iso/remaster-iso.conf
else
	echo "No remaster-iso.conf file found!"
	echo "Exiting ..."
	exit 1
fi

############################
# Main Code
############################
echo "Conf says remaster-iso version is ${_VER}"

# Set some values for remaster
while [ -n "$1" ]; do # while loop starts
	case "$1" in
	-itn | --iso-target-name)
		shift
		echo "I: --iso-target-name option was passed of $1"
		_ISOTargetName=$1
		;;
	-itt | --iso-target-title)
		shift
		echo "I: --iso-target-title was passed of $1"
		_ISOTargetTitle=$1
		;;
	--usage)
		echo
		echo "################## remaster-compose parameters ##################"
		echo "-itt || --iso-target-title      - set the target iso title"
		echo "-itn || --iso-target-name       - set the target iso name "
		echo "--usage                         - print this information  "
		echo
		exit 0
		;;
	*)
		echo "E: Unrecognized option of $1 !!Stopping here!!"
		exit 1
		;;
	esac
	shift
done

# Sanity check for work area
if [ -d "${_ISOExtractPath}" ]; then
	_BUILDDATE=$(date +%Y%m%d%H%M)
	echo "I: Attempting to compose the ISO: ${_BUILDDATE}-${_ISOTargetName} ..."
	echo "I: change directory to target live folder"
	cd "${_ISOExtractPath}"
	echo "I: creating a MD5SUMS file of iso contents"
	find -type f -print0 | xargs -0 md5sum | grep -v isolinux/boot.cat | tee MD5SUMS
	xorriso -as mkisofs -r -D -V "${_ISOTargetTitle} ${_VER}" -cache-inodes -J -l -iso-level 3 -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin -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 -isohybrid-gpt-basdat  -o "${_BASEDIR}/${_BUILDDATE}-${_ISOTargetName}.iso" .
	echo "I: ... ${_BUILDDATE}-${_ISOTargetName} ISO composition attempt is now complete."
	echo "I: Creating a MD5SUM of the completed iso file ..."
	md5sum "${_BASEDIR}/${_BUILDDATE}-${_ISOTargetName}.iso" > "${_BASEDIR}/${_BUILDDATE}-${_ISOTargetName}.iso.md5sum"
	echo "I: ... iso MD5SUM completed."
	exit 0
else
	echo "E: No iso-extract folder of ${_ISOExtractPath} !"
	echo "Exiting ..."
	exit 1
fi

