#!/bin/bash
# config script for Linux-Musterlösung Moodle
# is executed by postinst, if linuxmuster-setup was
# already executed at install time.
#
# fschuett 2012
# GPL-2

# Source debconf library.
. /usr/share/debconf/confmodule

db_version 2.0
db_capb backup

PRIORITY="critical"

# source linuxmuster defaults
. /usr/share/linuxmuster/config/dist.conf || exit 1

db_title "$DISTNAME $DISTFULLVERSION"

# source helperfunctions
. $HELPERFUNCTIONS || exit 1

# read package defaults
. /usr/share/linuxmuster-moodle/config/dist.conf || exit 1

# warning
echo
echo "$0 was called with parameter $1."
if [ "$1" = "--first" ]; then
  echo "Doing a fresh installation."
  echo "This script will fail, if a moodle database does already exist."
  echo "To remove an existing database: dropdb -U postgres moodle"
  echo "To use a already existing database can cause problems with linuxmuster-moodle."
  echo "The process will start in 5 seconds. Press CRTL-C to cancel."
  n=0
  while [ $n -lt 5 ]; do
    echo -n .
    sleep 1
    let n=n+1
  done
  echo
  echo "Let's go! :-)"
  echo
fi

# start logging
echo >> $LOGDIR/setup.log
echo "####################################################" >> $LOGDIR/setup.log
echo "Moodle Setup session started at" >> $LOGDIR/setup.log
date >> $LOGDIR/setup.log
echo "####################################################" >> $LOGDIR/setup.log
# wwwadmin password
if [ "$1" = "--first" ]; then

	# wwwadmin
	STATE=1
	while [ "$STATE"  != 0 ]; do

		case "$STATE" in

			1)
				db_fset linuxmuster-base/wwwadminpw seen false
				db_input $PRIORITY linuxmuster-base/wwwadminpw || true
				db_go
				db_get linuxmuster-base/wwwadminpw || true
				[ -n "$RET" ] && STATE=2
				unset RET
			;;

			2)
				db_fset linuxmuster-base/wwwadminpw_confirm seen false
				db_input $PRIORITY linuxmuster-base/wwwadminpw_confirm || true
				db_go
				db_get linuxmuster-base/wwwadminpw_confirm || true
				if [ -n "$RET" ]; then
					CONFIRM=$RET
					db_get linuxmuster-base/wwwadminpw || true
					if [ "$RET" = "$CONFIRM" ]; then
						STATE=0
					else
						STATE=1
						db_set linuxmuster-base/wwwadminpw "" || true
					fi
					unset RET
					unset CONFIRM
					db_set linuxmuster-base/wwwadminpw_confirm "" || true
				fi
			;;

		esac

	done

	db_get linuxmuster-base/wwwadminpw || true
	# escaping special characters in password
	esc_spec_chars "$RET"
	wwwadminpw=$RET

	# clear wwwadmin user password from debconf db
	db_set linuxmuster-base/wwwadminpw "" || true

else

	wwwadminpw="kein_Passwort"

fi # wwwadmin password

# read debconf database
echo "Reading debconf database ..."
for i in country state location servername domainname schoolname \
         internsubrange serverexternname fwconfig; do
  db_get linuxmuster-base/$i || true
  INPUT="$RET"
  oldvalue=${i}_old
  eval $oldvalue=$INPUT
  eval $i=$INPUT
done

db_stop

# compute ip's
internsub=`echo $internsubrange | cut -f1 -d"-"`
serverip=10.$internsub.1.1
ipcopip=10.$internsub.1.254

# compute basedn from domainname
basedn="dc=`echo $domainname|sed 's/\./,dc=/g'`"

# substitute spaces in schoolname, location and distro
schoolname=${schoolname// /_}
location=${location// /_}
distro="$DISTNAME"
distro=${distro// /-}

# messages for config file headers
message1="##### Do not change this file! It will be overwritten!"
message2="##### This configuration file was automatically created by linuxmuster-setup!"
message3="##### Last Modification: `date`"

# patching conffiles
for i in ${DYNTPLDIR}/${MOODLETPLDIR}; do
  cd $i
  echo "Processing $i ..."
  # do something before the configuration is patched
  [ -f prepatch ] && . prepatch $1 >> $LOGDIR/setup.log 2>&1
  # process all variables through all targets
  if ls *.target &> /dev/null; then
    for t in *.target; do
      eval target=`cat $t`
      conffile=${t%.target}
      [ "$1" = "--modify" ] && backup_file $target >> $LOGDIR/setup.log
      sed -e "s/@@message1@@/${message1}/g
              s/@@message2@@/${message2}/g
              s/@@message3@@/${message3}/g
              s/@@country@@/${country}/g
              s/@@state@@/${state}/g
              s/@@location@@/${location}/g
              s/@@schoolname@@/${schoolname}/g
              s/@@distro@@/${distro}/g
              s/@@servername@@/${servername}/g
              s/@@domainname@@/${domainname}/g
              s/@@serverip@@/${serverip}/g
              s/@@ipcopip@@/${ipcopip}/g
              s/@@fwconfig@@/${fwconfig}/g
              s/@@internsub@@/${internsub}/g
              s/@@basedn@@/${basedn}/g
              s/@@wwwadminpw@@/${wwwadminpw}/g
              s/@@administrator@@/${ADMINISTRATOR}/g
              s/@@admingroup@@/${ADMINGROUP}/g
              s/@@teachersgroup@@/${TEACHERSGROUP}/g
              s/@@wwwadmin@@/${WWWADMIN}/g
              s/@@serverexternname@@/${serverexternname}/g" $conffile > $target
    done
  fi
  # do something after the configuration is patched
  [ -f postpatch ] && . postpatch $1 >> $LOGDIR/setup.log 2>&1
  # move nopatch files back
  for np in *.nopatch; do
    [ -e "$np" ] && mv $np ${np%.nopatch}
  done
done

touch $MOODLEINSTALLED

# finish logging
echo >> $LOGDIR/setup.log
echo "####################################################" >> $LOGDIR/setup.log
echo "Moodle Setup session finished at" >> $LOGDIR/setup.log
date >> $LOGDIR/setup.log
echo "####################################################" >> $LOGDIR/setup.log

unset HISTSIZE HISTFILESIZE

exit 0
