#! /bin/sh -e
# postinst script for dotlrn 
#
# 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'.

get_config() {
    db_get dotlrn/db_host
    db_host="$RET"

    db_type="postgres7"
    db_port=5432 

    db_get dotlrn/dba_name
    dba_name="$RET"

    db_get dotlrn/dba_password
    dba_password="$RET"

    db_get dotlrn/dbu_name
    dbu_name="$RET"

    db_get dotlrn/dbu_password
    dbu_password="$RET"
}

db_not_installed() {
    echo "Please install the chosen dotLRN SGBD: postgresql, then try"
    echo "dpkg-reconfigure dotlrn"
}

case "$1" in
    configure)
	. /usr/share/debconf/confmodule
	db_version 2.0

	# Read debconf and edit the config file accordingly
	get_config
	db_stop
	exec 0<&1
	
	 # Care about the repository
        repository=/var/lib/dotlrn
        if [ -d $repository ]; then
            # set the owner and change rights accordingly
            chown -R www-data:www-data $repository
            chmod 0755 $repository
        fi

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

	# care about the database creation
	dbname=dotlrn
	dbserver=$db_host
	dbadmin=$dba_name
	dbadmpass=$dba_password
	dbuser=$dbu_name
	dbpass=$dbu_password

	. /usr/share/wwwconfig-common/pgsql-createuser.sh
	if [ "$status" = "error" ]
	then
		err=1
		echo $error
	fi
	. /usr/share/wwwconfig-common/pgsql-createdb.sh
	if [ "$status" = "error" ]
	then
		err=1
		echo $error
		# Install language plpgsql on the database
		
		su - $dbadmin -C createlang plpgsql $dbname
	fi

	# Install tsearch2
	#. /usr/share/wwwconfig-common/pgsql-exec.sh
        #if [ "$status" = "error" ]
        #then
        #        err=1
        #        echo $error
        #fi

	# Install plpgsql on database
	dbuser=$dba_name
	dbpass=$dba_password
	sqlfile=/tmp/dotlrn-create-plpgsql.sql
	echo "CREATE LANGUAGE plpgsql;" > $sqlfile
        . /usr/share/wwwconfig-common/pgsql-exec.sh
	rm -f $sqlfile
        if [ "$status" = "error" ]
        then
                err=1
                echo $error
        fi

	
	if [ "$err" = "1" ]
	then
		echo "-------------------------------------------------------------"
		echo "I've tried my best to create the dotLRN user and database"
		echo "but an error has occurred"
		echo "-------------------------------------------------------------"
		echo "You will have to set up the dotLRN user and database yourself"
		echo "-------------------------------------------------------------"
	fi

	# Install the init script and start it (not necessary, using debhelper at rules file)
	#update-rc.d dotlrn defaults
	#/etc/init.d/dotlrn start

    ;;

    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#

exit 0