# ╭───────────╮ # │ 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}" # ╭──────────╮ # │ internal │ # ╰──────────╯ # _RWX_IFS # ╭─────────╮ # │ private │ # ╰─────────╯ rwx_ifs_set() { _RWX_IFS="${IFS}" IFS=" " } rwx_ifs_unset() { IFS="${_RWX_IFS}" unset RWX_IFS } _rwx_main_log() { if rwx_shell_interactive; then [ ${#} -gt 0 ] || set -- "" local line for line in "${@}"; do echo "${line}" done fi } # ╭────────╮ # │ public │ # ╰────────╯ # find directory’s files by extension rwx_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 rwx_find_sh() { rwx_find_extension "sh" "${@}" } # get functions from file rwx_grep_functions() { local file="${1}" grep "()" "${file}" | cut --delimiter "(" --fields 1 } # output help message rwx_help() { rwx_log \ "rwx_… = functions" \ " a__… = aliases" \ " u__… = user" } # test if active shell is in interactive mode rwx_shell_interactive() { case "${-}" in *i*) ;; *) return 1 ;; esac } rwx_main_source() { local path="${1}" [ -d "${path}" ] || return 1 local cmd count module modules modules="$(rwx_find_sh "${path}" "${SH_MAIN_NAME}")" rwx_ifs_set count=0 _rwx_main_log "" \ ". ${path}" for module in ${modules}; do count=$((count + 1)) _rwx_main_log "$(printf "%02d" "${count}") ${module%.sh}" module="${path}/${module}" # shellcheck disable=SC1090 . "${module}" cmd="$(rwx_grep_functions "${module}")" if [ -n "${cmd}" ]; then [ -n "${CMD}" ] && CMD="${CMD} " CMD="${CMD}${cmd}" fi done rwx_ifs_unset } # ╭──────╮ # │ main │ # ╰──────╯ # run initial steps rwx_main() { # system root if ! rwx_main_source "${SH_ROOT}"; then _rwx_main_log "Not a directory: ${SH_ROOT}" return 1 fi # user root rwx_main_source "${SH_USER}" # run interactive extras if rwx_shell_interactive; then # check format rwx_log rwx_shfmt_check "${SH_ROOT}" # check syntax rwx_log rwx_shellcheck_check "${SH_ROOT}" # help rwx_log rwx_help fi } # run main function rwx_main