#!/bin/sh
# Postinst script for Dokuwiki.

set -e

# Create an apache configuration file for dokuwiki
write_apache_conf()
{
    apacheconf=`tempfile`
    
    # Get config options
    db_get dokuwiki/system/documentroot
    docroot=$RET;

    echo "Alias $docroot /usr/share/dokuwiki/" >> $apacheconf

    # Print directory options for /usr/share/dokuwiki
    cat >> $apacheconf << EOF

<Directory /usr/share/dokuwiki/>
	Options +FollowSymLinks
	AllowOverride All
	order allow,deny
EOF

    db_get dokuwiki/system/accessible
    if [ "$RET" = "global" ]; then  # Globally accessible
        echo "	allow from all" >> $apacheconf
    elif [ "$RET" = "localhost only" ]; then  # Access only from localhost
	echo "	allow from localhost 127.0.0.1" >> $apacheconf
    else  # Access from localnet
	db_get dokuwiki/system/localnet
	echo "	allow from localhost 127.0.0.1" >> $apacheconf
	echo "	allow from $RET" >> $apacheconf
    fi
    
    echo "</Directory>" >> $apacheconf
    
    ucf --debconf-ok $apacheconf /etc/dokuwiki/apache.conf
    ucfr dokuwiki /etc/dokuwiki/apache.conf

    # Remove temporary file
    rm $apacheconf;

    chmod 0664 /etc/dokuwiki/apache.conf
}

# Set up an initial access control system
setup_acl()
{
    # By default allow everyone read access
    if [ ! -e /var/lib/dokuwiki/acl/acl.auth.php ]; then
        aclauth=`tempfile`

        echo '*                     @ALL        4' >> $aclauth

    	ucf --debconf-ok $aclauth /var/lib/dokuwiki/acl/acl.auth.php
        ucfr dokuwiki /var/lib/dokuwiki/acl/acl.auth.php

        # Remove temporary file
        rm $aclauth;
    
        chown www-data:root /var/lib/dokuwiki/acl/acl.auth.php
    fi
    
    if [ ! -e /var/lib/dokuwiki/acl/users.auth.php ]; then
    usersauth=`tempfile`
    touch $usersauth
    	ucf --debconf-ok $usersauth /var/lib/dokuwiki/acl/users.auth.php
        ucfr dokuwiki /var/lib/dokuwiki/acl/users.auth.php

        # Remove temporary file
        rm $usersauth;

    chown www-data:root /var/lib/dokuwiki/acl/users.auth.php
    fi
}

# Create a .htaccess sample file for dokuwiki
write_htaccess()
{
    ucf --debconf-ok /usr/share/dokuwiki/.htaccess.dist /etc/dokuwiki/htaccess
    ucfr dokuwiki /etc/dokuwiki/htaccess

    # Restore the backup of the .htaccess that used to be part of the
    # package and would have been lost. See preinst.
    if [ -f /usr/share/dokuwiki/.htaccess.upgrade ]
    then
        mv /usr/share/dokuwiki/.htaccess.upgrade /etc/dokuwiki/htaccess
    fi
}

# Configure Apache web servers (possibly version 1.3 or 2.x?)
configure_apache()
{
    webserver=$1
    dir="/etc/$webserver/conf.d"
    file="$dir/dokuwiki.conf"
    # Skip servers with no configuration
    if [ -d "$dir" ] && [ ! -e "$file" ]; then
        # Link the apache configuration file to the server's
        # conf.d directory
        echo "Installing into... [$dir]" >/dev/stderr
        ln -fs /etc/dokuwiki/apache.conf "$file"
    fi
}

# Configure requested web server
configure_webservers()
{
    db_get dokuwiki/system/configure-webserver
    webservers="$RET"
    db_get dokuwiki/system/restart-webserver
    restart="$RET"
    for webserver in $webservers; do
        webserver=${webserver%,}
        configure_apache $webserver
        if [ "$restart" = "true" ]; then
            if which invoke-rc.d >/dev/null 2>&1; then
                invoke-rc.d $webserver force-reload 3>/dev/null || true
            else
                /etc/init.d/$webserver force-reload 2>/dev/null || true
            fi
        fi
    done
}

if [ $1 = "configure" ]; then
    . /usr/share/debconf/confmodule

    write_apache_conf
    write_htaccess
    setup_acl
    configure_webservers

    db_stop

    # Move template and plugins directories from /usr/share to
    # /var/lib and create symlinks to them to allow additions within
    # the FHS.
    if dpkg --compare-versions "$2" lt "0.0.20080411~rc.1"; then
        if [ ! -L /usr/share/dokuwiki/lib/tpl ]; then
            echo -n "Moving template directory from /usr/share to /var/lib... " >/dev/stderr
            mv /usr/share/dokuwiki/lib/tpl/* /var/lib/dokuwiki/tpl/ 2> /dev/null || true
            rmdir /usr/share/dokuwiki/lib/tpl
            ln -s /var/lib/dokuwiki/tpl /usr/share/dokuwiki/lib/
            echo "[done]" >/dev/stderr
        fi
    fi
    if dpkg --compare-versions "$2" lt "0.0.20090214b-4"; then
        if [ ! -L /usr/share/dokuwiki/lib/plugins ]; then
            echo -n "Moving plugins directory from /usr/share to /var/lib... " >/dev/stderr
            mv /usr/share/dokuwiki/lib/plugins/* /var/lib/dokuwiki/plugins/ 2> /dev/null || true
            rmdir /usr/share/dokuwiki/lib/plugins
            ln -s /var/lib/dokuwiki/plugins /usr/share/dokuwiki/lib/
            echo "[done]" >/dev/stderr
        fi
    fi
fi

#DEBHELPER#
