#!/bin/sh

set -e

atdir=/usr/share/request-tracker3.8/plugins/RTx-AssetTracker
etcdir=$atdir/etc

upgrade_database ()
{
    echo "upgrading the RT database for Asset Tracker" 1>&2
    for dir in $etcdir/upgrade/*
    do
        for action in schema acl
        do
            run_setup_database $action $dir
        done
    done
    echo "Database upgrade done, see the RT (sys)log for details" 1>&2
}

setup_database ()
{
    echo "setting up the RT database for Asset Tracker" 1>&2
    for action in schema acl insert
    do
        run_setup_database $action $etcdir
    done
    echo "Database setup done, see the RT (sys)log for details" 1>&2
}

run_setup_database ()
{
    action=$1
    datadir=$2
    command="$atdir/sbin/rt-setup-database-debian -v 3.8 -- --action $action --datadir $datadir"

    if [ "$action" = "insert" ]
    then
	export PERLLIB=$atdir/lib
	command="$command --datafile $etcdir/initialdata"
    fi

    # cosmetics for the output
    base=$(basename $datadir)
    if [ "$base" = "etc" ]
    then
        base="Asset Tracker"
    fi
    echo -n "updating database $action for $base..." 1>&2

    run_command "$command"
}

run_command ()
{
    command="$1"
    local exitcode
    set +e
    output=$($command 2>&1 >/dev/null)
    exitcode=$?
    set -e
    if [ $exitcode != 0 ] #|| [ -n "$output" ]
    then
        echo "command failed with code $exitcode" 1>&2
        db_capb escape
        error=$(debconf-escape -e << EOF
database modification command "$command" exited with code $exitcode
Error messages follow:
$output
EOF
)
        db_subst rt3.8-extension-assettracker/modify-database-error error "$error"
        db_fset rt3.8-extension-assettracker/modify-database-error seen false
        db_input critical rt3.8-extension-assettracker/modify-database-error || true
        db_go || true
        db_get rt3.8-extension-assettracker/modify-database-error
        ERROR_CHOICE="$RET"
        if [ "$ERROR_CHOICE" = "abort" ]
        then
            echo "postinst script aborted" 1>&2
            exit 1
        fi
        if [ "$ERROR_CHOICE" = "retry" ]
        then
            echo "postinst script: retry to set up the database $action" 1>&2
            run_command $@
        fi
     else
        echo "OK" 1>&2
     fi
}

test_database ()
{
    local testresult
    testresult=0
    permission="$1"
    $atdir/sbin/test-database 2>/dev/null 1>&2 || testresult=$?
    set -e
    if [ "$permission" = "prompt" ] && \
       ( [ $testresult = 1 ] || [ $testresult = 2 ] )
    then
        case $testresult in
            1)
                db_fset rt3.8-extension-assettracker/setup-database-prompt seen false
                db_input high rt3.8-extension-assettracker/setup-database-prompt || true
                db_go
                db_get rt3.8-extension-assettracker/setup-database-prompt
                break
                ;;
            2)
                db_fset rt3.8-extension-assettracker/upgrade-database-prompt seen false
                db_input high rt3.8-extension-assettracker/upgrade-database-prompt || true
                db_go
                db_get rt3.8-extension-assettracker/upgrade-database-prompt
                break
                ;;
        esac
        if [ "$RET" = "false" ]
        then
            echo "database modification denied by the administrator" 1>&2
            return 0
        fi
    fi
    if [ $testresult = 255 ]
    then
        echo "Database state check failed, skipping modifications. Check your RT_SiteConfig.pm." 1>&2
    fi
    return $testresult
}

. /usr/share/debconf/confmodule

db_version 2.0

case "$1" in
    configure|reconfigure)
        db_get rt3.8-extension-assettracker/modify-database-permission
        CONFFILE=/etc/rt3.8-extension-assettracker/debian.conf
        MODIFY_DATABASE="unknown"
        if [ -r $CONFFILE ]
        then
            . $CONFFILE
        fi
        if [ "$RET" != "$MODIFY_DATABASE" ]
        then
            # rewrite the configuration file
            tmpfile=$(mktemp -t rt3.8-extension-assettracker.XXXXXXXX) || exit 1
            chmod 644 $tmpfile
            cat >$tmpfile << EOF
# automatically modify the database when needed?
# valid options are "allow", "deny" and "prompt"
MODIFY_DATABASE="${RET}"
EOF
            ucf --debconf-ok $tmpfile $CONFFILE
            rm $tmpfile
        fi

        if [ "$RET" != "deny" ]
        then
            retcode=0
            test_database $RET || retcode=$?
            case $retcode in
            1)
                setup_database
                ;;
            2)
                upgrade_database
                ;;
            esac
        fi
        db_stop

        # add the configuration file into the ucf registry
        if which ucfr >/dev/null 2>&1
        then
            ucfr rt3.8-extension-assettracker $CONFFILE
        fi

        break
        ;;
esac

#DEBHELPER#

