#!/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/

EOF
            chmod 600 $NEWCONF
            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

            REALCONF=/etc/movabletype-opensource/mt-config.cgi
            if [ -f $REALCONF ]; then
                ISNEW=no
            else
                ISNEW=yes
            fi
            ucf --debconf-ok $NEWCONF $REALCONF
            if [ $ISNEW = "yes" ]; then
                chown root:www-data $REALCONF
                chmod 640 $REALCONF
            fi
            rm -f $NEWCONF

        fi
    fi

    if [ -x "`which apache2ctl`" ]; then
        if [ -z "$2" ] || dpkg --compare-versions "$2" lt 4.2.6.1-2~test.3; then
            db_get movabletype-opensource/reload_apache || true
            RELOAD="$RET"
            if [ "$RELOAD" = "true" ]; then
                if apache2ctl configtest 2>/dev/null; then
                    invoke-rc.d apache2 reload || true
                else
                    echo "apache2 configuration broken, not reloading!"
                fi
            else
                echo "Please manually reload Apache to apply the configuration update"
            fi
        fi
    fi

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

#DEBHELPER#
