#!/usr/bin/bash

set -e

PROGNAME=$(basename "$0")

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

grommunio_pkg_usage () {
	local BLAH='
  $PROGNAME: [-abhCD] <-i|-r|-e|-s> [-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  --no-clean-db       Don't clean DB installed by package.
         -D  --no-def-priority   Show all questions, not just default debconf
                                 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
         -l  --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_option_usage () {
	local BLAH='
  $PROGNAME: [-abhD] [-f frontend] [-o operation] [-s pkg-selection] [-p priority]

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

grommunio_option_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.
         -D  --no-def-priority    Show all questions, not just default debconf
                                  priority or higher
         -f  --frontend           select debconf frontend to use
         -h  --help               print this help
         -o  --option-operation   operation - install, reconfigure, or erase
         -p  --priority           show questions of this priority or higher
         -s  --option-select      option packages 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)

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

grommunio_setup_options__usage () {
	local BLAH='
  $PROGNAME: [-abhD] [-f frontend] [-s pkg-selection] [-p priority]

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

grommunio_setup_options_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.
         -D  --no-def-priority    Show all questions, not just default debconf
                                  priority or higher
         -f  --frontend           select debconf frontend to use
         -h  --help               print this help
         -p  --priority           show questions of this priority or higher
         -s  --option-select      option packages 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)

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

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

option_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-pkg-bits/option-management-description seen "$SEEN_STATE"
	db_reset grommunio-pkg-bits/option-operation
	db_fset grommunio-pkg-bits/option-operation seen "$SEEN_STATE"
	db_reset grommunio-pkg-bits/option-select
	db_fset grommunio-pkg-bits/option-select seen "$SEEN_STATE"
	db_reset grommunio-pkg-bits/option-erase
	db_fset grommunio-pkg-bits/option-erase seen "$SEEN_STATE"
}

option_preseed () {
	if [ -n "$GSETUP_OPT_OPERATION" -o -n "$GSETUP_OPT_SELECT" ]; then
		option_debconf_reset 'true'
	else
		option_debconf_reset 'false'
	fi
	if [ -n "$GSETUP_OPT_OPERATION" ]; then
		db_set grommunio-auth/option-operation "$GSETUP_OPT_OPERATION"
		if [ "$GSETUP_OPT_OPERATION" = 'erase' ]; then
			db_set grommunio-pkg-bits/option-erase true
		fi
		unset GSETUP_OPT_OPERATION
	fi

	if [ -n "$GSETUP_OPT_SELECT" ]; then
		local OPT_SELECT=$(echo "$GSETUP_OPT_SELECT" | perl -pe 's/\b /, /g')
		db_set grommunio-pkg-bits/option-select "$OPT_SELECT"
		unset GSETUP_OPT_SELECT
	fi
	return 0
}

setup_options_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_reset grommunio-pkg-bits/setup-options-select
	db_fset grommunio-pkg-bits/setup-options-select seen "$SEEN_STATE"
}

setup_options_preseed () {
	if [ -n "$GSETUP_OPT_SELECT" ]; then
		setup_options_debconf_reset 'true'
	else
		setup_options_debconf_reset 'false'
	fi

	if [ -n "$GSETUP_OPT_SELECT" ]; then
		local OPT_SELECT=$(echo "$GSETUP_OPT_SELECT" | perl -pe 's/\b /, /g')
		db_set grommunio-pkg-bits/setup-options-select "$OPT_SELECT"
		unset GSETUP_OPT_SELECT
	fi
	return 0
}

