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