#!/bin/sh
# postinst script for wims-moodle
#
# see: dh_installdeb(1)

set -e
[ -e /usr/share/debconf/confmodule ] && . /usr/share/debconf/confmodule

# 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>
#        * <postinst> `abort-remove'
#        * <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

####### reading dirroot and dataroot in Moodle's configuration file #######
d=">dirroot "
dirroot=$(grep $d /etc/moodle/config.php | head -1 | sed "s/.*'\\(.*\\)'.*/\1/")
d=">dataroot "
dataroot=$(grep $d /etc/moodle/config.php | sed "s/.*'\\(.*\\)'.*/\1/")
###########################################################################
## Getting the address for Wims
db_get wims-moodle/remoteurl
remoteurl="$RET"

manage_config(){
    # fixes the permissions of the configuration files and sets a random
    # password shared between Wims and Moodle
    pass=$(pwgen -1)
    ###### Moodle side
    f=/etc/moodle/wimsconf.php
    sed -e 's/\$CFG->passwims=.*/$CFG->passwims="'$pass'";/' $f > $f.tmp && \
      mv $f.tmp $f
    sed -e 's%\$CFG->wimsRemoteUrl=.*%$CFG->wimsRemoteUrl="'$remoteurl'";%' $f > $f.tmp && \
      mv $f.tmp $f
    rm -f $dirroot/mod/assignment/type/wims/wimsconf.php
    ln -s $f $dirroot/mod/assignment/type/wims/
    chown www-data:www-data $f
    chmod 440 $f
    ###### Wims side
    f=/var/lib/wims/log/classes/.connections/localwims
    [ -f $f ] || cp /usr/share/doc/wims-moodle/localwims.template $f
    sed -e 's/ident_password=.*/ident_password='$pass'/' $f > $f.tmp && \
      mv $f.tmp $f
}

manage_languages(){
    # patches Moodle's files about languages, for the locales
    # which are currently installed in Moodle, and add the translations
    # specific to the module Wims.
    for path in $dirroot/lang $dataroot/lang; do
	dirs=$(find $path -mindepth 1 -maxdepth 1 -type d)
	for d in $dirs; do
	    lang=$(basename $d)
	    includefile=/usr/share/doc/wims-moodle/$lang.inc
	    f=assignment.php
	    if [ -f $includefile ]; then
		cd $d
		[ -f "$f" ] &&
		  sed -e "/yoursubmission/ r $includefile" $f > $f.tmp && 
		  mv $f.tmp $f
		rm -f ./wims.php
		[ -e /usr/share/doc/wims-moodle/lang/$lang/wims.php ] &&
		  ln -s /usr/share/doc/wims-moodle/lang/$lang/wims.php .
	    fi
	done
    done
}

case "$1" in
    configure)
	manage_config
	manage_languages
    ;;

    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
