Fix the detection of rpm vs deb systems.
[arvados.git] / build / go-python-package-scripts / postinst
1 #!/bin/sh
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 # Detect rpm-based systems: the exit code of the following command is zero
7 # on rpm-based systems
8 /usr/bin/rpm -q -f /usr/bin/rpm >/dev/null 2>&1
9
10 if [ "$?" = "0" ]; then
11     # Red Hat ("%{...}" is interpolated at package build time)
12     pkg="%{name}"
13     pkgtype=rpm
14     prefix="${RPM_INSTALL_PREFIX}"
15 else
16     # Debian
17     script="$(basename "${0}")"
18     pkg="${script%.postinst}"
19     pkgtype=deb
20     prefix=/usr
21 fi
22
23 set -e
24 case "${pkgtype}-${1}" in
25     deb-configure | rpm-1)
26         dest_dir="/lib/systemd/system"
27         if ! [ -d "${dest_dir}" ]; then
28             exit 0
29         fi
30
31         # Find the unit file we need to install.
32         unit_file="${pkg}.service"
33         for dir in \
34             "${prefix}/share/doc/${pkg}" \
35             "${dest_dir}"; do
36             if [ -e "${dir}/${unit_file}" ]; then
37                 src_dir="${dir}"
38                 break
39             fi
40         done
41         if [ -z "${src_dir}" ]; then
42             echo >&2 "WARNING: postinst script did not find ${unit_file} anywhere."
43             exit 0
44         fi
45
46         # Install/update the unit file if necessary.
47         if [ "${src_dir}" != "${dest_dir}" ]; then
48             cp "${src_dir}/${unit_file}" "${dest_dir}/" || exit 0
49         fi
50
51         # Enable service, and make sure systemd re-reads the unit
52         # file, in case we changed it.
53         if [ -e /run/systemd/system ]; then
54             systemctl daemon-reload || true
55             eval "$(systemctl -p UnitFileState show "${pkg}")"
56             case "${UnitFileState}" in
57                 disabled)
58                     # Failing to enable or start the service is not a
59                     # package error, so don't let errors here
60                     # propagate up.
61                     systemctl enable "${pkg}" || true
62                     systemctl start "${pkg}" || true
63                     ;;
64                 enabled)
65                     systemctl reload-or-try-restart "${pkg}" || true
66                     ;;
67             esac
68         fi
69         ;;
70 esac