#!/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"

localwims_path="/var/lib/wims/log/classes/.connections/localwims"

create_localwims(){
  cat <<EOF > $localwims_path
# This is an example of identification file for connection to another
# platform. It is used for platform authentification.
# If you copy this file to another one in the same directory, 
# and modify the definitions, you get another platform identification
# whose name is the name of that file.

# calling the same WIMS server.

# For security reasons, it is highly recommended that you only accept
# localhost connections. Accept several sites, but no wildcard.
ident_site=127.0.0.1

# Short description
ident_desc=This local WIMS server

# User agent of the connecting platform must identify itself as such.
#ident_agent=WIMS-webget
# if ident_agent is void, there will be no check.
ident_agent=

# https is highly recommended for security.
ident_protocol=https

# password must be a word composed of alpha-numeric characters.
# This identification is disabled by the * in the password definition.
ident_password=secret

# type of remote platform.
# available types: wims.
ident_type=wims

# The address and identifier/password pair for calling back.
back_url=https://localhost/wims/wims.cgi
back_ident=myself
back_password=***

# allowed or disabled requests. At most one of the two should be active.
# ident_allow=
# ident_deny=deluser delclass
EOF
}

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
    echo '<? // this file was generated by wims-moodle package postinst script' > $f
    echo '$CFG->passwims='$pass';' >> $f
    echo '$CFG->wimsRemoteUrl="'$remoteurl'";' >> $f;
    echo '?>' >> $f
    chown www-data:www-data $f $f.dpkg-dist
    chmod 440 $f $f.dpkg-dist
    ###### Wims side
    [ -f $localwims_path ] || create_localwims;
    sed -e 's/ident_password=.*/ident_password='$pass'/' $localwims_path \
	> $localwims_path.tmp && \
	mv $localwims_path.tmp $localwims_path
}


case "$1" in
    configure)
	manage_config
    ;;

    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
