#!/bin/sh

set -e

case $1 in
  preinst)
    exit 0
    ;;
  postinst)
    getent group postfix 2>&1 > /dev/null || groupadd -g 32 postfix
    getent group postdrop 2>&1 > /dev/null || groupadd -g 33 postdrop
    getent passwd postfix 2>&1 > /dev/null || useradd -c "Postfix Daemon" \
      -g postfix -d /dev/null -s /bin/false -u 32 postfix

    if [ ! -f /etc/aliases ]; then
      printf "postmaster: root\nMAILER-DAEMON: root" > /etc/aliases
      newaliases
    fi

    for f in /usr/share/postfix/etc/*; do
      if [ ! -f /etc/postfix/`basename $f` ]; then
        cp -a $f /etc/postfix
      fi
    done

    chown -R postfix.postdrop /var/spool/postfix/* /var/lib/postfix
    chown root.root /var/spool/postfix/pid
    chgrp postdrop /usr/sbin/postqueue \
      /usr/sbin/postdrop \
      /var/spool/postfix/public \
      /var/spool/postfix/maildrop
    chmod 2555 /usr/sbin/postqueue \
      /usr/sbin/postdrop
    ;;
  prerm)
    exit 0
    ;;
  postrm)
    exit 0
    ;;
esac
