#!/bin/sh
# postinst script for scpdiscord.
#
# 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 https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.


case "$1" in
  configure)
    getent group scpdiscord > /dev/null || groupadd scpdiscord
    getent passwd scpdiscord > /dev/null || useradd -r -m -d /var/lib/scpdiscord -s /sbin/nologin -g scpdiscord scpdiscord

    install -m 770 -o scpdiscord -g scpdiscord -d /var/lib/scpdiscord
    install -m 755 -o scpdiscord -g scpdiscord -d /var/log/scpdiscord
    install -m 770 -o scpdiscord -g scpdiscord -d /etc/scpdiscord

    install -m 644 -o root -g root /usr/share/scpdiscord/default_config.yml
    install -m 755 -o root -g root /usr/bin/scpdiscord

    SYSTEMD_VERSION=$(systemctl --version | awk '{if($1=="systemd" && $2~"^[0-9]"){print $2}}' | head -n 1)
    if [ -n "$SYSTEMD_VERSION" ] && [ "$SYSTEMD_VERSION" -lt 253 ]; then
        echo "Systemd version is lower than 253 ($SYSTEMD_VERSION); using legacy service type 'notify' instead of 'notify-reload'"
        sed -i 's/^Type=notify-reload$/Type=notify/' "/usr/lib/systemd/system/scpdiscord@.service"
    fi

    systemctl daemon-reload
  ;;

  abort-upgrade|abort-remove|abort-deconfigure)
    systemctl daemon-reload
  ;;

  *)
    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
