#!/bin/sh

set -e

OTRSHOME=/usr/share/otrs
CONFIGFILES="Kernel/Config.pm Kernel/Config/GenericAgent.pm maintainance.html"

setup_cron()
{
  # save old cron config to new file /etc/otrs/cron before removing it
  if [ -e /etc/cron.d/otrs_aaa_base ]; then
    cat /etc/cron.d/otrs_* > /etc/otrs/cron
    for FILE in /etc/cron.d/otrs_*; do
      ucf --purge $FILE
      mv -f $FILE $FILE.dpkg-old
    done
  fi

  # register the new cron config
  TMPFILE=$(mktemp)
  cat /var/lib/otrs/cron/* > $TMPFILE
  ucf --three-way --debconf-ok $TMPFILE /etc/otrs/cron
  rm -f $TMPFILE
}

. /usr/share/debconf/confmodule

case "$1" in
  configure|reconfigure)
  for FILE in $CONFIGFILES; do
    ucf --three-way --debconf-ok $OTRSHOME/$FILE.dist /etc/otrs/$FILE
  done
  setup_cron
  ucf --three-way --debconf-ok $OTRSHOME/.fetchmailrc.dist /etc/otrs/fetchmailrc
  chmod 600 /etc/otrs/fetchmailrc
  $OTRSHOME/bin/SetPermissions.sh $OTRSHOME otrs www-data www-data www-data
  [ -e /etc/init.d/apache2 ] && invoke-rc.d apache2 reload
  ;;

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

#DEBHELPER#

