#!/bin/bash

set -e

PROGNAME=$(basename "$0")

. /usr/share/grommunio-pkg-bits/scripts/setup-include.sh

auth_manage_usage () {
	local BLAH="
  $PROGNAME: [-adhR] [-f frontend] [-o setup|delete] [-p priority] [-s client1,client2,... ]

"
	echo -n "$BLAH" 1>&2
}

auth_manage_vusage () {
	local BLAH="
	 -a  --admin-password    Prompt for admin password via /dev/tty
                                 Set GSETUP_ADMIN_PASSWORD to bypass this.
         -d  --default-priority  show questions of default debconf question priority or higher
         -f  --frontend          select debconf frontend to use
         -h  --help              display this help
         -o  --client-operation  operation - 'setup' or 'delete'
         -p  --priority          show questions of this priority or higher
         -R  --no-reset          don't reset g-auth-manage debconf settings
         -s  --client-select     clients to be operated on, comma separated

         Debconf frontends available: 'dialog', 'readline', 'noninteractive',
                                      'gnome', 'kde', 'editor', 'web'
         Question priorities: 'criticial', 'high', 'medium' 'low'
         If no question priority is selected all questions are shown (ie
         priority 'low' or higher)
         Client operation is either 'setup' or 'delete'
         Client select example is 'grommunio-web,grommunio-chat,grommunio-meet'
         etc

"
	auth_manage_usage
	echo -n "$BLAH" 1>&2
}

auth_manage_args () {
	local DB_FRONTEND=''
	local DB_PRIORITY=''
	local DB_DEF_PRIORITY=0
	local OPTS=''
	local CLIENT_OPERATION=''
	local CLIENT_SELECT=''
	local PROMPT_ADMIN_PASSWORD=0
	export GSETUP_AUTH_DEBCONF_NO_RESET='false'

	OPTS=$(getopt -o adhRf:o:p:s: -l admin-password,client-operation:,client-select:,default-priority,frontend:,help,priority:,no-reset -n "$PROGNAME" -- "$@")
	if [ $? -ne 0 ]; then
		#echo "${PROGNAME}: Failed to parse options." 1>&2
		auth_manage_usage
		exit 2
	fi
	# Reset the positional parameters to the parsed options
	eval set -- "$OPTS"

	while true; do
		case "$1" in
		-a | --admin-password )
			PROMPT_ADMIN_PASSWORD=1
			shift
			;;
		-d | --default-priority)
			DB_DEF_PRIORITY=1
			shift
			;;
		-h | --help)
			shift
			auth_manage_vusage
			exit 2
			;;
		-f | --frontend)
			DB_FRONTEND="$2"
			shift 2
			;;
		-o | --client-operation)
			CLIENT_OPERATION="$2"
			shift 2
			;;
		-p | --priority)
			DB_PRIORITY="$2"
			shift 2
			;;
		-R | --no-reset)
			GSETUP_AUTH_DEBCONF_NO_RESET='true'
			shift
			;;
		-s | --client-select)
			CLIENT_SELECT="$2"
			shift 2
			;;
		--)
			shift
			break
			;;
		*)
			echo "${PROGNAME}: Internal error!"
			exit 2
			;;
		esac
	done

	if [ $# -ne 0 ]; then
		auth_manage_usage
		exit 2
	fi

	# Check arguments
	if [ $DB_DEF_PRIORITY -eq 1 ] && [ -n "$DB_PRIORITY" ]; then
		echo "${PROGNAME}: you can not use -d and -p together." 1>&2
		exit 2
	fi

	case "$DB_PRIORITY" in
		''|critical|high|medium|low)
			;;
		*)
			echo "${PROGNAME}: debconf priority can only be one of 'critical', 'high', 'medium', or 'low'." 1>&2
			exit 2
			;;
	esac

	case "$DB_FRONTEND" in
		''|dialog|readline|noninteractive|gnome|kde|editor|web)
			;;
		*)
			echo "${PROGNAME}: debconf priority can only be one of 'critical', 'high', 'medium', or 'low'." 1>&2
			exit 2
			;;
	esac

	case "$CLIENT_OPERATION" in
		''|setup|delete)
			;;
		*)
			echo "${PROGNAME}: client operation can only be one of 'delete' or 'setup'." 1>&2
			exit 2
			;;
	esac

	CLIENT_SELECT=$(echo "$CLIENT_SELECT"| perl -pe 's/,/ /g')
	for CLIENT in ${CLIENT_SELECT}; do
		if ! echo "$CLIENT" | grep -qP "$CLIENT_REGEXP"; then
			echo "${PROGNAME}: bad client name '$CLIENT' - can only be  one of ${CLIENT_LIST}."
			exit 2
		fi
	done

	# All GSETUP_ vars need to be exported because invoking the debconf frontend
	# will reexec the script, and they probably won't survive

	if [ $DB_DEF_PRIORITY -eq 1 ]; then
		export GSETUP_PRIORITY='default'
	fi

	if [ -n "$DB_PRIORITY" ]; then
		export GSETUP_PRIORITY="$DB_PRIORITY"
	fi

	if [ -n "$DB_FRONTEND" ]; then
		export GSETUP_FRONTEND="$DB_FRONTEND"
	fi

	if [ -n "$CLIENT_OPERATION" ]; then
		export GSETUP_CLIENT_OPERATION="$CLIENT_OPERATION"
	fi

	if [ -n "$CLIENT_SELECT" ]; then
		export GSETUP_CLIENT_SELECT="$CLIENT_SELECT"
	fi

	if [ $PROMPT_ADMIN_PASSWORD -eq 1 ]; then
		GSETUP_ADMIN_PASSWORD=''
		read_password GSETUP_ADMIN_PASSWORD 'Enter Admin Password'
		[ -n "$GSETUP_ADMIN_PASSWORD" ] && export GSETUP_ADMIN_PASSWORD
	fi
	return 0
}

