#!/bin/sh
# postinst script for dolibarr

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>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/share/doc/packaging-manual/

if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
	# Needs to be run outside of functions to have access to parameters
	. /usr/share/apache2/apache2-maintscript-helper
fi

setup_empty_conf() {
	echo Create empty file $config		
    mkdir -p /etc/dolibarr
    touch /etc/dolibarr/conf.php
    chown root:www-data /etc/dolibarr/conf.php
    chmod 664 /etc/dolibarr/conf.php
}

is_new_upstream_version() {
    # $1 can be empty (not installed) and will result in a true value
    # for the check
    old_version=$(echo "$1" | sed -e 's/-[^-]*$//' -e 's/^[0-9]*://')
    new_version=$(dpkg-query -f '${Version}' -W dolibarr | \
		  sed -e 's/-[^-]*$//' -e 's/^[0-9]*://')
    test "$old_version" != "$new_version"
}

enable_install_upgrade_wizard() {
	echo Enable install wizard by removing install.lock file if present
    rm -f /var/lib/dolibarr/documents/install.lock
}

apache_install() {
	webserver=$1
	if which a2enconf >/dev/null 2>&1 ;then
		# a2enconf exists for ubuntu only
		a2enconf dolibarr
	else 
		if [ -d /etc/$webserver/conf.d ] && [ ! -e /etc/$webserver/conf.d/dolibarr.conf ]; then
			echo "Add link for Apache config file"
			ln -s /etc/$webserver/conf-available/dolibarr.conf /etc/$webserver/conf.d/dolibarr.conf
		fi
	fi
}

lighttpd_install() {
	if which lighty-enable-mod >/dev/null 2>&1 ; then
		echo "Enable lighttpd link for dolibarr config file"
		lighty-enable-mod dolibarr fastcgi-php
	else
		echo "Lighttpd not installed, skipping"
	fi
}



. /usr/share/debconf/confmodule

db_version 2.0


echo Run the dolibarr postinst script


# Define vars
docdir='/var/lib/dolibarr/documents'
installfileorig="/etc/dolibarr/install.forced.php.install"
installconfig="/etc/dolibarr/install.forced.php"
config="/etc/dolibarr/conf.php"


