#!/bin/bash

set -e

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

DISTRO_FAMILY=$(get_distro_family)

reset_debconf_db_usage () {
	local BLAH="
  $PROGNAME: [-hf] [-p package-name] [question question question...]

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

reset_debconf_db_vusage () {
	local BLAH="
         -f        force reset of debconf
         -h        print this help
         -p        package to reset debconf setting(s)
"
	reset_debconf_db_usage
	echo -n "$BLAH" 1>&2
}

reset_debconf_db () {
	local FORCE=0
	local DEBCONF_PACKAGE='grommunio-pkg-bits'
	local QUESTIONS=''
	OPTIND=1
	while getopts fhp: F; do
		case "$F" in
		h)
			reset_debconf_db_vusage
			exit 2
			;;
		f)
			FORCE=1
			;;
		p)
			DEBCONF_PACKAGE="$OPTARG"
			;;
		\?)
			reset_debconf_db_usage
			exit 2
			;;
		esac
	done
	shift $(( $OPTIND -1 ))

	QUESTIONS="$@"

	local CHECK_PROMPT="Reset debconf all settings for '${DEBCONF_PACKAGE}' package?"
	[ -n "$QUESTIONS" ]  && CHECK_PROMPT="Reset debconf settings for '${DEBCONF_PACKAGE}' package?"
	check_with_user $FORCE "$CHECK_PROMPT"

	[ -z "$QUESTIONS" ] && QUESTIONS=$(debconf-show grommunio-pkg-bits | perl -pe 's#^[* ]+(\S+):.*$#\1#')
	if [ ! -f /usr/share/debconf/confmodule ]; then
		echo "${PROGNAME}: You must have the systems debconf package installed."
		exit 2
	fi
	local CMDS=''
	local Q=''
	for Q in $QUESTIONS; do
		CMDS="${CMDS}RESET ${Q}\nFSET ${Q} seen false\n"
	done
	if local OUTPUT=$(echo -ne "$CMDS" | debconf-communicate "$DEBCONF_PACKAGE" 2>&1 | grep -v '^0'); then
		echo "$OUTPUT" | uniq 1>&2
		exit 1
	fi
	exit 0
}

grommunio_pkg_usage () {
	local BLAH='
  $PROGNAME: [-h] [package package package...]

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

grommunio_pkg_vusage () {
	local BLAH="
         -e  --erase        erase packages  
         -h  --help         print this help
	 -i  --install      install packages
	 -r  --reconfigure  reconfgirue packages
"
	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
	export GSETUP_AUTH_DEBCONF_NO_RESET='false'

	OPTS=$(getopt -o adhRf:p: -l admin-password,default-priority,frontend:,help,priority:,no-reset -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
			;;
		-d | --default-priority)
			DB_DEF_PRIORITY=1
			shift
			;;
		-h | --help)
			shift
			grommunio_pkg_vusage
			exit 2
			;;
		-f | --frontend)
			DB_FRONTEND="$2"
			shift 2
			;;
		-p | --priority)
			DB_PRIORITY="$2"
			shift 2
			;;
		-R | --no-reset)
			GSETUP_AUTH_DEBCONF_NO_RESET='true'
			shift
			;;
		--)
			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

	# 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

}

case "$PROGNAME" in 
	reset-debconf-db)
		check_if_root
		reset_debconf_db "$@"
		;;
	do-debconf-config)
		check_if_root
		if [ -z "$GSETUP_START_DEBCONF_ARGS" ]; then
			echo "${PROGNAME}: GSETUP_START_DEBCONF_ARGS not set - program incorrectly called."
			exit 2
		fi
		start_debconf ${GSETUP_START_DEBCONF_ARGS} "$@"
		exit 0
		;;
	grommunio-pkg)
		check_if_root
		grommunio_pkg_args "$@"
		;;

esac
