rwx/sh/main.sh

145 lines
2.9 KiB
Bash
Raw Permalink Normal View History

2024-12-01 18:51:06 +00:00
#! /usr/bin/env sh
2024-12-02 02:22:30 +00:00
# ╭──────┬───────────╮
# │ main │ constants │
# ╰──────┴───────────╯
2024-11-25 16:32:44 +00:00
2024-11-29 21:26:26 +00:00
RWX_MAIN_NAME="main.sh"
2024-11-29 22:52:46 +00:00
RWX_SELF_NAME="rwx"
2024-11-18 10:41:12 +00:00
2024-12-01 20:48:04 +00:00
RWX_SELF_COMMAND="_${RWX_SELF_NAME}_cmd_"
2024-12-01 20:03:57 +00:00
2024-12-02 02:22:30 +00:00
# ╭──────┬───────────╮
# │ main │ variables │
# ╰──────┴───────────╯
2024-11-25 16:32:44 +00:00
2024-12-02 03:02:26 +00:00
RWX_COMMAND_ARGUMENT="${0}"
2024-12-02 02:05:07 +00:00
RWX_SHELL="$(cat "/proc/${$}/comm")"
2024-12-02 03:02:26 +00:00
RWX_COMMAND_NAME="$(basename "${RWX_COMMAND_ARGUMENT}" |
sed "s|^-||")"
case "${RWX_COMMAND_NAME}" in
"bash" | "dash" | "sh") unset RWX_COMMAND_NAME ;;
*) ;;
esac
2024-12-01 20:28:42 +00:00
RWX_ROOT_SYSTEM="/usr/local/lib/${RWX_SELF_NAME}"
2024-12-01 21:10:05 +00:00
RWX_SELF_USER="${HOME}/${RWX_SELF_NAME}"
2024-11-25 16:32:44 +00:00
2024-11-29 21:26:26 +00:00
RWX_MAIN_PATH="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME}"
2024-12-02 02:22:30 +00:00
# ╭──────┬───────╮
# │ main │ shell │
# ╰──────┴───────╯
2024-11-29 22:03:52 +00:00
2024-11-29 22:56:14 +00:00
# test if active shell is in interactive mode
rwx_shell_interactive() {
case "${-}" in
*i*) ;;
*) return 1 ;;
esac
}
2024-11-29 22:36:32 +00:00
# ╭──────┬─────╮
2024-12-02 02:22:30 +00:00
# │ main │ log │
2024-11-29 22:36:32 +00:00
# ╰──────┴─────╯
__rwx_log() {
if rwx_shell_interactive; then
[ ${#} -gt 0 ] || set -- ""
local line
for line in "${@}"; do
echo "${line}"
done
fi
}
2024-11-29 22:27:43 +00:00
# ╭──────┬──────╮
2024-12-02 02:22:30 +00:00
# │ main │ find │
2024-11-29 22:27:43 +00:00
# ╰──────┴──────╯
2024-11-29 14:42:58 +00:00
# find directory’s files by extension
2024-11-29 18:25:49 +00:00
rwx_find_extension() {
2024-11-25 19:46:09 +00:00
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-29 14:42:58 +00:00
# find directory’s sh files
2024-11-29 22:03:52 +00:00
rwx_find_shell() {
2024-11-29 18:25:49 +00:00
rwx_find_extension "sh" "${@}"
2024-11-25 19:46:09 +00:00
}
2024-11-29 22:27:43 +00:00
# ╭──────┬─────╮
2024-12-02 02:22:30 +00:00
# │ main │ ifs │
2024-11-29 22:27:43 +00:00
# ╰──────┴─────╯
rwx_ifs_set() {
_RWX_IFS="${IFS}"
IFS="
"
}
rwx_ifs_unset() {
IFS="${_RWX_IFS}"
unset RWX_IFS
}
# ╭──────┬────────╮
2024-12-02 02:22:30 +00:00
# │ main │ source │
2024-11-29 22:27:43 +00:00
# ╰──────┴────────╯
rwx_source() {
2024-11-25 21:21:01 +00:00
local path="${1}"
[ -d "${path}" ] ||
return 1
2024-12-02 00:53:34 +00:00
local count module
2024-11-25 21:21:01 +00:00
count=0
2024-11-29 22:36:32 +00:00
__rwx_log "" \
2024-11-25 21:21:01 +00:00
". ${path}"
2024-12-02 00:53:34 +00:00
rwx_ifs_set
for module in $(rwx_find_shell "${path}" "${RWX_MAIN_NAME}"); do
2024-11-25 21:21:01 +00:00
count=$((count + 1))
2024-11-29 22:36:32 +00:00
__rwx_log "$(printf "%02d" "${count}") ${module%.sh}"
2024-11-25 21:21:01 +00:00
module="${path}/${module}"
# shellcheck disable=SC1090
. "${module}"
done
2024-11-29 18:25:49 +00:00
rwx_ifs_unset
2024-11-25 21:21:01 +00:00
}
2024-12-02 02:22:30 +00:00
# ╭──────╮
# │ main │
# ╰──────╯
2024-11-25 21:21:01 +00:00
2024-11-29 14:42:58 +00:00
# run initial steps
2024-11-29 16:13:51 +00:00
rwx_main() {
2024-12-01 19:23:52 +00:00
# system root
if ! rwx_source "${RWX_ROOT_SYSTEM}"; then
__rwx_log "Not a directory: ${RWX_ROOT_SYSTEM}"
return 1
fi
# user root
2024-12-01 21:10:05 +00:00
rwx_source "${RWX_SELF_USER}"
2024-12-02 14:07:55 +00:00
# context / command
if [ -n "${RWX_COMMAND_NAME}" ]; then
"${RWX_SELF_COMMAND}${RWX_COMMAND_NAME}" "${@}"
# context / shell
else
2024-12-02 14:24:27 +00:00
rwx_self_init
2024-12-02 14:07:55 +00:00
fi
2024-11-25 16:57:41 +00:00
}
2024-11-29 14:42:58 +00:00
# run main function
2024-12-01 18:49:14 +00:00
rwx_main "${@}"