#! /bin/sh -e
# postinst script for openacs 
#
# see: dh_installdeb(1)
#
# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

dbc_first_version="5.7.0+dfsg-1"
dbc_pgsql_createdb_encoding='UTF8'

. /usr/share/debconf/confmodule
db_version 2.0
# source dbconfig-common stuff');
. /usr/share/dbconfig-common/dpkg/postinst
dbc_go openacs $@

case "$1" in
    configure)
    
        # Care about the repository
        repository=/var/lib/openacs
        if [ -d $repository ]; then
            # set the owner and change rights accordingly
            chown www-data:www-data $repository $repository/content-repository-content-files/
            chmod 0755 $repository $repository/content-repository-content-files/
        fi

        # Care about the log directory
        logdir=/var/log/aolserver4/openacs
        if [ -d $logdir ]; then
            # set the owner and change rights accordingly
            chown www-data:www-data $logdir
            chmod 0755 $logdir
        fi

        # Care about the app dir
        appdir=/usr/share/openacs
        if [ -d $appdir ]; then
            # set the owner and change rights accordingly
            chown -R www-data:www-data $appdir
        fi

        # Install plpgsql on database and enable compatibility options
        sqlfile=$(mktemp)
        #echo "CREATE EXTENSION IF NOT EXISTS plpgsql; # For 9.1?
        echo "CREATE OR REPLACE LANGUAGE plpgsql;
            ALTER DATABASE $dbc_dbname SET default_with_oids = on;
        " > $sqlfile
        dbc_pgsql_exec_file $sqlfile
        rm -f $sqlfile
        
        # Modify config.tcl with debconf values
        if [ -z "$dbc_dbserver" ]; then
            dbc_dbserver="localhost";
        fi
        if [ -z "$dbc_dbport" ]; then
            dbc_dbport="5432";
        fi
        sed -i "/^    set db_host/,/^    set db_user/d;s/## Debconf changes (DO NOT EDIT BYHAND) ##/&\n    set db_host               \"$dbc_dbserver\"\n    set db_password           \"$dbc_dbpass\"\n    set db_port               \"$dbc_dbport\"\n    set db_user               \"$dbc_dbuser\"/" /etc/openacs/config.tcl


        # Restart aolserver
        invoke-rc.d aolserver4 restart
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#
db_stop

exit 0


