#! /bin/sh
#
# $FreeBSD: ports/mail/mailman/pkg-install,v 1.8 2006/05/12 18:28:39 jmelo Exp $

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

case $2 in

PRE-INSTALL)
  echo "---> Starting install script:"

  if [ -z "/usr/local/mailman" -o \
       -z "mailman" -o -z "mailman" -o \
       -z "91" -o -z "91" ]; then
    echo "ERROR: A required pragma was empty"
    exit 1
  fi

  # Create group if required
  if pw group show "mailman" >/dev/null 2>&1; then
    echo "---> Using existing group \"mailman\""
  else
    echo "---> Adding group \"mailman\" (91)"
    /usr/sbin/pw groupadd mailman -g 91 || exit 1
  fi

  # Create user if required
  if pw user show "mailman" >/dev/null 2>&1; then
    echo "---> Using existing user \"mailman\""
  else
    echo "---> Adding user \"mailman\" (91)"
    pw useradd "mailman" -u "91" -g "mailman" -h - \
      -d "/usr/local/mailman" -s "/sbin/nologin" -c "Mailman Owner" || exit 1
  fi

  # Create home directory if required
  if [ -d "/usr/local/mailman" ]; then
    echo "---> Using existing Mailman directory (/usr/local/mailman)"
    echo "     (There may be existing active mailing lists - this installation will"
    echo "     attempt to preserve them.)"
  else
    echo "---> Creating Mailman directory (/usr/local/mailman)"
    (umask 002 && /bin/mkdir -p "/usr/local/mailman") || exit 1
    /usr/sbin/chown -R "mailman:mailman" "/usr/local/mailman" || exit 1
    /bin/chmod g+s "/usr/local/mailman" || exit 1
  fi
  ;;

POST-INSTALL)
  echo "---> Starting post-install script:"

  echo "---> Checking crontab(5) file for user \"mailman\""

  if [ -e /var/cron/allow ]; then
  echo "mailman" >> /var/cron/allow
  fi

  if /usr/bin/crontab -u "mailman" -l >/tmp/mmctab$$ 2>&1 ; then
      if test -s /tmp/mmctab$$; then
          echo "---> \"mailman\" already has a crontab.  Not overwriting it"
          echo "---> Please merge any changes from the standard crontab file"
          echo "--->     /usr/local/mailman/cron/crontab.in"
      else
          echo "---> Installing crontab(5) file for user \"mailman\""
          /usr/bin/crontab -u "mailman" "/usr/local/mailman/cron/crontab.in" || exit 1
      fi
  else
      echo "---> Creating crontab(5) file for user \"mailman\""
      /usr/bin/crontab -u "mailman" "/usr/local/mailman/cron/crontab.in" || exit 1
  fi
  rm -f /tmp/mmctab$$

  echo "---> Checking (and fixing) file and directory permissions"
  /usr/local/mailman/bin/check_perms -f >/dev/null 2>&1
  ;;

esac
