#!/bin/sh
# postinst script for owncloud
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

reload_apache()
{
    if apache2ctl configtest 2>/dev/null; then
	if [ -x /usr/sbin/invoke-rc.d ]; then
            invoke-rc.d apache2 $1 3>/dev/null || true
	else
            /etc/init.d/apache2 $1 3>/dev/null || true
	fi
    else
        echo "Your Apache 2 configuration is broken, so we're not restarting it for you."
    fi
}


case "$1" in
    configure)
        # Test whether a2enmod is available (and thus also apache2ctl).
        if [ -x /usr/sbin/a2enmod ]; then
            # Enable the Apache2 rewrite module if not already enabled
            a2enmod rewrite > /dev/null || true
            # Restart Apache to really enable the module and load
            # /etc/apache2/conf.d/owncloud.conf.
            reload_apache restart
        fi
        # Fix ownership and permissions of existing configuration.
        if [ -f /etc/owncloud/config.php ]; then
            chown www-data:www-data /etc/owncloud/config.php
            chmod 0640 /etc/owncloud/config.php
        fi
        if [ -f /etc/owncloud/htaccess ]; then
            chown www-data:www-data /etc/owncloud/htaccess
            chmod 0640 /etc/owncloud/htaccess
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
