]> arthur.barton.de Git - sd-tool.git/blob - bin/sd-tool
550e8d8019362f8646f2ee1b48c09c1d5da2d1b7
[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 BIN_D=$(dirname "$0")
13
14 # Detect our "share" folder ...
15 unset SHARE_D
16 for d in "${BIN_D}" "${BIN_D}"/..; do
17         for s in share share/sd-tool; do
18                 if [[ -r "${d}/${s}/sd-tool-common.inc.sh" ]]; then
19                         SHARE_D="${d}/${s}"
20                         break
21                 fi
22         done
23         [[ -n "${SHARE_D}" ]] && break
24 done
25
26 if [[ -z "${SHARE_D}" || ! -d "${SHARE_D}" ]]; then
27         echo "Oops, failed to detect \"share\" folder of $0! Aborting!" >&2
28         exit 1
29 fi
30
31 # shellcheck source=../share/sd-tool-common.inc.sh
32 if ! . "${SHARE_D}/sd-tool-common.inc.sh"; then
33         echo "Oops, failed to read \"${SHARE_D}/sd-tool-common.inc.sh\"! Aborting!" >&2
34         exit 1
35 fi
36
37 # Try to handle a subcommand.
38 # NOTE: this function does NOT return!
39 handle_subcommand_and_quit() {
40         cmd="$1"
41         cmd_file="${SHARE_D}/sdt-cmd-${cmd}.inc.sh"
42         shift
43         if [[ -r "${cmd_file}" ]]; then
44                 # shellcheck source=../share/sd-tool-common.inc.sh
45                 if ! . "${cmd_file}"; then
46                         printf "Oops, failed to read \"%s\"! Aborting!\n" "${cmd_file}" >&2
47                         exit 1
48                 fi
49                 # Call command:
50                 sdt_cmd_"${cmd}" "$@"
51                 exit $?
52         else
53                 if [[ -n "${cmd}" ]]; then
54                         printf "Unknown subcommand \"%s\"! Available commands are:\n\n" "${cmd}" >&2
55                 else
56                         printf "No subcommand given! Available commands are:\n\n" >&2
57                 fi
58                 sdt_commands
59                 exit 2
60         fi
61 }
62
63 while true; do
64         case "$1" in
65                 "--help")
66                         sdt_version
67                         sdt_usage
68                         exit 0
69                         ;;
70                 "--quiet"|"-q")
71                         unset VERBOSE
72                         export QUIET=1
73                         ;;
74                 "--verbose"|"-v")
75                         unset QUIET
76                         export VERBOSE=1
77                         ;;
78                 "--version"|"-V")
79                         sdt_version
80                         exit 0
81                         ;;
82                 "-*")
83                         printf "Unknown option \"%s\"!\n" "$1" >&2
84                         exit 2
85                         ;;
86                 *)
87                         # Subcommand?
88                         handle_subcommand_and_quit "$@"
89         esac
90         shift
91 done
92 exit 1