#!/bin/sh

set -e

. /usr/share/debconf/confmodule

set -u

if [ -n "${DEBIAN_FIREBIRD_DEBUG:-}" ]; then
    set -x
fi

VER="2.5"
PASSWORD_FILE=/etc/firebird/$VER/SYSDBA.password

# find server flavour
if basename $0 | grep -q -- '-classic.config$'; then
    FB_FLAVOUR=classic
elif basename $0 | grep -q -- '-superclassic.config$'; then
    FB_FLAVOUR=superclassic
else
    FB_FLAVOUR=super
fi

DEFAULT=/etc/default/firebird$VER

ACTION=$1

case "$ACTION" in
    configure|reconfigure)

    if [ -f $DEFAULT ]; then
        . $DEFAULT
        if [ "${ENABLE_FIREBIRD_SERVER:-}" = yes ]; then
            enabled=true
        else
            enabled=false
        fi
    elif [ "$FB_FLAVOUR" = classic ]; then
        if grep -q ^gds_db /etc/inetd.conf; then
            enabled=true
        else
            enabled=false
        fi
    else
        enabled=false
    fi

    db_set shared/firebird/enabled $enabled || true

    db_input low shared/firebird/enabled || true
    db_go || true

    db_get shared/firebird/enabled || true

    # ask the password question
    if [ "${RET:-}" = true ]; then
        PASSWORD_FILE="/etc/firebird/$VER/SYSDBA.password"
        if [ -e $PASSWORD_FILE ]; then
            . $PASSWORD_FILE
            if [ -n "${ISC_PASSWORD:-}" ]; then
                # upgrade or reconfiguration
                QUESTION="shared/firebird/sysdba_password/upgrade_reconfigure"
                db_set "$QUESTION" "$ISC_PASSWORD"
                PRIORITY="low"
            else
                # new install
                QUESTION="shared/firebird/sysdba_password/first_install"
                PRIORITY="high"
            fi
        else
            QUESTION="shared/firebird/sysdba_password/first_install"
            PRIORITY="high"
        fi

        db_subst shared/firebird/title FB_VER "$VER"
        db_settitle shared/firebird/title

        db_subst "$QUESTION" FB_VER "$VER"
        db_input "$PRIORITY" "$QUESTION" || true
        db_go

        # store the answer into hidden template question
        db_get "$QUESTION"
        db_set shared/firebird/sysdba_password/new_password "$RET"

        # and remove it from the asked question
        db_reset "$QUESTION"
    fi

    db_stop

    ;;
esac

exit 0

# vi: set sw=4 ts=8 filetype=sh sts=4 :