option_manage () {
	[ ! "${DEBIAN_HAS_FRONTEND:-}" ] && \
		. /usr/share/grommunio-pkg-bits/scripts/debconf-communicate.sh
	local OPT_OPERATION=''
	local OPT_SELECT=''
	local OPT_DO_ERASE='false'
	case "$CMD_MODE" in
		''|option)
			db_get grommunio-pkg-bits/option-operation
			OPT_OPERATION="$RET"
			db_get grommunio-pkg-bits/option-select
			OPT_SELECT="$RET"
			db_get grommunio-pkg-bits/option-erase
			OPT_DO_ERASE="$RET"
			if [ "$OPT_OPERATION" = 'erase' ] && [ "$OPT_DO_ERASE" != 'true' ]; then
				echo "${PROGNAME}: exiting as erase operation not confirmed." 1>&2
				exit 4
			fi
			;;
		setup_options)
			OPT_OPERATION="install"
			db_get grommunio-pkg-bits/setup-options-select
			local OPT_SELECT="$RET"
			;;
		*)
			echo "${PROGNAME}: Internal error! - CMD_MODE processing."
			exit 3
			;;
	esac
	OPT_SELECT=$(echo "$OPT_SELECT" | perl -pe 's/,//')
	# reset dialog questions back to defaults - done here so that
	# debconf choices can be pre-seeded
	case "$CMD_MODE" in
		''|option)
			option_debconf_reset
			;;
		setup_options)
			setup_options_reset
			;;
	esac
	[ ! "${DEBIAN_HAS_FRONTEND:-}" ] && db_stop

	# Stuff to do things goes here
	# Form package name lists from menu items - work around package naming quirks
	local OS_FAMILY=$(get_distro_family)
	local PKG_SELECT=''
	for OPT in ${OPT_SELECT}; do
		case "$OS_FAMILY" in
			rhel|suse)
				case "$OPT" in
					grommunio-chat)
						PKG_SELECT="${PKG_SELECT} grommunio-chat-v10"
						;;
					grommunio-keycloak)
						PKG_SELECT="${PKG_SELECT} grommunio-auth-multi grommunio-keycloak"
						;;
					*)
						PKG_SELECT="${PKG_SELECT} ${OPT}"
						;;
				esac
				;;
			debian)
				case "$OPT" in
					grommunio-chat)
						PKG_SELECT="${PKG_SELECT} grommunio-chat-v10"
						;;
					grommunio-keycloak)
						PKG_SELECT="${PKG_SELECT} grommunio-auth grommunio-keycloak grommunio-keycloak-grommuniodb"
						;;
					*)
						PKG_SELECT="${PKG_SELECT} ${OPT}"
						;;
				esac
				;;
		esac
	done

	# Workout operation
	case "$OPT_OPERATION" in
		install)
			install_packages ${PKG_SELECT}
			;;
		reconfigure)
			reconfigure_packages ${PKG_SELECT}
			;;
		erase)
			erase_packages ${PKG_SELECT}
			;;
	esac
}

case "$PROGNAME" in
	grommunio-pkg)
		check_if_root
		CMD_MODE='pkg' CMD_SHORT_ARGS='abCDehirf:p:l::' \
			CMD_LONG_ARGS='admin-password,no-clean-db,db-password,no-def-priority,erase,frontend:,help,install,priority:,reconfigure,setup-last::' \
			grommunio_cmd_args "$@"
		# This preseeds debconf with argument values from above
		do_debconf_config -x grommunio-pkg-bits
		grommunio_pkg ${GSETUP_PKG_PACKAGES}
		;;
	grommunio-option-manage)
		check_if_root
		CMD_MODE='option' CMD_SHORT_ARGS='abDhf:o:p:s:' \
			CMD_LONG_ARGS='admin-password,db-password,no-def-priority,frontend:,help,option-operation:,option-select:,priority:' \
			grommunio_cmd_args "$@"
		do_debconf_config -x grommunio-pkg-bits -c /usr/share/grommunio-pkg-bits/scripts/grommunio-option-manage.config -p option_preseed -s /usr/sbin/grommunio-option-manage
		option_manage
		;;
	grommunio-setup-options)
		check_if_root
		CMD_MODE='setup_options' CMD_SHORT_ARGS='abDhf:p:s:' \
			CMD_LONG_ARGS='admin-password,db-password,no-def-priority,frontend:,help,,option-select:,priority:' \
			grommunio_cmd_args "$@"
		do_debconf_config -x grommunio-pkg-bits -c /usr/share/grommunio-pkg-bits/scripts/grommunio-setup-options.config -p setup_options_preseed -s /usr/sbin/grommunio-setup-options
		CMD_MODE='setup_options' option_manage
		;;
esac

