#!/bin/sh

set -e

# NOTE: This package name detection will only work on Debian.
# If this prerm script ever starts doing work on Red Hat,
# we'll need to adapt this code accordingly.
script="$(basename "${0}")"
pkg="${script%.prerm}"
systemd_unit="${pkg}.service"

case "${1}" in
    remove)
        if [ -e /run/systemd/system ]; then
            systemctl stop "${systemd_unit}" || true
            systemctl disable "${systemd_unit}" || true
        fi

        # Unit files from Python packages get installed by postinst so
        # we have to remove them explicitly here.
        py_unit="/usr/share/doc/${pkg}/${pkg}.service"
        if [ -e "${py_unit}" ]
        then
            rm "/lib/systemd/system/${pkg}.service" || true
        fi
        ;;
esac