rwx/sh/main.sh

118 lines
1.7 KiB
Bash
Raw Normal View History

2024-11-25 16:32:44 +00:00
# constants
2024-11-25 14:09:58 +00:00
SH_MAIN_NAME="main.sh"
2024-11-18 10:41:12 +00:00
SH_NAME="sh"
SH_ROOT="/etc/${SH_NAME}"
2024-11-25 14:09:58 +00:00
SH_MAIN="${SH_ROOT}/${SH_MAIN_NAME}"
2024-11-18 10:41:12 +00:00
2024-11-25 16:32:44 +00:00
# variables
2024-11-25 16:54:40 +00:00
SH_SHELL="$(cat "/proc/${$}/comm")"
2024-11-25 16:32:44 +00:00
SH_USER="${HOME}/${SH_NAME}"
2024-11-25 17:33:23 +00:00
# private
2024-11-15 15:43:48 +00:00
2024-11-25 15:38:48 +00:00
_sh_ifs_new() {
SH_IFS="${IFS}"
IFS="
"
}
_sh_ifs_pop() {
IFS="${SH_IFS}"
unset SH_IFS
}
2024-11-25 17:33:23 +00:00
_sh_main_log() {
case "${-}" in
*i*)
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
;;
*) ;;
esac
}
2024-11-25 17:39:25 +00:00
# public
2024-11-25 19:46:09 +00:00
sh_find_extension() {
local extension="${1}"
local root="${2}"
local file="${3}"
2024-11-25 19:40:57 +00:00
set -- \
"${root}" \
2024-11-25 19:46:09 +00:00
-name "*.${extension}" \
2024-11-25 19:40:57 +00:00
-type "f"
[ -n "${file}" ] &&
set -- "${@}" \
-not \
-name "${file}"
find "${@}" \
-printf "%P\n" |
sort
}
2024-11-25 19:46:09 +00:00
sh_find_sh() {
sh_find_extension "sh" "${@}"
}
2024-11-25 20:00:14 +00:00
sh_grep_functions() {
local file="${1}"
grep "()" "${file}" |
cut --delimiter "(" --fields 1
}
2024-11-25 16:57:41 +00:00
sh_help() {
2024-11-25 17:25:13 +00:00
sh_log \
2024-11-21 10:25:40 +00:00
"sh_… = shell functions" \
"a__… = aliases" \
"u__… = user"
}
2024-11-19 11:32:29 +00:00
2024-11-25 21:21:01 +00:00
sh_source_directory() {
local path="${1}"
[ -d "${path}" ] ||
return 1
local cmd count module modules
modules="$(sh_find_sh "${path}" "${SH_MAIN_NAME}")"
_sh_ifs_new
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_grep_functions "${module}")"
if [ -n "${cmd}" ]; then
[ -n "${CMD}" ] && CMD="${CMD}
"
CMD="${CMD}${cmd}"
fi
done
_sh_ifs_pop
}
# main
2024-11-25 17:09:03 +00:00
sh_main() {
2024-11-25 21:21:01 +00:00
if ! sh_source_directory "${SH_ROOT}"; then
2024-11-25 17:51:24 +00:00
_sh_main_log "Not a directory: ${SH_ROOT}"
return 1
fi
2024-11-25 21:21:01 +00:00
sh_source_directory "${SH_USER}"
2024-11-25 17:25:13 +00:00
sh_log
2024-11-25 17:48:22 +00:00
sh_shellcheck "${SH_ROOT}"
2024-11-25 17:25:13 +00:00
sh_log
2024-11-25 16:57:41 +00:00
sh_help
}
2024-11-25 17:09:03 +00:00
sh_main