#!/bin/bash
# postinst script for ocalcli
#
# see: dh_installdeb(1)

set -e

# 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

SOURCEDIR=

INSTDIR=
LINKDIR=

USRCONFDIRNAME=
SRCCONFDIR=
PROFILE=

USRSKEL=

SCDIR=
INSTROOT=

#echo "POSTINST-CASE: "$1

case "$1" in
    configure)
    	# change into src directory postinst should work from
	cd $SOURCEDIR

	# disable LD_ASSUME_KERNEL to be exported cause of wrong kernel set
	cp cal_linux cal_linux.save
	cat cal_linux.save | sed "s/export LD_ASSUME_KERNEL/#xport LD_ASSUME_KERNEL/" > cal_linux
	rm cal_linux.save

	# set destination directory specified in ocal_conf/silent.properties to what u need it to be using sed
	cp ocal_conf/silent.properties ocal_conf/silent.properties.save
	cat ocal_conf/silent.properties.save | sed -e 's!USER_INSTALL_DIR=\$USER_HOME\$\/OracleCalendar!'"USER_INSTALL_DIR=$INSTDIR"'!' | sed -e 's!USER_SHORTCUTS=\$USER_HOME\$\/bin!'"USER_SHORTCUTS=$LINKDIR"'!' > ocal_conf/silent.properties
	rm ocal_conf/silent.properties.save

	# run the ocalcli installer in silent mode
	./silent_install.sh
	
	# set template conffiles for real users
	declare -a allusers
	allusers=(`cat /etc/passwd | cut -d":" -f1,3,6,7`)

	for usr in ${allusers[@]}
	do
		usrname=${usr%%\:*}
		usrshell=${usr##*\:}

		tmp=${usr%\:*}
		usrhome=${tmp##*\:}

		tmp=${usr#*\:}
		usrid=${tmp%%\:*}

		if [[ -e $usrhome && ( "$usrid" -gt "999" || "$usrid" -eq "0" ) && "$usrshell" != "/bin/false" ]] ; then
			echo ""
			echo "Soll das HU-Kalendar-Profil für Benutzer '"$usrname"' installiert werden (Y/n)?"
			read HUCP
			if [ "$HUCP" == "" -o "$HUCP" == "Y" -o "$HUCP" == "y" ]; then
				echo "Installiere HU-Kalendar-Profil für Benutzer '"$usrname"' ..."

				# install personal configfiles only if theres nothing left 
				# -> when it was saved after upgrade
				# 
				# when directories exists ... check if files has been modified -> copy new ones if not otherwise ask  
				if [ ! -e $usrhome"/"$USRCONFDIRNAME ]; then
					#echo "+ Creating directory '"$usrhome"/"$USRCONFDIRNAME"' ..."
					mkdir $usrhome"/"$USRCONFDIRNAME
					#echo "+ Setting rights for directory '"$usrhome"/"$USRCONFDIRNAME"/' ..."
					chmod 700 $usrhome"/"$USRCONFDIRNAME
					#echo "+ Changing owner for directory '"$usrhome"/"$USRCONFDIRNAME"/' ..."
					chown $usrname:$username $usrhome"/"$USRCONFDIRNAME
				else	
					if [ -e $usrhome"/"$USRCONFDIRNAME"/"$PROFILE ]; then
						
						
						if ! diff --brief $usrhome"/"$USRCONFDIRNAME"/"$PROFILE $SRCCONFDIR"/"$PROFILE > /dev/null && [ \( $usrhome"/"$USRCONFDIRNAME"/"$PROFILE -ot $SRCCONFDIR"/"$PROFILE \) -o \( $usrhome"/"$USRCONFDIRNAME"/"$PROFILE -nt $SRCCONFDIR"/"$PROFILE \) ]; then
							echo "Benutzerkonfiguration hat sich geändert. Mit mitgelieferter Datei überschreiben (Y/n)?"
							read OVWR							
							if [ "$OVWR" == "" -o "$OVWR" == "Y" -o "$OVWR" == "y" ]; then							
								echo "Überschreibe veränderte Benutzerkonfiguration mit paketinterner."
								rm -rf $usrhome"/"$USRCONFDIRNAME"/*"
								cp $SRCCONFDIR"/"$PROFILE $usrhome"/"$USRCONFDIRNAME"/"	
							fi
						else
							echo "Überschreibe unveränderte Benutzerkonfiguration."
							rm -rf $usrhome"/"$USRCONFDIRNAME"/*"
							cp $SRCCONFDIR"/"$PROFILE $usrhome"/"$USRCONFDIRNAME"/"
						fi
					fi					
				fi

				if [ ! -e $usrhome"/"$USRCONFDIRNAME"/"$PROFILE ]; then
					#echo "+ Copying profile file '"$PROFILE"' to '"$usrhome"/"$USRCONFDIRNAME"/' ..."
					# copy profile nach ~/.OracleCalendar
					cp $SRCCONFDIR"/"$PROFILE $usrhome"/"$USRCONFDIRNAME"/"
					# chown und chmod auf ~/.OracleCalendar
					#echo "+ Setting rights for file '"$usrhome"/"$USRCONFDIRNAME"/"$PROFILE"' ..."
					chmod 600 $usrhome"/"$USRCONFDIRNAME"/"$PROFILE
					#echo "+ Changing owner for file '"$usrhome"/"$USRCONFDIRNAME"/"$PROFILE"' ..."
					chown $usrname:$username $usrhome"/"$USRCONFDIRNAME"/"$PROFILE
				fi
			fi
		fi
	done

	echo ""
	echo "Oracle calendar desktop client successfully installed to '$INSTDIR'."
    ;;

    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


