#!/bin/sh
# postinst script for frontaccounting

set -e

if [ "$DPKG_DEBUG" = "developer" ]; then
  set -x
fi

lighttpd_install() {
	if [ ! -f /etc/lighttpd/conf-available/50-frontaccounting.conf ] ; then
		if which lighty-enable-mod >/dev/null 2>&1 ; then
			ln -s /etc/frontaccounting/lighttpd.conf /etc/lighttpd/conf-available/50-frontaccounting.conf
		else
			echo "Lighttpd not installed, skipping"
		fi
	fi
}

apache_install() {
	webserver=$1
	if [ -d /etc/$webserver/conf.d ] && [ ! -e /etc/$webserver/conf.d/frontaccounting.conf ]; then
		ln -s /etc/frontaccounting/apache.conf /etc/$webserver/conf.d/frontaccounting.conf
	fi
}

get_db_params () {
        db_get "frontaccounting/db_host"
        dbserver="$RET"
        db_get "frontaccounting/db_name"
        dbname="$RET"
        db_get "frontaccounting/db_prefix"
        dbprefix="$RET"
        if [ "$RET" != "false" ]; then
                dbprefix="0_";
        else
                dbprefix="";
        fi
        db_get "frontaccounting/db_admin_user"
        dbadmin="$RET"
        db_get "frontaccounting/db_admin_pass"
        dbadmpass="$RET"
        db_get "frontaccounting/db_user"
        dbuser="$RET"
        db_get "frontaccounting/db_pass"
        dbpass="$RET"
        dbtype=mysql
        db_get "frontaccounting/company"
        company="$RET"
        db_get "frontaccounting/db_fadmin_pass"
        fa_pass="$RET"
        db_get "frontaccounting/db_fadmin_email"
        fa_email="$RET"
        db_get "frontaccounting/db_demo"
        if [ "$RET" != "false" ]; then
                coa_script="en_US-demo.sql";
        else
                coa_script="en_US-new.sql";
        fi
}


. /usr/share/debconf/confmodule

if [ -d /usr/share/frontaccounting/modules ]; then
	chown -R www-data:www-data /usr/share/frontaccounting/modules
fi

if [ -d /usr/share/frontaccounting/lang ]; then
	chown -R www-data:www-data /usr/share/frontaccounting/lang
fi

if [ -d /usr/share/frontaccounting/company ]; then
	chown -R www-data:www-data /usr/share/frontaccounting/company
fi

if [ "$1" = "configure" ]; then

	db_version 2.0

	config="/etc/frontaccounting/config_db.php"
	template="/usr/share/frontaccounting/config_db.php.template"


	# Configure webserver
	db_get frontaccounting/webserver

	webservers="$RET"

	. /usr/share/wwwconfig-common/php.get
        . /usr/share/wwwconfig-common/apache-run.get

	for webserver in $webservers; do
		webserver=${webserver%,}
		if [ "$webserver" = "lighttpd" ] ; then
			lighttpd_install
		else
			apache_install $webserver
		fi
		# Reload webserver
		if [ -x /usr/sbin/invoke-rc.d ]; then
			invoke-rc.d $webserver reload 3>/dev/null || true
		else
			/etc/init.d/$webserver reload 3>/dev/null || true
		fi
	done

		db_get "frontaccounting/restart-webserver"
		if [ "$RET" != "false" ]; then
			restart="${servers}"
			. /usr/share/wwwconfig-common/restart.sh
		fi

		# setup the db

                db_get "frontaccounting/db_initialized"
                dbinitialized="$RET" || true
                db_get "frontaccounting/skipdb"
                skipdb="$RET"
                if [ "$skipdb" != "true" ]; then 
                    get_db_params
                    if [ "$db_initialized" != "true" ]; then 
                        . /usr/share/wwwconfig-common/mysql-createdb.sh
                        . /usr/share/wwwconfig-common/mysql-createuser.sh
                        . /usr/share/wwwconfig-common/mysql.get
                        # Substitute table prefix
                        sqlfile="/usr/share/frontaccounting/sql/${coa_script}"
						varscript="s#0_#${dbprefix}#g;";
						sed $varscript < $sqlfile | $(eval $mysqlcmd -D ${dbname} -s)
                        # Expand our config file
                        varscript="s#{DB_USER}#${dbuser}#g;s#{DB_PASS}#${dbpass}#g;s#{DB_HOST}#${dbserver}#g;s#{DB_NAME}#${dbname}#g;s#{DB_PREFIX}#${dbprefix}#g;s#{DB_COMPANY}#${company}#g;";
                        sed "$varscript" < $template > $config
                        chown www-data:root $config
                        chmod 640 $config
						rm $template
                        db_set "frontaccounting/db_initialized" true
                    fi  # $db_initialized
					# update company name and FA admin password
                    dbadmin=${dbuser}
                    dbadmpass=${dbpass}
                    . /usr/share/wwwconfig-common/mysql.get
                    $(eval $mysqlcmd -D ${dbname} -e "'UPDATE ${dbprefix}users SET password = MD5(\"${fa_pass}\"), email = \"${fa_email}\" WHERE user_id =\"admin\";'" -s)
                    $(eval $mysqlcmd -D ${dbname} -e "'UPDATE ${dbprefix}company SET coy_name = \"${company}\" WHERE coy_code = 1;'" -s)
                fi # $skipdb

		# erase db admin password
		if [ "$skipdb" != "true" ]; then 
			db_reset "frontaccounting/db_admin_pass"
		fi
fi

#DEBHELPER#

db_stop

exit 0
