#!/bin/sh
# postinst script for docbookwiki
#
# see: dh_installdeb(1)
. /usr/share/debconf/confmodule

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


case "$1" in
    configure)
    if ! getent passwd dbwiki >/dev/null; then
        adduser --disabled-password  --quiet --system \
        --home /usr/share/docbookwiki \
        --gecos "DocBookWiki" --group dbwiki
    fi
    dpkg-maintscript-helper mv_conffile /etc/apache2/conf.d/docbookwiki /etc/apache2/conf.available/docbookwiki 0.9.2-2 docbookwiki -- "$@"
    # Removing configuration added in versions of the package prior to 0.9.2
    sed -e '/allow apache to run everything as dbwiki/{N;d;p}' < /etc/sudoers > /etc/sudoers.new
    cmp -s /etc/sudoers.new /etc/sudoers
    if [ $? -eq 0 ]
    then
	rm /etc/sudoers.new
    else  
	mv -f /etc/sudoers.new /etc/sudoers
	chmod 0440 /etc/sudoers
    fi
    if [ ! -f /etc/sudoers.d/docbookwiki ]; then
	cat <<EOF > /etc/sudoers.d/docbookwiki
www-data	ALL = (dbwiki) NOPASSWD: ALL
EOF
	chmod 0440 /etc/sudoers.d/docbookwiki
	if grep -q "#includedir /etc/sudoers.d" /etc/sudoers; then
	    # Ponder why grep -qv doesn't work, allowing us to test for a non-match
	    sleep 0
        else
	    db_input high docbookwiki/sudo_warning
	    db_go
        fi
    fi
    cd /usr/share/docbookwiki
    chown -R dbwiki .
    chown -R dbwiki /etc/docbookwiki
    su -c "install/configure.sh" dbwiki --shell /bin/sh
    if [ ! -d /usr/share/docbookwiki/content/books ]; then
	# This is a new installation
	db_get docbookwiki/setup_password
        su -c "install/set_su_passwd.sh $RET ; \
        content/clean.sh all ; \
        content/SVN/init.sh ; \
        echo -n Building content. This may take some time... ; \
        content/make-content.sh > /dev/null ; \
        echo done. ; \
        search/make_index.sh ; \
        content/downloads/checkout_xml_sources.sh" dbwiki --shell /bin/sh
        db_get docbookwiki/generate_downloads
        if [ "$RET" = true ]; then
            echo -n Generating downloads. This may take some time...
            su -c "nice content/downloads/make-all-downloads.sh > /dev/null" dbwiki --shell /bin/sh
            echo done.
        fi
        su -c "content/SVN/email-notifications.sh on" dbwiki --shell /bin/sh
        newfile=false
        if [ -d /etc/apache2/conf.available -a ! -f /etc/apache2/conf.available/docbookwiki.conf -a ! -h /etc/apache2/conf.available/docbookwiki.conf ]; then
            cp docbookwiki.conf /etc/apache2/conf.available/docbookwiki.conf
            newfile=true
        fi
        if $newfile = true; then
            if [ -e /usr/share/apache2/apache2-maintscript-helper ]; then
                /usr/share/apache2/apache2-maintscript-helper apache2_invoke enconf docbookwiki
            fi
        fi
    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


