#! /bin/sh
#
# $FreeBSD: ports/mail/mailman/files/pkg-deinstall.in,v 1.1 2012/03/04 06:00:37 tota Exp $

PATH=/bin:/usr/bin:/usr/sbin

case $2 in

DEINSTALL)
  echo "---> Starting deinstall script:"

  if /usr/bin/crontab -u "mailman" -l | \
      /usr/bin/diff - /usr/local/mailman/cron/crontab.in >/dev/null 2>&1 ; then
      echo "---> Zeroing crontab for \"mailman\""
      /usr/bin/crontab -u "mailman" /dev/null
      if [ -e /var/cron/allow ]; then
         grep -v mailman /var/cron/allow > /var/cron/allow.new
         mv /var/cron/allow.new /var/cron/allow
      fi
  else
      echo "---> Crontab for \"mailman\" not removed: please deinstall"
      echo "---> manually if you no-longer wish to use Mailman. eg:"
      echo "--->     /usr/bin/crontab -u "mailman" -r"
  fi

  echo "---> Preserving the \"last_mailman_version\" file"
  /bin/cp -f /usr/local/mailman/data/last_mailman_version /var/tmp/

  # If the errorlog is the only existing logfile, delete it.  (If Mailman's
  # qrunner had not been running, then the process of trying to stop the
  # qrunner (above) will cause the errorlog to be written to.  Hence, even if
  # the Mailman port/package is installed and immediately deinstalled, the
  # errorlog will exist.)
  if [ "`echo /usr/local/mailman/logs/*`" = "/usr/local/mailman/logs/error" ]; then
    echo "---> Deleting errorlog (It is the only existing logfile.)"
    /bin/rm -f /usr/local/mailman/logs/error
  fi
  ;;

POST-DEINSTALL)
  echo "---> Starting post-deinstall script:"

  if [ -d /usr/local/mailman ]; then
    echo '---> /usr/local/mailman is not empty - this installation may have active lists!'
    echo "---> Restoring \"last_mailman_version\" file"
    [ -d /usr/local/mailman/data ] || /bin/mkdir /usr/local/mailman/data
    /bin/mv -f /var/tmp/last_mailman_version /usr/local/mailman/data/
  fi

  ;;

esac
