#!/bin/sh
set -e

# monagent/ installs root-owned; the agent runs as site24x7-agent. Create the
# system user/group on first install, then hand the entire monagent tree to the
# agent on every configure (covers re-installs and upgrades where files were
# re-extracted root-owned). dpkg stores no per-file owner metadata for non-root
# users, so ownership must be applied here rather than baked into the package.

case "$1" in
    configure)
        if ! getent group site24x7-group >/dev/null; then
            addgroup --system site24x7-group
        fi
        if ! getent passwd site24x7-agent >/dev/null; then
            adduser --system --ingroup site24x7-group \
                    --home /opt/site24x7/monagent --no-create-home \
                    --shell /bin/bash --gecos "Site24x7 Monitoring Agent" \
                    site24x7-agent
        fi
        if [ -d /opt/site24x7/monagent ]; then
            chown -R site24x7-agent:site24x7-group /opt/site24x7/monagent
        fi
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        ;;
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#

exit 0
