]> arthur.barton.de Git - sd-tool.git/blob - bin/sd-tool
sd-tool: Set "pipefail" bash shell option
[sd-tool.git] / bin / sd-tool
1 #!/bin/bash
2 #
3 # sd-tool: Helper Tool for systemd
4 # Copyright (c)2023,2024 Alexander Barton (alex@barton.de)
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11
12 set -o pipefail
13
14 BIN_D=$(dirname "$0")
15
16 # Detect our "share" folder ...
17 unset SHARE_D
18 for d in "${BIN_D}" "${BIN_D}"/..; do
19         for s in share share/sd-tool; do
20                 if [[ -r "${d}/${s}/sd-tool-common.inc.sh" ]]; then
21                         SHARE_D="${d}/${s}"
22                         break
23                 fi
24         done
25         [[ -n "${SHARE_D}" ]] && break
26 done
27
28 if [[ -z "${SHARE_D}" || ! -d "${SHARE_D}" ]]; then
29         echo "Oops, failed to detect \"share\" folder of $0! Aborting!" >&2
30         exit 1
31 fi
32
33 # shellcheck source=../share/sd-tool-common.inc.sh
34 if ! . "${SHARE_D}/sd-tool-common.inc.sh"; then
35         echo "Oops, failed to read \"${SHARE_D}/sd-tool-common.inc.sh\"! Aborting!" >&2
36         exit 1
37 fi
38
39 # Try to handle a subcommand.
40 # NOTE: this function does NOT return!
41 handle_subcommand_and_quit() {
42         cmd="$1"
43         cmd_file="${SHARE_D}/sdt-cmd-${cmd}.inc.sh"
44         shift
45         if [[ -r "${cmd_file}" ]]; then
46                 # shellcheck source=../share/sd-tool-common.inc.sh
47                 if ! . "${cmd_file}"; then
48                         printf "Oops, failed to read \"%s\"! Aborting!\n" "${cmd_file}" >&2
49                         exit 1
50                 fi
51                 # Call command:
52                 sdt_cmd_"${cmd}" "$@"
53                 exit $?
54         else
55                 if [[ -n "${cmd}" ]]; then
56                         printf "Unknown subcommand \"%s\"! Available commands are:\n\n" "${cmd}" >&2
57                 else
58                         printf "No subcommand given! Available commands are:\n\n" >&2
59                 fi
60                 sdt_commands
61                 exit 2
62         fi
63 }
64
65 while true; do
66         case "$1" in
67                 "--help")
68                         sdt_version
69                         sdt_usage
70                         exit 0
71                         ;;
72                 "--quiet"|"-q")
73                         unset VERBOSE
74                         export QUIET=1
75                         ;;
76                 "--verbose"|"-v")
77                         unset QUIET
78                         export VERBOSE=1
79                         ;;
80                 "--version"|"-V")
81                         sdt_version
82                         exit 0
83                         ;;
84                 "-*")
85                         printf "Unknown option \"%s\"!\n" "$1" >&2
86                         exit 2
87                         ;;
88                 *)
89                         # Subcommand?
90                         handle_subcommand_and_quit "$@"
91         esac
92         shift
93 done
94 exit 1