#!/bin/bash
# postinst maintainer script for movabletype-opensource

set -e

# source debconf stuff
. /usr/share/debconf/confmodule
# source dbconfig-common stuff
dbc_dbfile_owner="www-data:www-data"
. /usr/share/dbconfig-common/dpkg/postinst 
dbc_go movabletype-opensource $@

if [ "$1" == "configure" ]; then
    # Generate mt-config.cgi

    NEWCONF=/etc/movabletype-opensource/.mt-config.cgi.new

    if [ -f /etc/dbconfig-common/movabletype-opensource.conf ]; then
        . /etc/dbconfig-common/movabletype-opensource.conf
        if [ -n "$dbc_install" -a "$dbc_install" == "true" ]; then
            # We can proceed
            cat > $NEWCONF <<EOF
CGIPath /cgi-bin/movabletype/
StaticWebPath /mt-static/
StaticFilePath /usr/share/movabletype/static/
MailTransfer sendmail
SendMailPath /usr/sbin/sendmail
HTMLUmask 0022
DBUmask 0022
UploadUmask 0022
DirUmask 0022
CaptchaSourceImageBase /usr/share/movabletype/static/images/captcha-source/
ThemesDirectory /usr/share/movabletype/themes/

EOF
            REALCONF=/etc/movabletype-opensource/mt-config.cgi
            if [ -f $REALCONF ]; then
                chown --reference $REALCONF $NEWCONF
                chmod --reference $REALCONF $NEWCONF
            else
                chown root:www-data $NEWCONF
                chmod 640 $NEWCONF
            fi

            if [ "$dbc_dbtype" == "sqlite" ]; then
                echo "ObjectDriver DBI::sqlite" >> $NEWCONF
                echo "Database $dbc_basepath/$dbc_dbname" >> $NEWCONF
            elif [ "$dbc_dbtype" == "mysql" ]; then
                echo "ObjectDriver DBI::mysql" >> $NEWCONF
                echo "Database $dbc_dbname" >> $NEWCONF
                echo "DBUser $dbc_dbuser" >> $NEWCONF
                if [ -n "$dbc_dbpass" ]; then
                    echo "DBPassword $dbc_dbpass" >> $NEWCONF
                fi
                if [ -n "$dbc_dbhost" ]; then
                    echo "DBHost $dbc_dbhost" >> $NEWCONF
                fi
                if [ -n "$dbc_dbport" ]; then
                    echo "DBPort $dbc_dbport" >> $NEWCONF
                fi
            elif [ "$dbc_dbtype" == "pgsql" ]; then
                echo "ObjectDriver DBI::pgsql" >> $NEWCONF
                echo "Database $dbc_dbname" >> $NEWCONF
                echo "DBUser $dbc_dbuser" >> $NEWCONF
                if [ -n "$dbc_dbpassword" ]; then
                    echo "DBPassword $dbc_dbpassword" >> $NEWCONF
                fi
                if [ -n "$dbc_dbhost" ]; then
                    echo "DBHost $dbc_dbhost" >> $NEWCONF
                fi
                if [ -n "$dbc_dbport" ]; then
                    echo "DBPort $dbc_dbport" >> $NEWCONF
                fi
            fi

            ucf --debconf-ok $NEWCONF $REALCONF
            rm -f $NEWCONF

        fi
    fi

    CONF="movabletype-opensource"
    if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
        . /usr/share/apache2/apache2-maintscript-helper
        apache2_invoke enconf $CONF
    elif dpkg-query -f '${Version}'  -W 'apache2.2-common' > /dev/null 2>&1 ; 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
    fi

    # Reset this for the next upgrade
    db_fset movabletype-opensource/schema_upgrade seen false
fi

#DEBHELPER#
