#!/bin/sh

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

# $1 = version of the package being upgraded.
install() {

    TURNSERVER_GROUP=turnserver
    if ! getent group "$TURNSERVER_GROUP" >/dev/null; then
        addgroup --system "$TURNSERVER_GROUP" || exit 1
    fi

    TURNSERVER_USER=turnserver
    if ! getent passwd "$TURNSERVER_USER" >/dev/null; then
        adduser --system \
           --home / \
           --shell /bin/false \
           --no-create-home \
           --ingroup "$TURNSERVER_GROUP" \
           --disabled-password \
           --disabled-login \
           --gecos "turnserver daemon" \
               "$TURNSERVER_USER" || exit 1
    fi

    # Don't ship the empty database; generate it on-demand. (#929269)
    TURNDB_DIR=/var/lib/turn
    TURNDB_SQLITE=$TURNDB_DIR/turndb
    TURNDB_SCHEMA=/usr/share/coturn/schema.sql

    if [ ! -e "$TURNDB_SQLITE" ] && [ -e "$TURNDB_SCHEMA" ]; then
        echo "I: Creating $TURNDB_SQLITE from $TURNDB_SCHEMA" >&2
        ( umask 007 && sqlite3 "$TURNDB_SQLITE" < "$TURNDB_SCHEMA" )
        chown "$TURNSERVER_USER":"$TURNSERVER_GROUP" "$TURNDB_SQLITE"
    fi
    if ! dpkg-statoverride --list "$TURNDB_DIR" >/dev/null 2>&1; then
        dpkg-statoverride --update --add "$TURNSERVER_USER" "$TURNSERVER_GROUP" 775 "$TURNDB_DIR"
    fi

    TURNSERVER_CONFIG=/etc/turnserver.conf
    if ! dpkg-statoverride --list "$TURNSERVER_CONFIG" >/dev/null 2>&1; then
        dpkg-statoverride --update --add root "$TURNSERVER_GROUP" 640 "$TURNSERVER_CONFIG"
    fi
}

case "$1" in
    configure)
        install "$2"
        ;;

    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.

# Automatically added by dh_installinit/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -z "${DPKG_ROOT:-}" ] && [ -x "/etc/init.d/coturn" ]; then
		update-rc.d coturn defaults >/dev/null
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		invoke-rc.d --skip-systemd-native coturn $_dh_action || exit 1
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'coturn.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'coturn.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'coturn.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'coturn.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -d /run/systemd/system ]; then
		systemctl --system daemon-reload >/dev/null || true
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		deb-systemd-invoke $_dh_action 'coturn.service' >/dev/null || true
	fi
fi
# End automatically added section


exit 0

