#!/bin/sh

set -e
#set -x


set_igestis_password()
{

while [ ! "${ROOT_PW1}" = "${ROOT_PW2}" -o -z "${ROOT_PW1}" ] ; do
	db_input high igestis/password1 || true
	db_go

	db_input high igestis/password2 || true
	db_go

	db_get igestis/password1
	ROOT_PW1="${RET}"

	db_get igestis/password2
	ROOT_PW2="${RET}"

	if [ ! "${ROOT_PW1}" = "${ROOT_PW2}" ] ; then
		db_input high igestis/password/mismatch || true
		db_go

		db_reset igestis/password1
		db_reset igestis/password2
	elif [ -z "${ROOT_PW1}" ] ; then
		db_input high igestis/password/empty || true
		db_go

		db_reset igestis/password1
		db_reset igestis/password2
	fi
done

}

init_authentication_info()
{

db_set igestis/directory/base "$(hostname -d)"		

if [ "${AUTHENTICATION}" = "Samba4" ] || [ "${AUTHENTICATION}" = "Active Directory" ] ; then
	db_set igestis/username "administrator"
fi

if [ "${AUTHENTICATION}" = "Samba4" ] ; then
	db_set igestis/directory/uris "ldapi://%2Fvar%2Flib%2Fsamba%2Fprivate%2Fldapi"
fi



}

get_authentication_info()
{

# Get information about the authentication
case "$AUTHENTICATION" in

	"OpenLDAP" | "Active Directory" | "Samba4" )
		
        # LDAP_URIS
		db_input high igestis/directory/uris  || true
		db_go
		db_get igestis/directory/uris
		LDAP_URIS="${RET}"

		## BASE ##
		db_input high igestis/directory/base || true
		db_go
		db_get igestis/directory/base
		LDAP_DOMAIN="${RET}"
		LDAP_BASE="dc=`echo $LDAP_DOMAIN | sed 's/^\.//; s/\./,dc=/g'`"

		## USERNAME ##
		if [ "${AUTHENTICATION}" = "OpenLDAP" ] ; then
			db_set igestis/directory/admin "cn=admin,${LDAP_BASE}"
		else
			db_set igestis/directory/admin "Administrator@${LDAP_DOMAIN}"
		fi
		db_input high igestis/directory/admin || true
		db_go
		db_get igestis/directory/admin
		LDAP_ADMIN="${RET}"

		## PASSWORD ##
		db_input high igestis/directory/password || true
		db_go
		db_get igestis/directory/password
		LDAP_PASS="${RET}"

	;;

esac

}

. /usr/share/debconf/confmodule
if [ -f /usr/share/dbconfig-common/dpkg/config.mysql ]; then
	. /usr/share/dbconfig-common/dpkg/config.mysql 
	dbc_go igestis $@
fi

# New iGestis password assistant for the root account.
if [ "$1" = "configure" ] && ([ -z "$2" ] && [ ! -e "/etc/igestis/ConfigIgestisGlobalVars.php" ] ) || [ "$1" = "reconfigure" ]; then

	# Let user choose his prefered directory.
	db_input high igestis/authentication || true
	db_go

	db_get igestis/authentication
	AUTHENTICATION="${RET}"

	init_authentication_info

	if [ "$AUTHENTICATION" = "Internal" ] || [ "$AUTHENTICATION" = "OpenLDAP" ] ; then
		set_igestis_password
	fi

    # CORE ADMIN ACCOUNT
	db_input high igestis/username || true
	db_go

    ## AUTOCREATE USER ##
    db_input high igestis/directory/import || true
    db_go

	while [ true ] && [ ! "$AUTHENTICATION" = "Internal" ] ; do

		get_authentication_info

		unset ERROR
		if [ "$(which ldapsearch)" ] ; then
		      LDAP_OUTPUT=$(ldapsearch -H "${LDAP_URIS}" -b "${LDAP_BASE}" -cxD "${LDAP_ADMIN}" -w "${LDAP_PASS}" "(none=)" 2>&1 ) || ERROR=true
		      LDAP_OUTPUT=$(echo $LDAP_OUTPUT | head -n 1 )
        fi

		if [ "${ERROR}" = true ] ; then

			db_fset igestis/directory/wrong seen false
			db_subst igestis/directory/wrong error "${LDAP_OUTPUT}"
			db_input high igestis/directory/wrong || true
			db_go
			db_get igestis/directory/wrong
			action="${RET}"
	
			case "$action" in
				"Cancel")
					exit 1
				;;
				"Ignore")
					break
				;;
				"Change settings")
					db_fset igestis/directory/uris seen false
					db_fset igestis/directory/base seen false
					db_fset igestis/directory/admin seen false
					db_fset igestis/directory/password seen false
				;;
			esac

		else

			break

		fi

	done



fi
