#!/bin/sh

set -e

case $1 in
    configure)
        # Add the "orthanc" user
        if ! getent passwd orthanc > /dev/null; then
            adduser --system --quiet \
                    --home /var/lib/orthanc --no-create-home \
                    --shell /bin/bash --group --gecos "Orthanc Administrator" orthanc
        fi
        if test "`id -u orthanc`" -eq 0; then
            echo "The orthanc administrative user must not be root." >&2
            false
        fi
        if test "`id -g orthanc`" -eq 0; then
            echo "The orthanc administrative group must not be root." >&2
            false
        fi

        # Configure the permissions of the working directories
        chown orthanc:orthanc /var/lib/orthanc
        chown orthanc:orthanc /var/log/orthanc

	chmod 0750 /var/lib/orthanc
	chmod 0750 /var/log/orthanc
	
	# The "credentials.json" contains unencrypted sensitive
	# configuration options ("RegisteredUsers"): It must only be
	# readble by the users running Orthanc.
        chown root:orthanc /etc/orthanc/credentials.json
        chmod 0640 /etc/orthanc/credentials.json

        # Make sure the en_US locale has been generated (required for
        # case-insensitive comparison of strings with accents)
	locale-gen en_US
	locale-gen en_US.UTF-8
	update-locale

	# Start the Orthanc service after installation
        # https://www.debian.org/doc/debian-policy/ch-opersys.html#s9.3.3.2
	if [ -x /etc/init.d/orthanc ]; then
           if which invoke-rc.d >/dev/null 2>&1; then
               invoke-rc.d orthanc start
           else
               /etc/init.d/orthanc start
           fi
        fi
	;;

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

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

#DEBHELPER#
