#! /bin/sh 
# postinst script for piwigo

set -e

old_version=$2

lighttpd_install() {
    if [ ! -f /etc/lighttpd/conf-available/50-piwigo.conf ] ; then
	if which lighty-enable-mod >/dev/null 2>&1 ; then
	    ln -s ../../piwigo/lighttpd.conf /etc/lighttpd/conf-available/50-piwigo.conf
	    lighty-enable-mod piwigo
	    reload_server lighttpd
	fi
    fi
}

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

reload_server() {
    server=$1
    if [ -x /usr/sbin/invoke-rc.d ]; then
	invoke-rc.d $server reload 3>/dev/null || true
    else
	/etc/init.d/$server reload 3>/dev/null || true
    fi
}

database_default_config() {
    if [ "$dbc_dbtype" = sqlite3 ]; then
	. /usr/share/dbconfig-common/internal/sqlite
    else    
	. /usr/share/dbconfig-common/internal/$dbc_dbtype
    fi

    if [ "$dbc_dbtype" = pgsql ]; then
	command_prefix='_dbc_'
    else
	command_prefix='dbc_'
    fi

    if [ "$dbc_dbtype" = sqlite3 ]; then
	exec_command=${command_prefix}sqlite_exec_command
    else 
	exec_command=${command_prefix}${dbc_dbtype}_exec_command
    fi

    # install admin and guest users
    # sqlite doesn't know md5 function
    md5_password="`echo -n $ADMIN_PASSWORD | md5sum | sed -e 's/ *-$//'`"

    pwg_command="INSERT INTO piwigo_users 
    (id, username, password, mail_address) 
    VALUES 
    (1, '$ADMIN_LOGIN', '$md5_password', '$ADMIN_MAIL');"
    $exec_command $pwg_command || true

    pwg_command="INSERT INTO piwigo_users 
    (id, username, password, mail_address) 
    VALUES 
    (2, 'guest', '', '');"
    $exec_command $pwg_command || true

    # create user infos
    # sqlite doesn't know now function
    now="`date +'%F %T'`"
    pwg_command="INSERT INTO piwigo_user_infos
    (user_id, status, registration_date)
    VALUES
    (1, 'webmaster', '$now');"
    $exec_command $pwg_command || true

    pwg_command="INSERT INTO piwigo_user_infos
    (user_id, status, registration_date)
    VALUES
    (2, 'guest', '$now');";
    $exec_command $pwg_command || true

    # create default gallery
    pwg_command="INSERT INTO piwigo_sites 
    (id,galleries_url) VALUES (1,'./galleries/');"
    $exec_command $pwg_command || true

    # secret key in config table
    secret_key="`dd if=/dev/urandom count=1 | shasum | sed -e s'/ *-$//'`"
    pwg_command="INSERT INTO piwigo_config (param,value,comment) 
    VALUES ('secret_key','$secret_key','a secret key specific to the gallery for internal use');"
    $exec_command $pwg_command || true

    pwg_command="INSERT INTO piwigo_config (param,value) 
    VALUES('piwigo_db_version', '2.3');"
    $exec_command $pwg_command || true    
}

database_default_params() {
    if [ "$dbc_dbtype" = sqlite3 ]; then
	. /usr/share/dbconfig-common/internal/sqlite	
    else
	. /usr/share/dbconfig-common/internal/$dbc_dbtype
    fi

    if [ "$dbc_dbtype" = pgsql ]; then
	command_prefix='_dbc_'
    else
	command_prefix='dbc_'
    fi

    if [ "$dbc_dbtype" = sqlite3 ]; then
	exec_command=${command_prefix}sqlite_exec_command
    else 
	exec_command=${command_prefix}${dbc_dbtype}_exec_command
    fi

    # default languages
    pwg_command="INSERT INTO piwigo_languages (id, name)
    VALUES('en_UK', 'English [UK]');"
    $exec_command $pwg_command || true

    # default themes
    pwg_command="INSERT INTO piwigo_themes (id, version, name) VALUES('Sylvia', 1, 'Sylvia');"
    $exec_command $pwg_command || true
    pwg_command="INSERT INTO piwigo_themes (id, version, name) VALUES('clear', 3, 'clear');"
    $exec_command $pwg_command || true
    pwg_command="INSERT INTO piwigo_themes (id, version, name) VALUES('dark', 1, 'dark');"
    $exec_command $pwg_command || true
}

