#!/bin/sh
# preinst script for logdata-anomaly-miner
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    install)
# Create the user to run the analysis service.
    analysisGroup="aminer"
    if [ "$(getent group "${analysisGroup}")" = "" ]; then
# Add a separate group for aitmon.
# The group does not need to be a system group, but low gid is
# preferable to avoid mixing with user groups. Using '--system'
# flag would cause gid allocation to go down from UID_MIN, not
# up from SYS_GID_MIN, so avoid using --system.
        groupadd -K GID_MIN=100 -K GID_MAX=1000 "${analysisGroup}"
    fi

    analysisUser="aminer"
    if [ "$(getent passwd "${analysisUser}")" = "" ]; then
# Add a system user, set home directory to nonexisting directory
# to avoid loading of user-defined files. Create user without
# using '--system' flag, thus allocating UIDs upwards.
        useradd -M --shell /usr/sbin/nologin --gid "${analysisGroup}" -K PASS_MAX_DAYS=-1 -K UID_MIN=100 -K UID_MAX=999 --home /nonexistent "${analysisUser}"
# There is no way to make useradd ommit assignment of subuids,
# so remove them immediately on affected systems.
        if test -e /etc/subuid; then
            usermod --del-subuids 1-4294967295 --del-subgids 1-4294967295 "${analysisUser}"
        fi
    fi
    ;;
esac

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

#DEBHELPER#

exit 0
