#!/bin/bash

## remaster-extract
## 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/bin/rsync ]; then
	echo "No /usr/bin/rsync file found!"
	echo "Exiting ..."
	echo "On Debian based systems, rsync can be installed with:"
	echo "  apt install rsync"
	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
_ISOSentry=0
while [ -n "$1" ]; do # while loop starts
	case "$1" in
	-i | --iso)
		shift
		echo "--iso option was passed of $1"
		_ISOName=$1
		_ISOSentry=1
		;;
	--usage)
		echo
		echo "################## remaster-extract parameters ##################"
		echo "  -i || --iso          - iso to use for extract operation"
		echo
		exit 0
		;;
	*) echo "Option $1 not recognized" ;; # In case you typed a different option other than a,b,c
	esac
	shift
done

if [ "$_ISOSentry" = "1" ]; then
	echo "I: ISO was passed starting ..."
	if [ -f "${_ISOName}" ]; then
		echo "I: Found iso of ${_ISOName}"
		mkdir -p $_ISOMountPoint
		echo "I: Making iso extract path of ${_ISOExtractPath} "
		mkdir -p $_ISOExtractPath
		echo "I: Mounting iso on ${_ISOMountPoint} "
		mount -o loop "${_ISOName}" ${_ISOMountPoint}
		echo "I: Syncing ${_ISOMountPoint} to ${_ISOExtractPath} "
		rsync -av ${_ISOMountPoint}/ ${_ISOExtractPath}/
		echo "I: Unmounting iso ${_ISOMountPoint} "
		umount -l ${_ISOMountPoint}
		echo "I: ISO extract is completed"
		echo "I: You should be ready to run remaster-squashfs-editor"
	else
		echo "No ${_ISOName} file found!"
		echo "Exiting ..."
		exit 1
	fi

else
	echo "E: No ISO name was passed please see --usage."
	exit
fi

exit 0
