#!/bin/sh
# gws interface
path="${0%/*}/gwconf-files"
##############################################
. "${path}/gwconf-common"
filterPath="${path}/filters/"
##############################################

die () {
	echo >&2 "$@"
	exit 1
}

usage () {
	cat "${path}/gwconf-help"
	exit 1
}

setSourcePath() {
	# $1: file path or ID
	# Set path and do minimal tests
	sourceInput="${1}"
	sourcePath="$(getConfPath "${sourceInput}" | cpSourceFilter)"
	# tests
	[ ! -z "${1}" ] && [ -z "${sourcePath}" ] && die "source path is bad or not allowed (${sourceInput})"
	[ -f "${sourcePath}" ]                  || die "source file does not exist"
}

setTargetPath() {
	# $1: file path or ID
	# Set path and do minimal tests
	targetInput="${1}"
	targetPath="$(getConfPath "${targetInput}" | cpTargetFilter)"
	# tests
	[ ! -z "${1}" ] && [ -z "${targetPath}" ] && die "target path is bad or not allowed (${targetInput})"
}

revertOnLockout() {
	# In case user does not reconnect.
	# Create hard link to current setup.
	sudo "${path}/gwconf-ln" "${current_setup_file}" "${revert_setup_file}"
}

isUserConf() {
	[ -r "${sourcePath}" ]
}

getSudo() {
	sudo -v || exit 1
}

doApply() {
	setSourcePath "${sourceInput}"
	setTargetPath 'new' # target is always new
	if isUserConf; then
		# TODO: check if config changes anything (to avoid reboots)
		#       compare current with new
		extractAndTestTarXz "${sourcePath}" || die 'Test failed'
	fi
	getSudo
	gw_log_verbose "cp ${sourceInput} ${targetInput}"
	gw_log_verbose "cp ${sourcePath} ${targetPath}"
	# copy file
	sudo "${path}/gwconf-cp" "${sourcePath}" "${targetPath}" && \
	#$doRevert && \
	sudo "${path}/gwconf-touch" "reset_1"
}

doCopy() {
	setSourcePath "${sourceInput:=current}"
	setTargetPath "${targetInput:=setup.tar.xz}"
	getSudo
	gw_log_verbose "cp ${sourceInput} ${targetInput}"
	gw_log_verbose "cp ${sourcePath} ${targetPath}"
	# copy file
	sudo "${path}/gwconf-cp" "${sourcePath}" "${targetPath}"
}

doGWSInfo() {
	setSourcePath "${sourceInput:=current}"
	sudo "${path}/gwconf-info" "${sourcePath}"
}

doTest() {
	gw_doTest=true
	setSourcePath "${sourceInput:=current}"
	extractAndTestTarXz "${sourcePath}"
}

doTouch() {
	sudo "${path}/gwconf-touch" "/var/local/telem/${1}"
}

listAllConfigs() {
	sudo "${path}/gwconf-list" --all
}

listOldConfigs() {
	sudo "${path}/gwconf-list" --old
}

setvar() {
	"${path}/gwconf-conf" "$@" \
	|| die "Failed"
}

##############################################

doRevert=true
sourceInput=
targetInput=
sourcePath=
targetPath=
# by default list configs
run=listAllConfigs
while : ; do
	case "$1" in
		-a|--a|--apply)
			sourceInput='setup.tar.xz'
			run=doApply
		;;
		--clear-setup|--reset-3)
			#doTouch "clear_setup"
			sourceInput='default'
			run=doApply
		;;
		-c|--cp|--copy)
			run=doCopy
		;;
		--gws-info)
			run=doGWSInfo
		;;
		--help)
			usage
		;;
		--list)
			listAllConfigs; exit
		;;
		--list-old)
			listOldConfigs; exit
		;;
		--reboot)
			doTouch "reboot"; exit
		;;
		--reload)
			sourceInput='current'
			run=doApply
		;;
		--reset-1)
			doTouch "reset_1"; exit
		;;
		--reset-2)
			doTouch "reset_2"; exit
		;;
		-r|--r|--revert-on-lockout)
			doRevert=revertOnLockout
		;;
		-t|--t|--test)
			run=doTest
		;;
		--test-enable)
			setvar 'gw_doTest' 'true'; exit
		;;
		--test-disable)
			setvar 'gw_doTest' 'false'; exit
		;;
		-v|--v|--verbose)
			gw_verbose=true
		;;
		--)
			shift
			break
		;;
		-*)
			usage
		;;
		*)
			break
		;;
	esac
	shift
done

[ ! -z "${1}" ] && sourceInput="${1}"; shift
[ ! -z "${1}" ] && targetInput="${1}"; shift
# we have remaining unknown arguments
[ ! -z "${1}" ] && usage

$run
