#!/bin/bash # # sd-tool: Helper Tool for systemd # Copyright (c)2023,2024 Alexander Barton (alex@barton.de) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Detect our "share" folder ... unset SHARE_D for d in . .. /usr/local /usr; do for s in share share/sd-tool; do if [[ -r "${d}/${s}/sd-tool-common.inc.sh" ]]; then SHARE_D="${d}/${s}" break fi done [[ -n "${SHARE_D}" ]] && break done if [[ -z "${SHARE_D}" || ! -d "${SHARE_D}" ]]; then echo "Oops, failed to detect \"share\" folder of $0! Aborting!" >&2 exit 1 fi # shellcheck source=../share/sd-tool-common.inc.sh if ! . "${SHARE_D}/sd-tool-common.inc.sh"; then echo "Oops, failed to read \"${SHARE_D}/sd-tool-common.inc.sh\"! Aborting!" >&2 exit 1 fi # Try to handle a subcommand. # NOTE: this function does NOT return! handle_subcommand_and_quit() { cmd="$1" cmd_file="${SHARE_D}/sdt-cmd-${cmd}.inc.sh" shift if [[ -r "${cmd_file}" ]]; then # shellcheck source=../share/sd-tool-common.inc.sh if ! . "${cmd_file}"; then printf "Oops, failed to read \"%s\"! Aborting!\n" "${cmd_file}" >&2 exit 1 fi # Call command: sdt_cmd_"${cmd}" "$@" exit $? else if [[ -n "${cmd}" ]]; then printf "Unknown subcommand \"%s\"! Available commands are:\n\n" "${cmd}" >&2 else printf "No subcommand given! Available commands are:\n\n" >&2 fi sdt_commands exit 2 fi } while true; do case "$1" in "--help") sdt_version sdt_usage exit 0 ;; "--quiet"|"-q") unset VERBOSE export QUIET=1 ;; "--verbose"|"-v") unset QUIET export VERBOSE=1 ;; "--version"|"-V") sdt_version exit 0 ;; "-*") printf "Unknown option \"%s\"!\n" "$1" >&2 exit 2 ;; *) # Subcommand? handle_subcommand_and_quit "$@" esac shift done exit 1