148 lines
2.5 KiB
Bash
148 lines
2.5 KiB
Bash
# ╭───────────╮
|
|
# │ constants │
|
|
# ╰───────────╯
|
|
|
|
SH_MAIN_NAME="main.sh"
|
|
SH_NAME="sh"
|
|
|
|
SH_ROOT="/etc/${SH_NAME}"
|
|
|
|
SH_MAIN="${SH_ROOT}/${SH_MAIN_NAME}"
|
|
|
|
# ╭───────────╮
|
|
# │ variables │
|
|
# ╰───────────╯
|
|
|
|
SH_SHELL="$(cat "/proc/${$}/comm")"
|
|
SH_USER="${HOME}/${SH_NAME}"
|
|
|
|
# ╭─────────╮
|
|
# │ private │
|
|
# ╰─────────╯
|
|
|
|
_sh_ifs_new() {
|
|
SH_IFS="${IFS}"
|
|
IFS="
|
|
"
|
|
}
|
|
|
|
_sh_ifs_pop() {
|
|
IFS="${SH_IFS}"
|
|
unset SH_IFS
|
|
}
|
|
|
|
_sh_main_log() {
|
|
if sh_shell_interactive; then
|
|
[ ${#} -gt 0 ] || set -- ""
|
|
local line
|
|
for line in "${@}"; do
|
|
echo "${line}"
|
|
done
|
|
fi
|
|
}
|
|
|
|
# ╭────────╮
|
|
# │ public │
|
|
# ╰────────╯
|
|
|
|
# find directory’s files by extension
|
|
sh_find_extension() {
|
|
local extension="${1}"
|
|
local root="${2}"
|
|
local file="${3}"
|
|
set -- \
|
|
"${root}" \
|
|
-name "*.${extension}" \
|
|
-type "f"
|
|
[ -n "${file}" ] &&
|
|
set -- "${@}" \
|
|
-not \
|
|
-name "${file}"
|
|
find "${@}" \
|
|
-printf "%P\n" |
|
|
sort
|
|
}
|
|
|
|
# find directory’s sh files
|
|
sh_find_sh() {
|
|
sh_find_extension "sh" "${@}"
|
|
}
|
|
|
|
# get functions from file
|
|
sh_grep_functions() {
|
|
local file="${1}"
|
|
grep "()" "${file}" |
|
|
cut --delimiter "(" --fields 1
|
|
}
|
|
|
|
# output help message
|
|
rwx_help() {
|
|
sh_log \
|
|
"sh_… = shell functions" \
|
|
"a__… = aliases" \
|
|
"u__… = user"
|
|
}
|
|
|
|
# test if active shell is in interactive mode
|
|
sh_shell_interactive() {
|
|
case "${-}" in
|
|
*i*) ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
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 │
|
|
# ╰──────╯
|
|
|
|
# run initial steps
|
|
rwx_main() {
|
|
# system root
|
|
if ! sh_source_directory "${SH_ROOT}"; then
|
|
_sh_main_log "Not a directory: ${SH_ROOT}"
|
|
return 1
|
|
fi
|
|
# user root
|
|
sh_source_directory "${SH_USER}"
|
|
# run interactive extras
|
|
if sh_shell_interactive; then
|
|
# check format
|
|
sh_log
|
|
rwx_shfmt "${SH_ROOT}"
|
|
# check syntax
|
|
sh_log
|
|
rwx_shellcheck_check "${SH_ROOT}"
|
|
# help
|
|
sh_log
|
|
rwx_help
|
|
fi
|
|
}
|
|
|
|
# run main function
|
|
rwx_main
|