#!/bin/sh

set -e

. /usr/share/debconf/confmodule

db_fget icinga2-classicui/adminpassword seen || true
password_seen="$RET"

if [ "$1" = "reconfigure" ]; then
    password_seen=false
fi

if [ "$password_seen" != "true" ]; then
	# the passwords are reset at the end of every postinst run,
	# however, they are left in the "seen" state so that the
	# admin is not prompted for them at every upgrade.
	while [ ! "$passwordsmatch" ]; do
		#only ask for a password if no htpasswd exists
		db_input high icinga2-classicui/adminpassword || true
		db_input high icinga2-classicui/adminpassword-repeat || true
		db_go || true
		db_get icinga2-classicui/adminpassword
		p1="$RET"
		db_get icinga2-classicui/adminpassword-repeat
		p2="$RET"
		if [ "$p1" = "$p2" ]; then
			passwordsmatch="yes"
		else
			db_fset icinga2-classicui/adminpassword seen false
			db_fset icinga2-classicui/adminpassword-repeat seen false
			db_fset icinga2-classicui/adminpassword-mismatch seen false
			db_input critical icinga2-classicui/adminpassword-mismatch || true
		fi
	done
fi
db_go || true
