120 lines
2 KiB
Bash
120 lines
2 KiB
Bash
SH_MAIN_NAME="main.sh"
|
|
SH_NAME="sh"
|
|
SH_SHELL="$(cat /proc/${$}/comm)"
|
|
|
|
SH_ROOT="/etc/${SH_NAME}"
|
|
SH_USER="${HOME}/${SH_NAME}"
|
|
|
|
SH_MAIN="${SH_ROOT}/${SH_MAIN_NAME}"
|
|
|
|
[ -n "${ENV}" ] || export ENV="${SH_MAIN}"
|
|
|
|
_sh_ifs_new() {
|
|
SH_IFS="${IFS}"
|
|
IFS="
|
|
"
|
|
}
|
|
|
|
_sh_ifs_pop() {
|
|
IFS="${SH_IFS}"
|
|
unset SH_IFS
|
|
}
|
|
|
|
sh_source_check() {
|
|
local file=".shellcheck.sh"
|
|
local module modules
|
|
modules="$(sh_source_find "." "${file}")"
|
|
rm --force "${file}"
|
|
_sh_ifs_new
|
|
for module in ${modules}; do
|
|
echo ". \"${module}\"" >>"${file}"
|
|
done
|
|
_sh_ifs_pop
|
|
shellcheck \
|
|
--check-sourced \
|
|
"${file}"
|
|
rm "${file}"
|
|
}
|
|
|
|
sh_source_find() {
|
|
local root="${1}"
|
|
local file="${2}"
|
|
set -- \
|
|
"${root}" \
|
|
-name "*.sh" \
|
|
-type "f"
|
|
[ -n "${file}" ] &&
|
|
set -- "${@}" \
|
|
-not \
|
|
-name "${file}"
|
|
find "${@}" \
|
|
-printf "%P\n" |
|
|
sort
|
|
}
|
|
|
|
_sh_main_commands() {
|
|
local file="${1}"
|
|
grep "()" "${file}" |
|
|
cut --delimiter "(" --fields 1
|
|
}
|
|
|
|
_sh_main_log() {
|
|
case "${-}" in
|
|
*i*)
|
|
local argument
|
|
for argument in "${@}"; do
|
|
echo "${argument}"
|
|
done
|
|
;;
|
|
*) ;;
|
|
esac
|
|
}
|
|
|
|
main_source_directory() {
|
|
local path="${1}"
|
|
if [ ! -d "${path}" ]; then
|
|
_sh_main_log "Not a directory: ${path}"
|
|
return 1
|
|
fi
|
|
local cmd count ifs module modules
|
|
modules="$(sh_source_find "${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_main_commands "${module}")"
|
|
if [ -n "${cmd}" ]; then
|
|
[ -n "${CMD}" ] && CMD="${CMD}
|
|
"
|
|
CMD="${CMD}${cmd}"
|
|
fi
|
|
done
|
|
_sh_ifs_pop
|
|
}
|
|
|
|
main_source_file() {
|
|
local path="${1}"
|
|
if [ -f "${path}" ]; then
|
|
main_source_directory "$(dirname "${path}")"
|
|
else
|
|
_sh_main_log "Not a file: ${path}"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
_sh_main() {
|
|
main_source_file "${ENV}"
|
|
main_source_directory "${SH_USER}"
|
|
_sh_main_log "" \
|
|
"sh_… = shell functions" \
|
|
"a__… = aliases" \
|
|
"u__… = user"
|
|
}
|
|
|
|
_sh_main
|