15370: Fix flaky test.
[arvados.git] / build / go-python-package-scripts / prerm
index c6ec18ca106cb1bba11e761363715fb1ce40c0d2..c0f45d60c698cb77f9c2472d7d18b5d3715b33c9 100755 (executable)
@@ -1,27 +1,35 @@
 #!/bin/sh
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
 
 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"
+# Detect rpm-based systems: the exit code of the following command is zero
+# on rpm-based systems
+if /usr/bin/rpm -q -f /usr/bin/rpm >/dev/null 2>&1; 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 "${1}" in
-    remove)
+case "${pkgtype}-${1}" in
+    deb-remove | rpm-0)
         if [ -e /run/systemd/system ]; then
-            systemctl stop "${systemd_unit}" || true
-            systemctl disable "${systemd_unit}" || true
+            systemctl stop "${pkg}" || true
+            systemctl disable "${pkg}" || 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
+        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