125 lines
2.8 KiB
Bash
125 lines
2.8 KiB
Bash
# ╭──────┬──────────╮
|
|
# │ self │ commands │
|
|
# ╰──────┴──────────╯
|
|
|
|
# get commands from root
|
|
rwx_self_commands() {
|
|
grep \
|
|
--directories "recurse" \
|
|
--no-filename \
|
|
"^${RWX_SELF_COMMAND}" "${RWX_ROOT_SYSTEM}" |
|
|
cut --delimiter "(" --fields 1 |
|
|
sed "s|^${RWX_SELF_COMMAND}||"
|
|
}
|
|
|
|
# ╭──────┬───────────╮
|
|
# │ self │ functions │
|
|
# ╰──────┴───────────╯
|
|
|
|
# get functions from root
|
|
rwx_self_functions() {
|
|
grep \
|
|
--directories "recurse" \
|
|
--no-filename \
|
|
"()" "${RWX_ROOT_SYSTEM}" |
|
|
cut --delimiter "(" --fields 1
|
|
}
|
|
|
|
# ╭──────┬──────╮
|
|
# │ self │ help │
|
|
# ╰──────┴──────╯
|
|
|
|
# output help message
|
|
rwx_self_help() {
|
|
rwx_log \
|
|
"rwx_… = functions" \
|
|
" a__… = aliases" \
|
|
" u__… = user"
|
|
}
|
|
|
|
# ╭──────┬──────╮
|
|
# │ self │ init │
|
|
# ╰──────┴──────╯
|
|
|
|
rwx_self_init() {
|
|
# 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
|
|
}
|
|
|
|
# ╭──────┬─────────╮
|
|
# │ self │ install │
|
|
# ╰──────┴─────────╯
|
|
|
|
_rwx_cmd_rwx_install() { rwx_self_install "${@}"; }
|
|
rwx_self_install() {
|
|
local target="${1}"
|
|
local command file
|
|
# commands
|
|
local root="${target}/usr/local/bin"
|
|
for command in $(rwx_self_commands); do
|
|
file="${root}/${command}"
|
|
rwx_remove "${file}"
|
|
rwx_link "${file}" "${RWX_MAIN_PATH}"
|
|
done
|
|
# sh
|
|
file="${target}/etc/profile.d/${RWX_SELF_NAME}.sh"
|
|
rwx_remove "${file}"
|
|
rwx_file_write "${file}" "export ENV=\"${RWX_MAIN_PATH}\""
|
|
# bash
|
|
file="/etc/bash.bashrc"
|
|
rwx_remove "${file}"
|
|
rwx_link "${file}" "${RWX_MAIN_PATH}"
|
|
}
|
|
|
|
# ╭──────┬────────╮
|
|
# │ self │ subset │
|
|
# ╰──────┴────────╯
|
|
|
|
rwx_self_subset() {
|
|
local argument path
|
|
for argument in "${@}"; do
|
|
path="${RWX_ROOT_SYSTEM}/${argument}"
|
|
if [ -d "${path}" ]; then
|
|
local file
|
|
for file in $(rwx_find_shell "${path}"); do
|
|
echo "${argument}/${file}"
|
|
done
|
|
elif [ -f "${path}" ]; then
|
|
echo "${argument}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# ╭──────┬───────╮
|
|
# │ self │ write │
|
|
# ╰──────┴───────╯
|
|
|
|
rwx_self_write() {
|
|
local target="${1}"
|
|
if [ -n "${target}" ]; then
|
|
shift
|
|
local file text
|
|
text="#! /usr/bin/env sh
|
|
"
|
|
rwx_ifs_set
|
|
for file in $(rwx_self_subset "${@}"); do
|
|
text="${text}
|
|
$(cat "${RWX_ROOT_SYSTEM}/${file}")
|
|
"
|
|
done
|
|
rwx_ifs_unset
|
|
rwx_file_write "${target}" "${text}"
|
|
rwx_shfmt "${target}"
|
|
rwx_shellcheck_file "${target}"
|
|
fi
|
|
}
|