#!/bin/sh

set -e

case $1 in
  preinst)
    exit 0
    ;;
  postinst)
    if ! getent group named 2>&1 > /dev/null; then
      if groupadd -g 20 named; then
        echo 'Created group named'
      else
        echo 'Failed to create group named!'
        exit 1
      fi
    fi
    if ! getent passwd named 2>&1 > /dev/null; then
      if useradd -c 'BIND Account' -g named \
      -s /bin/false -u 20 named; then
        echo 'Created user named'
      else
        echo 'Failed to create user named!'
        exit 1
      fi
    fi

    if [ -d /var/named ]; then
      chown -R named:named /var/named
    fi

    /etc/init.d/named status 2>&1 > /dev/null && /etc/init.d/named restart

    exit 0
    ;;
  prerm)
    /etc/init.d/named status 2>&1 > /dev/null && /etc/init.d/named stop
    exit 0
    ;;
  postrm)
    exit 0
    ;;
esac
