#! /bin/bash

set -e

case "$1" in
    configure)
        update-rc.d mon-agent defaults
        adduser --system mon-agent --disabled-login --shell /bin/sh --no-create-home --quiet
        usermod -d /usr/share/mon/agent mon-agent
        chown root:root /etc/init.d/mon-agent
        chown -R mon-agent:root /etc/mon-agent
        chown -R mon-agent:root /var/log/mon-agent
        chown -R root:root /usr/share/mon/agent
        chown -h root:root /usr/bin/monstatsd
        chown -h root:root /usr/bin/mon-collector
        chown -h root:root /usr/bin/mon-forwarder

        set +e

        if which invoke-rc.d >/dev/null 2>&1; then
            invoke-rc.d mon-agent restart
        else
            /etc/init.d/mon-agent restart
        fi

        RETVAL=$?
        if [ $RETVAL -ne 0 ]; then
          if [ $RETVAL -eq 3 ]; then
            # No agent.conf file is present. The user is probably following
            # the step-by-step instructions and will add the config file next.
            echo "No /etc/mon-agent/agent.conf found, exiting"
            exit 0
          else
            exit $?
          fi
        fi
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
    ;;
esac

#DEBHELPER#

exit 0