dbc_first_version="2.0.8-1"
dbc_dbtypes="mysql, pgsql, sqlite3"

. /usr/share/debconf/confmodule

db_get piwigo/debconf_install
if [ "$RET" = false ]; then
    exit 0;
fi

. /usr/share/dbconfig-common/dpkg/postinst

dbc_dbfile_owner="www-data:www-data"
dbc_dbfile_perms="0640"

if [ "$1" = configure ] || [ "$1" = reconfigure ]; then
    db_get piwigo/debconf_install
    db_get piwigo/database-type
    if [ "$RET" = sqlite3 ];then
	include_template="config_db_sqlite_php.template"
    else
	include_template="config_db_php.template"
    fi

    dbc_generate_include_owner="root:www-data"
    dbc_generate_include_perms="0640"
    dbc_generate_include=template:/etc/piwigo/config-db.php
    dbc_generate_include_args="-o template_infile=/usr/share/piwigo/debian/$include_template"

    dbc_go piwigo $@

    if [ -z "$old_version" ]; then
	db_get piwigo/admin_login
	ADMIN_LOGIN=${RET:-admin}
	db_get piwigo/admin_password
	ADMIN_PASSWORD=${RET:-admin}
	db_get piwigo/admin_mail
	ADMIN_MAIL=${RET:-admin@localhost}	

	database_default_config
	database_default_params
    else
	if [ ! "$old_version" = "2.2.1-1" ];then
	    mv /usr/share/piwigo/web/local /var/lib/piwigo/web/local
	    if [ -d  /usr/share/piwigo/web/local ];then 
		rmdir /usr/share/piwigo/web/local
	    fi
	    ln -s /var/lib/piwigo/web/local /usr/share/piwigo/web/local
	fi
	if [ -f /var/cache/www/piwigo/piwigo ];then
	    mv /var/cache/www/piwigo/piwigo /var/lib/dbconfig-common/sqlite3/piwigo/
	fi
    fi

    chgrp www-data /var/cache/www/piwigo
    chmod 775 /var/cache/www/piwigo

    chgrp -R www-data /var/lib/piwigo/web/plugins
    chmod -R 775 /var/lib/piwigo/web/plugins

    chgrp -R www-data /var/lib/piwigo/web/themes
    chmod -R 775 /var/lib/piwigo/web/themes

    chgrp -R www-data /var/lib/piwigo/web/language
    chmod -R 775 /var/lib/piwigo/web/language

    chgrp -R www-data /var/lib/piwigo/web/local/css
    chmod -R 775 /var/lib/piwigo/web/local/css

    chgrp -R www-data /var/lib/piwigo/web/local/combined
    chmod -R 775 /var/lib/piwigo/web/local/combined

    chgrp -R www-data /var/lib/piwigo/web/local/language
    chmod -R 775 /var/lib/piwigo/web/local/language

    chgrp -R www-data /var/lib/piwigo/web/upload
    chmod -R 775 /var/lib/piwigo/web/upload

    chgrp www-data /var/lib/piwigo/web/galleries
    chmod 775 /var/lib/piwigo/web/galleries

    # Configure webserver
    db_get piwigo/webserver
    webservers="$RET"

    for webserver in $webservers; do
	webserver=${webserver%,}

	if [ "$webserver" = "lighttpd" ] ; then
	    lighttpd_install
	else 
	    apache_install $webserver
	fi
    done
fi

#DEBHELPER#

exit 0
