rwx/sh/self.sh
2025-07-08 06:17:21 +02:00

50 lines
1.1 KiB
Bash

# ╭──────╮
# │ self │
# ╰──────╯
# ╭──────┬────────╮
# │ self │ subset │
# ╰──────┴────────╯
# TODO move to code module
rwx_self_subset() {
local argument file root
for argument in "${@}"; do
root="${RWX_ROOT_SYSTEM}/${argument}"
file="${argument}.sh"
if [ -d "${root}" ]; then
local file
for file in $(rwx_find_shell "${root}"); do
echo "${argument}/${file}"
done
elif [ -f "${RWX_ROOT_SYSTEM}/${file}" ]; then
echo "${file}"
fi
done
}
# ╭──────┬───────╮
# │ self │ write │
# ╰──────┴───────╯
# TODO move to code module
rwx_self_write() {
local target="${1}"
if [ -n "${target}" ]; then
shift
local file files text
text="#! /usr/bin/env sh
"
files="$(rwx_self_subset "${@}")"
while IFS= read -r file; do
text="${text}
$(cat "${RWX_ROOT_SYSTEM}/${file}")
"
done <<EOF
${files}
EOF
rwx_file_write "${target}" "${text}"
rwx_shfmt "${target}"
rwx_shellcheck_file "${target}"
fi
}