case "$1" in
	configure)
		if [ -z "$2" ]; then
			echo First install
		    #setup_empty_conf
		else
			echo This is not a first install
		fi

		# Apache 2 setup
		if which a2enmod >/dev/null 2>&1 ;then
			a2enmod alias
		fi
		
		apache_install
		lighttpd_install
		
		# Remove lock file
		if is_new_upstream_version "$2"; then
		    enable_install_upgrade_wizard
		fi
				
        # Create document directory for uploaded data files
        mkdir -p $docdir
        chown -R www-data:www-data $docdir
        chmod -R 775 $docdir
        chmod -R g+s $docdir

		# Copy install config file (with matching Debian values) into target directory
		superuserlogin=''
		superuserpassword=''
		if [ -f /etc/mysql/debian.cnf ] ; then
			# Load superuser login and pass
			superuserlogin=$(grep --max-count=1 "user" /etc/mysql/debian.cnf | sed -e 's/^user[ =]*//g')
			superuserpassword=$(grep --max-count=1 "password" /etc/mysql/debian.cnf | sed -e 's/^password[ =]*//g')
		fi
		echo Mysql superuser found to use is $superuserlogin
		if [ -z "$superuserlogin" ] ; then
			cat $installfileorig | sed -e 's/__SUPERUSERLOGIN__/root/g' | sed -e 's/__SUPERUSERPASSWORD__//g' > $installconfig
		else
			cat $installfileorig | sed -e 's/__SUPERUSERLOGIN__/'$superuserlogin'/g' | sed -e 's/__SUPERUSERPASSWORD__/'$superuserpassword'/g' > $installconfig
		fi
		chown -R root:www-data $installconfig
	   	chmod -R 660 $installconfig

		# If a conf already exists and its content was already completed by installer
		if [ ! -s $config ] || ! grep -q "File generated by" $config
		then
			# Create an empty conf.php with permission to web server
			setup_empty_conf
		else
			# File already exist. We add params not found.
			echo Add new params to overwrite path to use shared libraries/fonts
			grep -q -c "dolibarr_lib_ADODB_PATH" $config    || [ ! -d "/usr/share/php/adodb" ]  || echo "<?php \$dolibarr_lib_ADODB_PATH='/usr/share/php/adodb'; ?>" >> $config
			grep -q -c "dolibarr_lib_FPDI_PATH" $config     || [ ! -d "/usr/share/php/fpdi" ]   || echo "<?php \$dolibarr_lib_FPDI_PATH='/usr/share/php/fpdi'; ?>" >> $config
			#grep -q -c "dolibarr_lib_GEOIP_PATH" $config    || echo "<?php \$dolibarr_lib_GEOIP_PATH=''; ?>" >> $config
			grep -q -c "dolibarr_lib_NUSOAP_PATH" $config   || [ ! -d "/usr/share/php/nusoap" ] || echo "<?php \$dolibarr_lib_NUSOAP_PATH='/usr/share/php/nusoap'; ?>" >> $config
			grep -q -c "dolibarr_lib_ODTPHP_PATHTOPCLZIP" $config || [ ! -d "/usr/share/php/libphp-pclzip" ]  || echo "<?php \$dolibarr_lib_ODTPHP_PATHTOPCLZIP='/usr/share/php/libphp-pclzip'; ?>" >> $config
			#grep -q -c "dolibarr_lib_PHPEXCEL_PATH" $config || echo "<?php \$dolibarr_lib_PHPEXCEL_PATH=''; ?>" >> $config
			#grep -q -c "dolibarr_lib_TCPDF_PATH" $config    || echo "<?php \$dolibarr_lib_TCPDF_PATH=''; ?>" >> $config
			grep -q -c "dolibarr_js_CKEDITOR" $config       || [ ! -d "/usr/share/javascript/ckeditor" ]  || echo "<?php \$dolibarr_js_CKEDITOR='/javascript/ckeditor'; ?>" >> $config
			grep -q -c "dolibarr_js_JQUERY" $config         || [ ! -d "/usr/share/javascript/jquery" ]    || echo "<?php \$dolibarr_js_JQUERY='/javascript/jquery'; ?>" >> $config
			grep -q -c "dolibarr_js_JQUERY_UI" $config      || [ ! -d "/usr/share/javascript/jquery-ui" ] || echo "<?php \$dolibarr_js_JQUERY_UI='/javascript/jquery-ui'; ?>" >> $config
			grep -q -c "dolibarr_js_JQUERY_FLOT" $config    || [ ! -d "/usr/share/javascript/flot" ]      || echo "<?php \$dolibarr_js_JQUERY_FLOT='/javascript/flot'; ?>" >> $config
			grep -q -c "dolibarr_font_DOL_DEFAULT_TTF_BOLD" $config || echo "<?php \$dolibarr_font_DOL_DEFAULT_TTF_BOLD='/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf'; ?>" >> $config			
		fi
	
		db_get dolibarr/reconfigure-webserver
	
		webservers="$RET"
	
		# Set up web server.
		for webserver in $webservers ; do
			webserver=${webserver%,}
			echo Complete config of server $webserver
			
			# Detect webuser and webgroup
			webuser=
			webgroup=

			if [ -z "$webuser" ] ; then
				webuser=www-data
			fi
			if [ -z "$webgroup" ] ; then
				webgroup=www-data
			fi

			echo Web user.group used is $webuser.$webgroup

			# Set permissions to web server
			chown -R $webuser:$webgroup /usr/share/dolibarr
            chown -R root:$webgroup $config
		done

		# Restart web server.
		for webserver in $webservers; do
			webserver=${webserver%,}
			if [ "$webserver" = "lighttpd" ] ; then
				lighttpd_install
			else
				apache_install $webserver
			fi
			# Reload webserver in any case, configuration might have changed
			# Redirection of 3 is needed because Debconf uses it and it might 
			# be inherited by webserver. See bug #446324.
	        if [ -f /etc/init.d/$webserver ] ; then
	            if [ -x /usr/sbin/invoke-rc.d ]; then
    	            echo Restart web server $server using invoke-rc.d
	    		    # This works with Debian (5.05,...) and Ubuntu (9.10,10.04,...)
	                invoke-rc.d $webserver reload 3>/dev/null || true
	            else
	                echo Restart web server $server using $server reload
	                /etc/init.d/$webserver reload 3>/dev/null || true
	            fi
	        fi
		done

		
		echo ----------
		echo "Call Dolibarr page http://localhost/dolibarr/ to complete the setup and use Dolibarr."
		echo ----------
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
	;;

	*)
		echo "postinst called with unknown argument $1" >&2
		exit 0
	;;
esac

#DEBHELPER#

db_stop

exit 0
