#!/usr/bin/env bash
# postinst script for sgx-dcap-pccs
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
        adduser --quiet --system pccs --group --home /opt/intel/sgx-dcap-pccs --no-create-home --shell /bin/bash
        chown -R pccs:pccs /opt/intel/sgx-dcap-pccs/
        if which pm2 > /dev/null
        then
            echo "pm2 is installed, continue ..."
        else
            npm install -g pm2
        fi
        if [ "${DEBIAN_FRONTEND}" != "noninteractive" ]
        then
            /bin/su -c "/opt/intel/sgx-dcap-pccs/install.sh postinst" pccs
            pm2cfg=`/bin/su -c "pm2 startup systemd | grep 'sudo'" pccs` || true
            eval $pm2cfg
        fi
    ;;

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

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
