#!/bin/bash

set -e

PROGNAME=$(basename "$0")

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


grommunio_pkg_usage () {
	local BLAH='
  $PROGNAME: [-abcdh] <-i|-r|-e> [-f frontend] [-p priority] <package> [package package...]

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

grommunio_pkg_vusage () {
	local BLAH="
         -a  --admin-password    Prompt for admin password via /dev/tty
                                 Set GSETUP_ADMIN_PASSWORD to bypass this.
         -b  --db-password       Prompt for grommunio DB password via /dev/tty
                                 Set GSETUP_DB_PASSWORD to bypass this.
         -c  --clean-db          Clean DB installed by package.
         -d  --default-priority  show questions of default debconf question priority or higher
         -e  --erase             erase packages  
         -f  --frontend          select debconf frontend to use
         -h  --help              print this help
	 -i  --install           install packages
         -p  --priority          show questions of this priority or higher
	 -r  --reconfigure       reconfigure packages
	 -s  --setup-last        set up for last (default 30) minutes

         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)

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

grommunio_pkg_args () {
	local DB_FRONTEND=''
	local DB_PRIORITY=''
	local DB_DEF_PRIORITY=0
	local OPTS=''
	local PROMPT_ADMIN_PASSWORD=0
	local PROMPT_DB_PASSWORD=0
	local PKG_OP_INSTALL=0
	local PKG_OP_RECONFIGURE=0
	local PKG_OP_ERASE=0
	export GSETUP_PKG_CLEAN_DB='true'
	export GSETUP_SETUP_LAST=''
	local PKG_OP_SETUP_LAST=0

	OPTS=$(getopt -o abCdehirf:p:s:: -l admin-password,no-clean-db,db-password,default-priority,erase,frontend:,help,install,priority:,reconfigure,setup-last:: -n "$PROGNAME" -- "$@")
	if [ $? -ne 0 ]; then
		#echo "${PROGNAME}: Failed to parse options." 1>&2
		grommunio_pkg_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
			;;
		-b | --db-password )
			PROMPT_DB_PASSWORD=1
			shift
			;;
		-C | --no-clean-db )
			GSETUP_PKG_CLEAN_DB='false'
			shift
			;;
		-d | --default-priority)
			DB_DEF_PRIORITY=1
			shift
			;;
		-e | --erase )
			PKG_OP_ERASE=1
			shift
			;;
		-f | --frontend)
			DB_FRONTEND="$2"
			shift 2
			;;
		-h | --help)
			shift
			grommunio_pkg_vusage
			exit 2
			;;
		-i | --install )
			PKG_OP_INSTALL=1
			shift
			;;
		-p | --priority)
			DB_PRIORITY="$2"
			shift 2
			;;
		-r | --reconfigure )
			PKG_OP_RECONFIGURE=1
			shift
			;;
		-s | --setup-last)
			PKG_OP_SETUP_LAST=1
			case "$2" in
				'')
					GSETUP_SETUP_LAST='30'
					;;
				*)
					GSETUP_SETUP_LAST="$2"
					;;
			esac
			shift 2
			;;
		--)
			shift
			break
			;;
		*)
			echo "${PROGNAME}: Internal error!"
			exit 2
			;;
		esac
	done

	if [ $PKG_OP_SETUP_LAST -eq 0 -a  $# -eq 0 ]; then
		grommunio_pkg_vusage
		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

	local O_COUNT=0
	[ $PKG_OP_INSTALL -eq 1 ] && O_COUNT=$(( O_COUNT++  ))
	[ $PKG_OP_RECONFIGURE -eq 1 ] && O_COUNT=$(( O_COUNT++ ))
	[ $PKG_OP_ERASE -eq 1 ] && O_COUNT=$(( O_COUNT++ ))
	[ $PKG_OP_SETUP_LAST -eq 1 ] && O_COUNT=$(( O_COUNT++ ))
	if [ $O_COUNT -gt 1 ]; then
		echo "${PROGNAME}: only one of --install, --reconfigure, --erase, --setup-last may be given."
		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

	# 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 [ $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

	if [ $PROMPT_DB_PASSWORD -eq 1 ]; then
		GSETUP_DB_PASSWORD=''
		read_password GSETUP_DB_PASSWORD 'Enter grommunio DB Password'
		[ -n "$GSETUP_DB_PASSWORD" ] && export GSETUP_DB_PASSWORD
	fi

	export GSETUP_PKG_OPERATION=''
	[ $PKG_OP_INSTALL -eq 1 ] && GSETUP_PKG_OPERATION='install'
	[ $PKG_OP_RECONFIGURE -eq 1 ] && GSETUP_PKG_OPERATION='reconfigure'
	[ $PKG_OP_ERASE -eq 1 ] && GSETUP_PKG_OPERATION='erase'
	[ $PKG_OP_SETUP_LAST -eq 1 ] && GSETUP_PKG_OPERATION='setup-last'

	export GSETUP_PKG_PACKAGES="$@"

	return 0
}

grommunio_pkg () {
	case "$GSETUP_PKG_OPERATION" in
		install)
			install_packages "$@"
			;;
		reconfigure)
			reconfigure_packages "$@"
			;;
		erase|remove)
			erase_packages "$@"
			;;
		setup-last)
			setup_last_packages "$@"
			;;
	esac
}

case "$PROGNAME" in 
	grommunio-pkg)
		check_if_root
		grommunio_pkg_args "$@"
		# This preseeds debconf with argument values from above
		do_debconf_config -x grommunio-pkg-bits
		grommunio_pkg ${GSETUP_PKG_PACKAGES}
		;;
esac

