#!/bin/sh

set -e

. /usr/share/debconf/confmodule

db_get photopub/munge_apache_config

if [ "$RET" = "true" ]; then

   FRAGMENT=`tempfile -d /etc/apache`
   if [ $? -ne 0 ]; then
      echo "$0: Can't create temp file, exiting..." >&2
      exit 1
   fi

   HTTPDCONF=`tempfile -d /etc/apache`
   if [ $? -ne 0 ]; then
      echo "$0: Can't create temp file, exiting..." >&2
      exit 1
   fi

   cat > $FRAGMENT << END
#---photopub---

# Changes to this section will be lost if you remove or upgrade PhotoPub
# You can safely move it elsewhere in this file if you wish
# Do not alter the delimiters above and below or bad things will happen!

Include /etc/apache/conf.d/photopub.conf

#---endphotopub---
END

   if [ -f /etc/photopub/httpd.conf.diff ]; then
      if [ `patch -F 0 --dry-run /etc/apache/httpd.conf \
              < /etc/photopub/httpd.conf.diff \
              2>&1 | wc -l` -eq 1 ]; then
         patch /etc/apache/httpd.conf < /etc/photopub/httpd.conf.diff
      fi
      rm -f /etc/photopub/httpd.conf.diff
   fi

   cp -p /etc/apache/httpd.conf $HTTPDCONF # to get the ownership/perms right
   /usr/lib/photopub/utils/mungefile.pl -a photopub $FRAGMENT \
      < /etc/apache/httpd.conf > $HTTPDCONF

   mv $HTTPDCONF /etc/apache/httpd.conf
   rm $FRAGMENT

fi

/etc/init.d/apache reload || true

case "$1" in
        configure)
                # continue as normal
                ;;
        abort-upgrade|abort-remove|abort-deconfigure)
                exit 0;
        ;;
        *)
                echo "postinst called with unknown argument \`$1'" >&2;
                exit 0;
        ;;
esac

#DEBHELPER#
