#!/bin/sh

httpserver_init() {
    db_get movim/httpserver
    httpservers="$RET"
    for httpserver in $httpservers; do
    httpserver=${httpserver%,}

    #if [ "$httpserver" = "nginx" ] ; then
    #    nginx_install
    #else
        apache_install $1 $httpserver
    #fi
    done
}

apache_install() {
    server="apache2"
    CONF="movim"
    COMMON_STATE=$(dpkg-query -f '${Status}' -W 'apache2.2-common' 2>/dev/null | awk '{print $3}' || true)

    if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
    . /usr/share/apache2/apache2-maintscript-helper
    apache2_invoke enconf $CONF || exit $?
    elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
        [ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/$CONF.conf ] \
        && ln -s ../conf-available/$CONF.conf /etc/apache2/conf.d/$CONF.conf
        reload_server $server
    fi
}

#nginx_install() {
    #server="apache2"
    #CONF="movim"
    #COMMON_STATE=$(dpkg-query -f '${Status}' -W 'apache2.2-common' 2>/dev/null | awk '{print $3}' || true)

    #if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
    #. /usr/share/apache2/apache2-maintscript-helper
    #apache2_invoke enconf $CONF || exit $?
    #elif [ "$COMMON_STATE" = "installed" ] || [ "$COMMON_STATE" = "unpacked" ] ; then
        #[ -d /etc/apache2/conf.d/ ] && [ ! -L /etc/apache2/conf.d/$CONF.conf ] \
        #&& ln -s ../conf-available/$CONF.conf /etc/apache2/conf.d/$CONF.conf
        #reload_server $server
    #fi
#}

#
# fixperms 0644 /usr/share/movim
#
fixperms () {
    PERMS="$1"
    FILE="$2"
    chmod ${PERMS} "${FILE}"
}

#
# fixownership user:group /var/cache/movim
#
fixownership ()
{
    OWNER="$1"
    FILE="$2"
    chown ${OWNER} "${FILE}"
}

fixownershipdir ()
{
    OWNER="$1"
    FILE="$2"
    chown -R ${OWNER} "${FILE}"
}

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
}

. /usr/share/debconf/confmodule

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

. /usr/share/dbconfig-common/dpkg/postinst
dbc_go movim $@

if [ "$dbc_dbtype" == 'mysql' ]; then
    dbport='3306'
else
    dbport='5432'
fi

# We create the Movim configuration file
#touch /etc/movim/db.inc.php
echo "<?php
# This is the database configuration of Movim
# You need to copy an rename this file to 'db.inc.php' and complete the values
\$conf = array(
    # The type can be 'pgsql' or 'mysql'
    'type'        => '$dbc_dbtype',
    # The database username
    'username'    => '$dbc_dbuser',
    # The password
    'password'    => '$dbc_dbpass',
    # Where can we find the database ?
    'host'        => 'localhost',
    # The port number, 3306 for MySQL and 5432 for PostGreSQL
    'port'        => $dbport,
    # The database name
    'database'    => '$dbc_dbname'
);
" > /etc/movim/db.inc.php

httpserver_init $1

# We create some links
ln -s /etc/movim/ /usr/share/movim/config 2>/dev/null
ln -s /var/lib/movim/lib /usr/share/movim/ 2>/dev/null
ln -s /var/lib/movim/locales /usr/share/movim/ 2>/dev/null
ln -s /var/lib/movim/themes /usr/share/movim/ 2>/dev/null
ln -s /var/lib/movim/vendor /usr/share/movim/ 2>/dev/null

# We create the cache folder
mkdir -p /var/cache/movim/cache 2>/dev/null
ln -s /var/cache/movim/cache /usr/share/movim/cache 2>/dev/null
fixperms 0775 /usr/share/movim/cache
fixownershipdir "www-data:www-data" /var/cache/movim/cache
fixownershipdir "www-data:www-data" /usr/share/movim/cache

# We create the log folder
mkdir /var/log/movim 2>/dev/null
ln -s /var/log/movim /usr/share/movim/log 2>/dev/null
fixperms 0775 /var/log/movim
fixownership "root:www-data" /var/log/movim

# We create the user folder
mkdir /var/cache/movim/users 2>/dev/null
ln -s /var/cache/movim/users /usr/share/movim/users 2>/dev/null
fixperms 0775 /user/share/movim/users
fixownershipdir "www-data:www-data" /var/cache/movim/users
fixownershipdir "www-data:www-data" /usr/share/movim/users

# Fix rights
fixperms 0775 /usr/share/movim
fixownership "root:www-data" /usr/share/movim

fixperms 0775 /usr/share/movim/daemon.php
fixownership "root:www-data" /usr/share/movim/daemon.php

# And we install/update the Movim database
echo "Creating and/or updating the Movim database… please be patient"
chmod +x /usr/share/movim/mud.php
/usr/share/movim/mud.php db set

# We clean some log created by the install to prevent issues
rm -rf /usr/share/movim/log/*  /usr/share/movim/cache/*