auth_debconf_reset () {
	local SEEN_STATE='false'
	[ -n "$1" ] && SEEN_STATE="$1"
	# reset dialog questions back to defaults - done here so that
	# debconf choices can be pre-seeded
	db_fset grommunio-auth/management-description seen "$SEEN_STATE"
	db_reset grommunio-auth/client-operation
	db_fset grommunio-auth/client-operation seen "$SEEN_STATE"
	db_reset grommunio-auth/client-select
	db_fset grommunio-auth/client-select seen "$SEEN_STATE"
}

auth_preseed () {
	if [ -n "$GSETUP_CLIENT_OPERATION" -o -n "$GSETUP_CLIENT_SELECT" ]; then
		auth_debconf_reset 'true'
	elif [ "$GSETUP_AUTH_DEBCONF_NO_RESET" != 'true' ]; then
		auth_debconf_reset 'false'
	fi
	if [ -n "$GSETUP_CLIENT_OPERATION" ]; then
		db_set grommunio-auth/client-operation "$GSETUP_CLIENT_OPERATION"
		unset GSETUP_CLIENT_OPERATION
	fi

	if [ -n "$GSETUP_CLIENT_SELECT" ]; then
		local CLIENT_SELECT=$(echo "$GSETUP_CLIENT_SELECT" | perl -pe 's/\b /, /g')
		db_set grommunio-auth/client-select "$GSETUP_CLIENT_SELECT"
		unset GSETUP_CLIENT_SELECT
	fi
	return 0
}

auth_manage () {

	[ ! "${DEBIAN_HAS_FRONTEND:-}" ] && \
		. /usr/share/grommunio-pkg-bits/scripts/debconf-communicate.sh
	db_get grommunio-auth/client-operation
	CLIENT_OPERATION="$RET"
	db_get grommunio-auth/client-select
	CLIENT_SELECT="$RET"
	# reset dialog questions back to defaults - done here so that
	# debconf choices can be pre-seeded
	auth_debconf_reset
	[ ! "${DEBIAN_HAS_FRONTEND:-}" ] && db_stop

	local CMD_PREFIX='/usr/share/grommunio-pkg-bits/scripts'
	case "$CLIENT_OPERATION" in
		setup)
			CMD_PREFIX="${CMD_PREFIX}/setup-gk-client-"
			;;
		delete)
			CMD_PREFIX="${CMD_PREFIX}/delete-gk-client-"
			;;
		*)
			echo "${PROGNAME}: invalid operation '$CLIENT_OPERATION'- software failure!"
			exit 199
			;;
	esac

	local CLIENT=''
	local SUFFIX=''
	for CLIENT in ${CLIENT_SELECT}; do
		SUFFIX=$(echo "$CLIENT" | perl -pe 's/^.*-([^-,]+),{0,1}$/\1/')
		"${CMD_PREFIX}${SUFFIX}"
	done

	return 0
}

case "$PROGNAME" in 
	grommunio-auth-manage)
		check_if_root
		auth_manage_args "$@"
		do_debconf_config -x grommunio-auth -c /usr/share/grommunio-auth/scripts/grommunio-auth-manage.config -p auth_preseed -s /usr/sbin/grommunio-auth-manage
		auth_manage
		;;
esac
