# ╭───────────╮ # │ constants │ # ╰───────────╯ RWX_MAIN_NAME="main.sh" RWX_SELF_NAME="rwx" # ╭───────────╮ # │ variables │ # ╰───────────╯ RWX_ROOT_USER="${HOME}/${RWX_SELF_NAME}" RWX_SHELL="$(cat "/proc/${$}/comm")" RWX_SYSTEM_ROOT="/usr/local/lib" RWX_MAIN_PATH="${RWX_ROOT_SYSTEM}/${RWX_MAIN_NAME}" RWX_ROOT_SYSTEM="${RWX_SYSTEM_ROOT}/${RWX_SELF_NAME}" # ╭────────╮ # │ public │ # ╰────────╯ # get functions from file rwx_grep_functions() { local file="${1}" grep "()" "${file}" | cut --delimiter "(" --fields 1 } # ╭──────╮ # │ core │ # ╰──────╯ # test if active shell is in interactive mode rwx_shell_interactive() { case "${-}" in *i*) ;; *) return 1 ;; esac } # ╭──────┬─────╮ # │ core │ log │ # ╰──────┴─────╯ __rwx_log() { if rwx_shell_interactive; then [ ${#} -gt 0 ] || set -- "" local line for line in "${@}"; do echo "${line}" done fi } # ╭──────┬──────╮ # │ core │ find │ # ╰──────┴──────╯ # 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_shell() { rwx_find_extension "sh" "${@}" } # ╭──────┬─────╮ # │ core │ ifs │ # ╰──────┴─────╯ rwx_ifs_set() { _RWX_IFS="${IFS}" IFS=" " } rwx_ifs_unset() { IFS="${_RWX_IFS}" unset RWX_IFS } # ╭──────┬────────╮ # │ core │ source │ # ╰──────┴────────╯ rwx_source() { local path="${1}" [ -d "${path}" ] || return 1 local cmd count module modules modules="$(rwx_find_shell "${path}" "${RWX_MAIN_NAME}")" rwx_ifs_set count=0 __rwx_log "" \ ". ${path}" for module in ${modules}; do count=$((count + 1)) __rwx_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 } # ╭──────┬──────╮ # │ core │ main │ # ╰──────┴──────╯ # run initial steps rwx_main() { # system root if ! rwx_source "${RWX_ROOT_SYSTEM}"; then __rwx_log "Not a directory: ${RWX_ROOT_SYSTEM}" return 1 fi # user root rwx_source "${RWX_ROOT_USER}" # run interactive extras if rwx_shell_interactive; then # check format rwx_log rwx_shfmt "${RWX_ROOT_SYSTEM}" # check syntax rwx_log rwx_shellcheck "${RWX_ROOT_SYSTEM}" # help rwx_log rwx_self_help fi } # run main function rwx_main