LibreSSL: fixes pulled from OpenBSD patchset
[exim.git] / configs / system-integration / systemd / install
1 #!/bin/bash
2 # simple helper, mainly for testing the provided Systemd units.
3
4 set -eu
5 export LC_ALL=C
6
7 : ${EXIM=exim}
8 : ${EXIM_LOGDIR=/var/log/exim}
9 : ${EXIM_SPOOLDIR=/var/spool/exim}
10
11 # Packagers should install to $(systemd-path systemd-system-unit)
12 # which mostly is something like /lib/systemd/system
13 dstdir=
14
15 usage="$0 [OPTIONS] variant...
16   This simple script installs Systemd unit files to the desired destination, replacing
17   the {{Placeholder}}s.
18
19   VARIANT: one of daemon, inet, socket, maintainance, queuerunner
20
21   OPTIONS:
22   --help          print this help and exit cleanly
23   --uninstall|-u  uninstall the installed files
24   --dstdir|-d DIR the destination directory (mandatory, use 'DEFAULT'
25                   to use Systemd's default location (`systemd-path systemd-system-conf`)
26
27   Placeholders:
28   {{exim}} from \$EXIM ($EXIM)
29   {{logdir}} from \$EXIM_LOGDIR ($EXIM_LOGDIR)
30   {{spooldir}} from \$EXIM_SPOOLDIR ($EXIM_SPOOLDIR)
31 "
32
33
34 tmp=$(getopt -n $0 -o d:n --long dstdir:,help,uninstall -- "$@")
35 eval set -- "$tmp"
36 while true
37 do
38         o=$1; shift
39         case $o in
40                 -d|--dstdir) dstdir=$1; shift;;
41                 --help) echo "$usage"; exit;;
42                 -n|--uninstall) uninstall=1;;
43                 --) break
44         esac
45 done
46
47 if [[ -v uninstall ]]
48 then
49         if ! [[ -r .installed ]]
50         then
51                 echo "$0: noting to uninstall (.installed is empty or isn't readable)" >&2
52                 exit
53         fi
54
55         rm -vf $(<.installed)
56         rm -f .installed
57         exit
58 fi
59
60 case $dstdir in
61         DEFAULT) dstdir=$(systemd-path systemd-system-conf);;
62         "") echo "$0: --dstdir is mandatory" >&2; exit 1;;
63         *) ;;
64 esac
65
66 if (( $# == 0 ))
67 then echo "$0: need variant" >&2; exit 1;
68 fi
69
70 function xform() {
71         sed -e "s|{{exim}}|${EXIM:?}|g" \
72             -e "s|{{logdir}}|${EXIM_LOGDIR:?}|g" \
73             -e "s|{{spooldir}}|${EXIM_SPOOLDIR:?}|g"
74 }
75
76 for dir in ${@:?need source dir(s)}
77 do
78                 echo "# $dir"
79                 for src in "$dir"/*
80                 do
81                         dst="$dstdir/${src##*/}"
82                         echo "installing $dst"
83                         xform <"$src" >"$dst"
84                         echo $dst >> .installed
85                 done
86 done
87
88 if [[ $dstdir == $(systemd-path systemd-system-conf) ]]
89 then
90         echo "# reloading systemd configuration"
91         systemctl daemon-reload
92 fi