#!/bin/sh
# postinst script for CLIENTPACKAGENAME

set -e

case "$1" in
    configure|reconfigure)
        if ! getent passwd natp-ssh>/dev/null; then
          # should be a --system user for something run out of cron like
          # like this but keeping the legacy CAIDA pattern for now
          if [ -f /opt/CLIENTPACKAGENAME/.ssh/env ] ;then
            . /opt/CLIENTPACKAGENAME/.ssh/env
            P=${SERVERPORT}
            groupadd --gid ${P} natp-ssh
            useradd --comment "SSH for NAT reverse tunnel" \
              --uid ${P} --gid ${P} --home-dir /opt/CLIENTPACKAGENAME \
              --no-create-home --no-user-group --shell /bin/bash \
              --password '*' natp-ssh
          fi # conf exists
        fi # user does not exist
        chown --recursive --no-dereference natp-ssh: /opt/CLIENTPACKAGENAME/.ssh
        /etc/cron.daily/natp-dns-update.sh || true
        systemctl daemon-reload
        systemctl enable CLIENTPACKAGENAME 
        # note that this will start it if it isn't running but won't
        # restart it if it is already running.
        systemctl start CLIENTPACKAGENAME
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0
