#!/bin/sh
# Copyright (C) The Arvados Authors. All rights reserved.
#
# SPDX-License-Identifier: AGPL-3.0

set -e

if [ "%{name}" != "%\{name\}" ]; then
    # Red Hat ("%{...}" is interpolated at package build time)
    pkg="%{name}"
    pkgtype=rpm
    prefix="${RPM_INSTALL_PREFIX}"
else
    # Debian
    script="$(basename "${0}")"
    pkg="${script%.prerm}"
    pkgtype=deb
    prefix=/usr
fi

case "${pkgtype}-${1}" in
    deb-remove | rpm-0)
        if [ -e /run/systemd/system ]; then
            systemctl stop "${pkg}" || true
            systemctl disable "${pkg}" || true
        fi
        if [ -e "${prefix}/share/doc/${pkg}/${pkg}.service" ]; then
            # Unit files from Python packages get installed by
            # postinst so we have to remove them explicitly here.
            rm "/lib/systemd/system/${pkg}/${pkg}.service" || true
        fi
        ;;
esac