#!/usr/bin/bash

function pecho() {
	if [[ "$1" = "debug" ]] && [[ "${PORTABLE_LOGGING}" = "debug" ]]; then
		echo "[Debug]		$2"
	elif [[ "$1" = "info" ]] && [[ ! "${PORTABLE_LOGGING}" = "warn" ]]; then
		echo "[Info]		$2"
	elif [[ "$1" = "warn" ]]; then
		echo "[Warn]		$2"
	elif [[ "$1" = "crit" ]]; then
		echo "[Critical]	$2"
	fi
}

function printHelp() {
	echo "This is Portable Pools, a tool to create and enter transient sandboxes"
	echo "Visit https://github.com/Kraftland/portable for documentation and information."
	echo "Supported arguments:"
	echo "	-v	-	-	-	-> Verbose output (optional)"
	echo "	--game	-	-	-	-> Enable game mode for discrete GPU utilisation"
	echo "	[container name]	-	-> Specifies the name for sandbox"

	exit 0
}

function cmdlineDispatcher() {
	declare -i argumentCount=0
	while true; do
		if [[ $1 = "--quit" ]]; then
			declare -g -i -r termApp=1
		elif [[ $1 = "-v" ]]; then
			declare -g -r -x PORTABLE_LOGGING=debug
			pecho debug "Enabled verbose logging"
		elif [[ $1 = "--help" ]]; then
			pecho debug "Printing help on explicit request"
			printHelp
		elif [[ $1 = "--game" ]]; then
			pecho debug "Enabled game mode"
			declare -g -x -r gameMode=true
		elif [[ -z $* ]]; then
			declare trailS="s"
			if [[ "${argumentCount}" -eq 1 ]]; then
				unset trailS
			fi
			pecho debug "Resolution of command line arguments finished with ${argumentCount} argument${trailS}."
			if [[ "${argumentCount}" -eq 0 ]]; then
				printHelp
			fi
			break
		else
			if [[ ! "${sandboxName}" ]]; then
				declare -g sandboxName
				sandboxName=$1
				pecho info "Interpreted sandbox name: $1"
			else
				pecho warn "Unrecognized option: $1"
			fi
		fi
		argumentCount+=1
		shift
	done

	if [[ ! "${sandboxName}" ]]; then
		pecho crit "Sandbox name undefined!"
	fi
}

function sourceXDG() {
	if [[ ! ${XDG_CONFIG_HOME} ]]; then
		export XDG_CONFIG_HOME="${HOME}"/.config
		pecho debug "Guessing XDG Config Home @ ${XDG_CONFIG_HOME}"
	else
		source "${XDG_CONFIG_HOME}"/user-dirs.dirs
		pecho debug "XDG Config Home defined @ ${XDG_CONFIG_HOME}"
	fi
	if [[ ! ${XDG_DATA_HOME} ]]; then
		export XDG_DATA_HOME="${HOME}"/.local/share
	fi
	export XDG_DOCUMENTS_DIR="$(xdg-user-dir DOCUMENTS)"
}

function initialStart() {
	sourceXDG
	if [ ! -d "${XDG_CONFIG_HOME}"/portable-pools ]; then
		pecho info "Welcome to portable-pools"
		mkdir -p "${XDG_CONFIG_HOME}"/portable-pools
	fi
}

function generatePortableInfo() {
	if [ -f "${XDG_CONFIG_HOME}"/portable-pools/"${sandboxName}"/config  ]; then
		pecho debug "Skipping generation"
		return 0
	fi
	pecho debug "Starting generation..."
	mkdir -p "${XDG_CONFIG_HOME}"/portable-pools/"${sandboxName}"
	echo '''#!/usr/bin/bash
appID="top.kimiblock.placeHolder"
friendlyName="placeHolder"
stateDirectory="placeHolder_Data"
launchTarget="/usr/bin/bash"
waylandOnly="adaptive"
''' >"${XDG_CONFIG_HOME}"/portable-pools/"${sandboxName}"/config
	sed -i "s|placeHolder|$1|g" "${XDG_CONFIG_HOME}"/portable-pools/"${sandboxName}"/config
	pecho debug "Generation Done!"
}

function invokePortable() {
	export \
		_portableConfig="${XDG_CONFIG_HOME}"/portable-pools/"${sandboxName}"/config
	export gameMode
	if [[ "${termApp}" -eq 1 ]]; then
		 portable \
		 	--actions quit
	else
		portable \
			--actions debug-shell
	fi
}

initialStart $@
cmdlineDispatcher $@
generatePortableInfo $@
invokePortable $@