#! /bin/sh

set -e

case "$1" in
  configure)
        if [ -z "$2" ]; then
          # Changed in 1.1.5-1 for new installs (we used to start at S50
          # and stop at K50)  We now start at S50 and stop at K19 so we
          # start after services which may be used and stop before them.
          update-rc.d freeradius start 50 2 3 4 5 . stop 19 0 1 6 . >/dev/null

          # Set up initial permissions on all the freeradius directories

          if ! dpkg-statoverride --list | grep -q /var/run/freeradius$; then
            mkdir -p /var/run/freeradius
            dpkg-statoverride --add --update freerad freerad 0755 /var/run/freeradius
          fi

          if ! dpkg-statoverride --list | grep -q /var/log/freeradius$; then
            mkdir -p /var/log/freeradius
            dpkg-statoverride --add --update freerad freerad 0750 /var/log/freeradius
          fi

          for file in radius.log radwtmp; do
            [ ! -f "/var/log/freeradius/${file}" ] && install -o freerad -g freerad -m 644 /dev/null /var/log/freeradius/${file}
          done

          for file in /etc/freeradius/mods-config/files/pre-proxy \
            /etc/freeradius/mods-config/files/accounting \
            /etc/freeradius/mods-config/preprocess/huntgroups \
            /etc/freeradius/mods-config/preprocess/hints \
            /etc/freeradius/experimental.conf \
            /etc/freeradius/proxy.conf \
            /etc/freeradius/clients.conf
          do
            if ! dpkg-statoverride --list | grep -qw $file$; then
              dpkg-statoverride --add --update root freerad 0640 $file
            fi
          done

          for dir in /etc/freeradius/certs \
            /etc/freeradius/sites-available \
            /etc/freeradius/sites-enabled \
            /etc/freeradius/mods-config/attr_filter \
            /etc/freeradius/policy.d
          do
            if ! dpkg-statoverride --list | grep -qw $dir$; then
              dpkg-statoverride --add --update freerad freerad 2751 $dir
            fi
          done

          action="start"
        else
          action="restart"
        fi

        # Create links for default sites, but only if this is an initial
        # install or an upgrade from before there were links; users may
        # want to remove them...
        if [ -z "$2" ] || dpkg --compare-versions "$2" lt 2.0.4+dfsg-4; then
          for site in default inner-tunnel; do
            if [ ! -e /etc/freeradius/sites-enabled/$site ]; then
              ln -s ../sites-available/$site /etc/freeradius/sites-enabled/$site
            fi
          done
        fi

        if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
          invoke-rc.d freeradius $action || true
        else
          /etc/init.d/freeradius $action
        fi
        ;;

  abort-upgrade)
        if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
          invoke-rc.d freeradius restart || true
        else
          /etc/init.d/freeradius restart
        fi
        ;;

  abort-remove)
        if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
          invoke-rc.d freeradius start || true
        else
          /etc/init.d/freeradius start
        fi
        ;;

  abort-deconfigure)
        ;;
esac

#DEBHELPER#

exit 0
