#!/bin/sh
# postinst script for eprints
#
# 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>
#        * <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)
        # ./configure inserted user/group www-data into SystemSettings.pm. Fix this now.
        # (Can't fix it earlier because ./install.pl checks that the user/group exist.)
        sed -i -e 's/www-data/eprints/' /opt/eprints3/perl_lib/EPrints/SystemSettings.pm

        # Set up eprints user/group and fix file ownership.
        adduser --quiet --system --group --home /opt/eprints3 --no-create-home eprints
	usermod -a -G eprints www-data
        chown -Rf eprints:eprints /opt/eprints3

        # Set up Apache config
        echo "Include /opt/eprints3/cfg/apache.conf" > /etc/apache2/sites-available/eprints3
        /usr/sbin/a2ensite eprints3
        sudo -u eprints /opt/eprints3/bin/generate_apacheconf --quiet

        # Restart Apache to load the EPrints config.
        if [ -x "/etc/init.d/apache2" ]; then
            invoke-rc.d apache2 restart
        fi

        # Warn about the possible need to configure the SMTP server.
        echo
        echo "You may still need to configure the outgoing mail server for EPrints."
        echo "This setting may be altered using the Webmin configuration tool, or"
        echo "by editing /opt/eprints3/perl_lib/EPrints/SystemSettings.pm (edit"
        echo "the value of 'smtp_server')."
        echo
        echo "You can now create an archive using either the Webmin configuration"
        echo "or by running 'sudo -u eprints /opt/eprints3/bin/epadmin create' in"
        echo "a shell window."
        echo
     ;;

    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


