#!/bin/sh

set -e

case "$1" in
    install|upgrade)
        # We have renamed the previous systemd service name from icingadb-redis-server.service to icingadb-redis-server.service
        # with Icinga DB Redis version 7.0.15, so make sure to clean up any previously created symlinks.
        # Starting with Icinga DB Redis 7.0.15, the icingadb-redis-server unit file is just an alias of the new
        # icingadb-redis.service file and therefore, this should only be the case when upgrading from an older
        # version to an Icinga DB Redis version >= 7.0.15.
        if ! [ -L "/lib/systemd/system/icingadb-redis-server.service" ]; then
          # We need to clean up all systemd files that refer to both names, otherwise cleaning up the systemd files of
          # the first name will not necessarily clean up all symlinks that refer to the second one.
          deb-systemd-helper purge 'icingadb-redis.service' >/dev/null || true
          deb-systemd-helper purge 'icingadb-redis-server.service' >/dev/null || true

          if [ -d /run/systemd/system ]; then
            # Reload the systemd daemon configuration to bring the newly made changes into effect, and the dh
            # system will unmask the just renamed service, re-enable it and then recreate all required symlinks.
            systemctl --system daemon-reload >/dev/null || true
          fi
        fi
    ;;

    abort-upgrade)
    ;;

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

#DEBHELPER#

exit 0
