#!/bin/sh -e

# Source debconf library.
. /usr/share/debconf/confmodule

check_pg_hba_conf() {
    # Check the format of pg_hba.conf: does it need converting?
    # Uncommented lines used to match
    #  local  [database]   [method]
    # or
    #  host   [database]   [address]  [mask]  [method]
    # and any such must have a [user] field added after [database]
    if grep -qsE '^[ 	]*local[ 	]+[^ 	]*[ 	]+(trust|ident|reject|md5|password|crypt|krb5|pam)' /etc/postgresql/pg_hba.conf
    then
	return 1
    else
	:
    fi
    if grep -qsE '^[ 	]*(host|hostssl)[ 	]+[^ 	]+[ 	]+[^ 	]+[ 	]+[^/ 	]+[ 	]+(trust|ident|reject|md5|password|crypt|krb5|pam)' /etc/postgresql/pg_hba.conf
    then
	return 1
    else
	:
    fi
    return 0
}

#######################################################################
#                       Execution starts here                         #
#######################################################################

if [ "$1" = configure -o "$1" = reconfigure ]
then
    if [ -n "$2" ]
    then
	# this is an upgrade from a previous version or a reinstall
	if dpkg --compare-versions $2 lt 7.2
	then
	    db_input high postgresql/very_old_version_warning || true
	    db_go
	fi
    fi

    # See if pg_hba.conf needs converting
    if ! check_pg_hba_conf
    then
	db_input low postgresql/convert-pg_hba.conf || true
	db_go
    fi

    # on new installs, ask user for database directory $PGDATA
    if [ ! -f /etc/postgresql/postmaster.conf ]
    then
	db_subst postgresql/initdb/location PGDATALOC "${PGDATA:=/var/lib/postgres/data}"
	db_input medium postgresql/initdb/location || true
	db_get postgresql/initdb/location || true
	PGDATA="$RET"
    else
    # we are upgrading from a previous installation, find out PGDATA
        . /etc/postgresql/postmaster.conf
	PGDATA="$POSTGRES_DATA"
    fi
    if [ -z "$PGDATA" ]; then
	PGDATA="/var/lib/postgres/data"
    fi

    db_input medium postgresql/purge_data_too || true
    db_go

    # determine default value of PGLANG
    db_fget postgresql/settings/day_month_order seen || true
    if [ "$RET" = "true" ]; then
	db_get postgresql/settings/locale
	PGLANG="$RET"
    else
	if [ -r "$PGDATA/global/pg_control" -a -x /usr/lib/postgresql/bin/pg_controldata ]
	then
	    PGLANG=`/usr/lib/postgresql/bin/pg_controldata "$PGDATA" | grep LC_COLLATE | awk '{print $2}'`
	else
	    if [ -r /etc/environment ]
	    then
		. /etc/environment
		PGLANG=${LC_COLLATE:-${LC_TYPE:-${LC_ALL:-${LANG}}}}
	    else
		PGLANG=C
	    fi
	fi

	# set default value
	db_set postgresql/settings/locale "$PGLANG"
    fi

    # Available locales
    CHOICES=C
    langs=`locale -a` || true
    if [ -n "$langs" ]
    then
	for lng in $langs
	do
	    if [ "$lng" != C -a "$lng" != POSIX ]; then
		CHOICES="$CHOICES, $lng"
	    fi
	done

	# Ensure that PGLANG is in choices; it might be written differently
	if ! echo "$CHOICES" | grep -Fq "$PGLANG"; then
	    CHOICES="$CHOICES, $PGLANG"
	fi
    fi

    db_subst postgresql/settings/locale CURRENT_LOCALE_LIST "$CHOICES"
    db_input medium postgresql/settings/locale || true
    db_go

    # Guess the postgresql date order from the PGLANG setting if question
    # is new
    db_fget postgresql/settings/day_month_order seen || true
    if [ "$RET" != "true" ]
    then
	db_get postgresql/settings/locale || true
	D=`env LC_TIME=$RET date -d "January 30 2000" +%x | cut -c 1-2` || true
	if [ "$D" = "01" ]
	then
	    db_set postgresql/settings/day_month_order US
	else
	    db_set postgresql/settings/day_month_order European
	fi
	db_input medium postgresql/settings/day_month_order || true
    fi

    db_go

    # when upgrade is needed, ask for policy and paths
    if [ -f "$PGDATA/PG_VERSION" ] && [ "`cat \"${PGDATA}/PG_VERSION\" 2>/dev/null`" != %PG_VERSION% ]
    then
	# ask for upgrading dump directories
	db_set postgresql/upgrade/dump_location "/var/lib/postgres/"
	PARENT="`dirname \"$PGDATA\"`"
	db_set postgresql/upgrade/preserve_location "$PARENT/preserve"

	db_input medium postgresql/upgrade/policy || true
	db_go

	db_get postgresql/upgrade/policy
	if [ "$RET" = true ]
	then
	    db_input medium postgresql/upgrade/dump_location || true
	    db_input medium postgresql/upgrade/preserve_location || true
	    db_go
	fi
    fi
fi
