rwx/sh/main.sh

136 lines
2.1 KiB
Bash
Raw Normal View History

2024-11-25 17:32:44 +01:00
# constants
2024-11-25 15:09:58 +01:00
SH_MAIN_NAME="main.sh"
2024-11-18 11:41:12 +01:00
SH_NAME="sh"
SH_ROOT="/etc/${SH_NAME}"
2024-11-25 15:09:58 +01:00
SH_MAIN="${SH_ROOT}/${SH_MAIN_NAME}"
2024-11-18 11:41:12 +01:00
2024-11-25 17:32:44 +01:00
# variables
2024-11-25 17:54:40 +01:00
SH_SHELL="$(cat "/proc/${$}/comm")"
2024-11-25 17:32:44 +01:00
SH_USER="${HOME}/${SH_NAME}"
# functions
2024-11-15 16:43:48 +01:00
2024-11-25 16:38:48 +01:00
_sh_ifs_new() {
SH_IFS="${IFS}"
IFS="
"
}
_sh_ifs_pop() {
IFS="${SH_IFS}"
unset SH_IFS
}
2024-11-25 13:28:42 +01:00
sh_source_check() {
2024-11-25 17:17:05 +01:00
local root="${1}"
local file module modules
file="$(mktemp)"
modules="$(sh_source_find "${root}")"
2024-11-25 16:38:48 +01:00
_sh_ifs_new
2024-11-25 15:33:18 +01:00
for module in ${modules}; do
2024-11-25 17:17:05 +01:00
echo ". \"${root}/${module}\"" >>"${file}"
2024-11-25 15:33:18 +01:00
done
2024-11-25 16:38:48 +01:00
_sh_ifs_pop
2024-11-25 13:56:01 +01:00
shellcheck \
--check-sourced \
2024-11-25 17:17:05 +01:00
--enable "all" \
--exclude "3043" \
--external-sources \
--shell "dash" \
2024-11-25 13:56:01 +01:00
"${file}"
2024-11-25 13:28:42 +01:00
rm "${file}"
}
2024-11-25 14:45:16 +01:00
sh_source_find() {
local root="${1}"
local file="${2}"
set -- \
"${root}" \
-name "*.sh" \
-type "f"
[ -n "${file}" ] &&
set -- "${@}" \
-not \
-name "${file}"
find "${@}" \
2024-11-25 14:52:47 +01:00
-printf "%P\n" |
sort
2024-11-25 14:45:16 +01:00
}
2024-11-19 14:11:11 +01:00
_sh_main_commands() {
2024-11-17 18:11:25 +01:00
local file="${1}"
2024-11-17 21:46:01 +01:00
grep "()" "${file}" |
2024-11-17 18:11:25 +01:00
cut --delimiter "(" --fields 1
}
2024-11-20 16:47:41 +01:00
_sh_main_log() {
case "${-}" in
*i*)
2024-11-25 18:22:35 +01:00
[ ${#} -gt 0 ] || set -- ""
2024-11-25 18:20:08 +01:00
local line
for line in "${@}"; do
echo "${line}"
2024-11-20 16:47:41 +01:00
done
;;
*) ;;
esac
}
2024-11-15 19:06:14 +01:00
main_source_directory() {
2024-11-15 19:02:22 +01:00
local path="${1}"
2024-11-25 16:31:59 +01:00
if [ ! -d "${path}" ]; then
2024-11-20 16:47:41 +01:00
_sh_main_log "Not a directory: ${path}"
2024-11-12 10:14:08 +01:00
return 1
fi
2024-11-25 17:17:05 +01:00
local cmd count module modules
2024-11-25 16:31:59 +01:00
modules="$(sh_source_find "${path}" "${SH_MAIN_NAME}")"
2024-11-25 16:38:48 +01:00
_sh_ifs_new
2024-11-25 16:31:59 +01:00
count=0
_sh_main_log "" \
". ${path}"
for module in ${modules}; do
count=$((count + 1))
_sh_main_log "$(printf "%02d" "${count}") ${module%.sh}"
module="${path}/${module}"
# shellcheck disable=SC1090
. "${module}"
cmd="$(_sh_main_commands "${module}")"
if [ -n "${cmd}" ]; then
[ -n "${CMD}" ] && CMD="${CMD}
"
CMD="${CMD}${cmd}"
fi
done
2024-11-25 16:38:48 +01:00
_sh_ifs_pop
2023-05-09 22:15:40 +02:00
}
2023-05-09 22:09:01 +02:00
2024-11-15 19:06:14 +01:00
main_source_file() {
2024-11-15 19:02:22 +01:00
local path="${1}"
if [ -f "${path}" ]; then
2024-11-15 19:11:16 +01:00
main_source_directory "$(dirname "${path}")"
2024-11-15 19:02:22 +01:00
else
2024-11-20 16:47:41 +01:00
_sh_main_log "Not a file: ${path}"
2024-11-15 19:02:22 +01:00
return 1
fi
}
2024-11-25 17:57:41 +01:00
sh_help() {
2024-11-25 18:23:48 +01:00
sh_log "" \
2024-11-21 11:25:40 +01:00
"sh_… = shell functions" \
"a__… = aliases" \
"u__… = user"
}
2024-11-19 12:32:29 +01:00
2024-11-25 18:09:03 +01:00
sh_main() {
2024-11-25 17:57:41 +01:00
main_source_file "${ENV}"
main_source_directory "${SH_USER}"
sh_source_check "$(dirname "${ENV}")"
sh_help
}
2024-11-25 18:09:03 +01:00
sh_main