#!/bin/sh
# lo-up — self-contained lok8s installer (argsh runtime bundled).
#   curl -fsSL https://get.lok8s.io | sh
# Built from install/lo-up via `install/build` (argsh minify). Do not edit by hand.
#
# POSIX preamble: the bundle below is bash. Re-exec it under bash from a real
# FILE whenever we're not already running bash-on-a-file — i.e. under a POSIX
# /bin/sh (no $BASH_VERSION) OR piped with no script file ($0 isn't a file, as
# with `curl … | sh`). The latter matters even when /bin/sh IS bash: a bundle
# read from stdin has no ${BASH_SOURCE[0]}, which trips `set -u`. POSIX
# guarantees the shell hasn't read past this command, so `cat` gets exactly the
# bash bundle that follows.
if [ -z "${BASH_VERSION:-}" ] || [ ! -f "${0}" ]; then
  command -v bash >/dev/null 2>&1 || { echo "lo-up: bash is required (install bash and retry)" >&2; exit 1; }
  _f=$(mktemp 2>/dev/null || echo "/tmp/lo-up.$$") || exit 1
  cat >"$_f"; bash "$_f" "$@"; _r=$?; rm -f "$_f"; exit "$_r"
fi
set -euo pipefail; ARGSH_COMMIT_SHA="a7b0295"; ARGSH_VERSION="v0.10.0-30-ga7b0295"
# Dispatch is lo-up's own tail (the `|| main "$@"` already in the bundle below);
# we deliberately do NOT append argsh::shebang afterwards — it would re-dispatch
# once main has returned and trip on an obfuscated unbound under `set -u`.
: "${ARGSH_VERSION:=unknown}";: "${ARGSH_COMMIT_SHA:=unknown}";: "${ARGSH_FIELD_WIDTH:=24}";COMMANDNAME=("$(a90="${ARGSH_SOURCE:-"${0}"}"; echo "${a90##*/}")");declare -ga __ARGSH_BUILTINS=(:usage :usage::help :usage::completion :usage::docgen :usage::mcp :args is::array is::uninitialized is::set is::tty args::field_name to::int to::float to::boolean to::file to::string lib::pull lib::push import import::clear);declare -g __ARGSH_LIB_DIR="${BASH_SOURCE[0]%/*}";__argsh_try_builtin() { local _so _d;local -r _n="argsh.so";for _so in "${ARGSH_BUILTIN_PATH:-}" "${PATH_LIB:+${PATH_LIB}/${_n}}" "${PATH_BIN:+${PATH_BIN}/${_n}}" ;do [[ -n "${_so}" && -f "${_so}" ]] || continue;enable -f "${_so}" "${__ARGSH_BUILTINS[@]}" 2>/dev/null || continue;return 0;done;for _d in "${LD_LIBRARY_PATH:-}" "${BASH_LOADABLES_PATH:-}";do [[ -n "${_d}" ]] || continue;local IFS=:;for _so in ${_d};do [[ -n "${_so}" && -f "${_so}/${_n}" ]] || continue;enable -f "${_so}/${_n}" "${__ARGSH_BUILTINS[@]}" 2>/dev/null || continue;return 0;done;done;return 1;};if [[ -z "${ARGSH_BUILTIN+x}" ]];then declare -gi ARGSH_BUILTIN=0;if __argsh_try_builtin;then ARGSH_BUILTIN=1;fi;else declare -gi ARGSH_BUILTIN="${ARGSH_BUILTIN}";fi;if (( ARGSH_BUILTIN ));then ARGSH_BUILTIN=1;unset -f import 2>/dev/null || true;[[ "$(type -t import 2>/dev/null)" == "builtin" ]] || import() { declare -A _i; (( ${_i[${1}]:-} )) || { _i[${1}]=1; . "${BASH_SOURCE[0]%/*}/${1}.sh"; } };fi;if (( ARGSH_BUILTIN ));then unset -f is::array is::uninitialized is::set is::tty args::field_name to::int to::float to::boolean to::file to::string 2>/dev/null || true;fi;if ! (( ARGSH_BUILTIN ));then :usage::help() { local a46="${1}"; shift;local -a a47=("${@}");:usage::text "${a46}";exit 0;};fi;if ! (( ARGSH_BUILTIN ));then :usage::suggest() { local a38="${1}"; shift;local -a a13=("${@}");local a50="" a5=999;local a88 a4 a84 a57;local -a _sug_aliases=();for (( a88=0; a88 < ${#a13[@]}; a88+=2 ));do a4="${a13[a88]/:*}";a4="${a4/@*/}";a4="${a4#\#}";IFS='|' read -ra _sug_aliases <<< "${a4}";for a84 in "${_sug_aliases[@]}";do a57="$(:usage::levenshtein "${a38}" "${a84}")";if (( a57 < a5 ));then a5="${a57}";a50="${a84}";fi;done;done;if (( a5 > 0 && a5 <= 2 ));then local a14=${#a38};(( ${#a50} > a14 )) && a14=${#a50};if (( a5 * 100 <= a14 * 40 ));then echo "${a50}";fi;fi;};:usage::levenshtein() { local a84="${1}" a85="${2}";local a27=${#a84} a31=${#a85};if (( a27 == 0 )); then echo "${a31}"; return; fi;if (( a31 == 0 )); then echo "${a27}"; return; fi;local -a a65;local a89;for (( a89=0; a89 <= a31; a89++ )); do a65[a89]="${a89}"; done;local a88 a19 a53 a16;for (( a88=1; a88 <= a27; a88++ ));do a19="${a65[0]}";a65[0]="${a88}";for (( a89=1; a89 <= a31; a89++ ));do if [[ "${a84:a88-1:1}" == "${a85:a89-1:1}" ]];then a53=0;else a53=1;fi;a16=$(( a19 + a53 ));(( a65[a89] + 1 < a16 )) && a16=$(( a65[a89] + 1 ));(( a65[a89-1] + 1 < a16 )) && a16=$(( a65[a89-1] + 1 ));a19="${a65[a89]}";a65[a89]="${a16}";done;done;echo "${a65[a31]}";};:usage() { local a46="${1}"; shift;declare -p a47 &>/dev/null || local -a a47=();declare -p args &>/dev/null || local -a args=();:args::_dedup_inherited;[[ $(( ${#a47[@]} % 2 )) -eq 0 ]] || :args::_error "usage must be an associative array";if [[ -z ${1:-} || ${1} == "-h" || ${1} == "--help" ]];then a47=(":usage::help" "${a46}" "${a47[@]}");return 0;fi;if ! (( ${#COMMANDNAME[@]} )) && [[ ${1:-} == "--argsh" ]];then echo "https://arg.sh ${ARGSH_COMMIT_SHA:-} ${ARGSH_VERSION:-}";exit 0;fi;local -A a41=();local -a a72=("${@}");local a73 a33="";while (( ${#a72[@]} ));do if [[ ${a72[0]:0:1} != "-" ]];then [[ -z "${a73:-}" ]] || break;a73="${a72[0]}";a72=("${a72[@]:1}");continue;fi;:args::parse_flag || break;a41["${a33}"]=1;done;:args::check_required_flags;local a60;for (( a88=0; a88 < ${#a47[@]}; a88+=2 ));do local _entry_cmd="${a47[a88]/:*}";_entry_cmd="${_entry_cmd/@*/}";IFS='|' read -ra _aliases <<< "${_entry_cmd}";for a28 in "${_aliases[@]}";do a28="${a28#\#}";[[ "${a73}" == "${a28}" ]] || continue;a33="${a47[a88]#\#}";a60="${a47[a88]/*:-}";a60="${a60#\#}";[[ "${a60}" == "${a47[a88]}" ]] || break 2;a60="${a60/|*}";a60="${a60/@*/}";break 2;done;done;if [[ -z "${a60:-}" ]];then if [[ -z "${a73:-}" ]];then a47=(":usage::help" "${a46}" "${a47[@]}");return 0;fi;local _sug;_sug="$(:usage::suggest "${a73}" "${a47[@]}")";if [[ -n "${_sug}" ]];then :args::error_usage "Invalid command: ${a73}. Did you mean '${_sug}'?";else :args::error_usage "Invalid command: ${a73}";fi;fi;local a9=0;[[ "${a33}" != *":-"* ]] || a9=1;if (( a9 ));then declare -F -- "${a60}" >/dev/null 2>&1 || { local _sug;_sug="$(:usage::suggest "${a73}" "${a47[@]}")";if [[ -n "${_sug}" ]];then :args::error_usage "Invalid command: ${a73}. Did you mean '${_sug}'?";else :args::error_usage "Invalid command: ${a73}";fi;};else local a18="${FUNCNAME[1]:-}";if [[ -n "${a18}" ]] && declare -F -- "${a18}::${a60}" >/dev/null 2>&1;then a60="${a18}::${a60}";elif [[ "${a18}" == *"::"* ]] && declare -F -- "${a18##*::}::${a60}" >/dev/null 2>&1;then a60="${a18##*::}::${a60}";elif declare -F -- "argsh::${a60}" >/dev/null 2>&1;then a60="argsh::${a60}";elif declare -F -- "${a60}" >/dev/null 2>&1;then :;else local _sug;_sug="$(:usage::suggest "${a73}" "${a47[@]}")";if [[ -n "${_sug}" ]];then :args::error_usage "Invalid command: ${a73}. Did you mean '${_sug}'?";else :args::error_usage "Invalid command: ${a73}";fi;fi;fi;local _cmd_name="${a33/[|:]*}";COMMANDNAME+=("${_cmd_name/@*/}");declare -F __argsh_trace_usage &>/dev/null && __argsh_trace_usage "${a60}" "${a73}";a47=("${a60}" "${a72[@]}");};:usage::text() { local a46="${1:-}";string::indent "${a46}";echo;echo "Usage: ${COMMANDNAME[*]} <command> [args]";[[ ${a47[0]:-} == '-' ]] || echo -e "\nAvailable Commands:";for (( a88=0; a88 < ${#a47[@]}; a88+=2 ));do [[ "${a47[a88]:0:1}" != "#" ]] || continue;[[ "${a47[a88]}" != "-" ]] || { echo;echo "${a47[a88+1]}";continue;};local _disp_name="${a47[a88]/[:|]*}";printf "  %-${ARGSH_FIELD_WIDTH}s %s\n" "${_disp_name/@*/}" "${a47[a88+1]}";done;:args::text_flags;echo;echo "Use \"${COMMANDNAME[*]} <command> --help\" for more information about a command.";};:args::_dedup_inherited() { while (( ${#args[@]} % 2 )) && (( ${#args[@]} )) && [[ -z "${args[-1]}" ]];do unset 'args[-1]';done;(( ${#args[@]} % 2 == 0 )) || return 0;(( ${#args[@]} > 2 )) || return 0;local -A _seen=() _non_inherited=();local _name _i;for (( _i=0; _i < ${#args[@]}; _i+=2 ));do [[ "${args[_i]}" != "-" && -n "${args[_i]}" ]] || continue;_name="$(args::field_name "${args[_i]}")";[[ -n "${_name}" ]] || continue;_seen["${_name}"]=1;[[ "${args[_i]}" == *":^"* ]] || _non_inherited["${_name}"]=1;done;local -A _kept=();local -a _rev=();for (( _i=${#args[@]}-2; _i >= 0; _i-=2 ));do [[ "${args[_i]}" != "-" && -n "${args[_i]}" ]] || { _rev+=("${args[_i+1]}" "${args[_i]}");continue;};_name="$(args::field_name "${args[_i]}")";[[ -n "${_name}" ]] || { _rev+=("${args[_i+1]}" "${args[_i]}"); continue; };[[ -z "${_kept[${_name}]:-}" ]] || continue;[[ -z "${_non_inherited[${_name}]:-}" || "${args[_i]}" != *":^"* ]] || continue;_kept["${_name}"]=1;_rev+=("${args[_i+1]}" "${args[_i]}");done;local -a _result=();for (( _i=${#_rev[@]}-1; _i >= 0; _i-=2 ));do _result+=("${_rev[_i]}" "${_rev[_i-1]}");done;args=("${_result[@]}");};:args() { local a46="${1}"; shift;declare -p args &>/dev/null || local -a args=();:args::_dedup_inherited;[[ $(( ${#args[@]} % 2 )) -eq 0 ]] || :args::_error "args must be an associative array";if [[ ${1:-} == "-h" || ${1:-} == "--help" ]];then :args::text;exit 0;fi;local a34=0 a33="" a88 a0=1;local -A a41=();local -a a72=("${@}");while (( ${#a72[@]} ));do if [[ ${a72[0]:0:1} != "-" ]];then local _pos_name _pos_value;a88="$(:args::field_positional "${a0}")" || :args::error_usage "too many arguments: ${a72[0]}";a33="${args[a88]}";_pos_name="$(args::field_name "${a33}")";_pos_value="$(:args::field_value "${a72[0]}")" || exit "${?}";local -n a77="${_pos_name}";if is::array "${_pos_name}";then (( a34 )) || { a77=();a34=1;};a77+=("${_pos_value}");else a77="${_pos_value}";fi;a72=("${a72[@]:1}");(( ++a0 ));continue;fi;if [[ "${a72[0]}" == "-h" || "${a72[0]}" == "--help" ]];then :args::text;exit 0;fi;:args::parse_flag || :args::error_usage "unknown flag: ${a72[0]}";a41["${a33}"]=1;done;if a88="$(:args::field_positional "${a0}")";then a33="$(args::field_name "${args[a88]}")";if is::uninitialized "${a33}" && ! is::array "${a33}";then :args::error_usage "missing required argument: ${a33}";fi;fi;:args::check_required_flags;[[ ${#a72[@]} -eq 0 ]] || :args::error_usage "too many arguments: ${a72[*]}";declare -F __argsh_trace_args &>/dev/null && __argsh_trace_args "${a46}";};:args::text() { declare -p args &>/dev/null || return 0;local -a a2=() a24=();:args::positional;string::indent "${a46}";echo;echo "Usage:";echo "  ${COMMANDNAME[*]} ${a24[*]}";(( ${#a2[@]} == 0 )) || { echo;echo "Arguments:";for a88 in "${a2[@]}";do [[ ${args[a88]} != "-" ]] || continue;a55="$( printf "   %-${ARGSH_FIELD_WIDTH}s%s" " " "${args[a88+1]}" | fmt::tty;)";printf "   %-${ARGSH_FIELD_WIDTH}s%s\n" "$(:args::fieldf "${args[a88]}")" "$(string::trim-left "${a55}")";done;};:args::text_flags;echo;};:args::text_flags() { local -a args=("${args[@]}");local -a a35=();array::contains 'help|h:+' "${args[@]}" || args+=('help|h:+' "Show this help message");:args::flags;(( ${#a35[@]} )) || return 0;[[ "${args[${a35[0]}]}" == "-" ]] || echo -e "\nOptions:";for a88 in "${a35[@]}";do [[ "${args[a88]:0:1}" != "#" && "${args[a88]}" != *":#"* ]] || continue;[[ "${args[a88]}" != "-" ]] || { echo;echo "${args[a88+1]}";continue;};:args::fieldf "${args[a88]}";{ echo -n "           ";echo -e "${args[a88+1]}\n";} | fmt::tty;done;};:args::flags() { declare -p args &>/dev/null || local -a args;declare -p a35 &>/dev/null || local -a a35;for (( a88=0; a88 < "${#args[@]}"; a88+=2 ));do if [[ ${args[a88]} == *"|"* || ${args[a88]} == '-' ]];then a35+=("${a88}");fi;done;};:args::positional() { declare -p args &>/dev/null || local -a args;declare -p a2 &>/dev/null || local -a a2;declare -p a24 &>/dev/null || local -a a24;local a77;for (( a88=0; a88 < "${#args[@]}"; a88+=2 ));do [[ ${args[a88]} != *"|"* && ${args[a88]} != '-' ]] || continue;a77="$(args::field_name "${args[a88]}")";a2+=("${a88}");if is::array "${a77}";then a24+=("...${a77}");continue;fi;if ! is::uninitialized "${a77}";then a24+=("[${a77}]");continue;fi;a24+=("<${a77}>");done;};:args::parse_flag() { declare -p a72 a33 &>/dev/null || return 1;local a59="${a72[0]/=*}";if [[ ${a59:0:2} == "--" ]];then a33="$(:args::field_lookup "${a59:2}")" || return "${?}";elif [[ ${a59:0:1} == "-" ]];then a59="${a59:0:2}";a33="$(:args::field_lookup "${a59:1}")" || return "${?}";fi;:args::field_set_flag "${a33}";};:args::check_required_flags() { declare -p a41 args &>/dev/null || return 1;local a33;local -a a30;for (( a88=0; a88 < ${#args[@]}; a88+=2 ));do a33="${args[a88]}";:args::field_attrs "${a33}";if (( a30[2] )) && ! (( a30[4] ));then local -n a77="${a30[0]}";a77=0;fi;if (( a30[6] )) && [[ ${args[a88]} == *"|"* ]] && [[ -z ${a41[${args[a88]}]:-} ]];then :args::error_usage "missing required flag: ${args[a88]/|*}";fi;done;};:args::field_set_flag() { local a33="${1}";declare -p a72 a59 &>/dev/null || return 1;local -a a30;:args::field_attrs "${a33}";local -n a77="${a30[0]}";local a8 a6;if (( a30[2] ));then a8=1;if [[ ${a59:0:2} == "--" ]];then a72=("${a72[@]:1}");else a72[0]="-${a72[0]:2}";[[ ${a72[0]} != "-" ]] || a72=("${a72[@]:1}");fi;fi;[[ -n ${a8:-} ]] || { a6="${a72[0]/${a59}}";if [[ ${a6} == "" ]];then (( ${#a72[@]} > 1 )) || :args::error "missing value for flag: ${a30[0]}";a8="${a72[1]}";a72=("${a72[@]:1}");else [[ "${a6:0:1}" != "=" ]] || a6="${a6:1}";a8="${a6}";fi;a8="$(:args::field_value "${a8}")" || exit "${?}";a72=("${a72[@]:1}");};if (( a30[5] ));then a77+=("${a8}");else a77="${a8}";fi;};:args::field_value() { local a48="${1}";declare -p a33 &>/dev/null || return 1;declare -p a30 &>/dev/null || { local -a a30;:args::field_attrs "${a33}";};declare -f "to::${a30[3]}" &>/dev/null || :args::_error "unknown type: ${a30[3]}";"to::${a30[3]}" "${a48}" "${a30[0]}" || :args::error_usage "invalid type (${a30[3]}): ${a48}";};:args::field_lookup() { local a33="${1}";declare -p args &>/dev/null || return 1;for (( a88=0; a88 < ${#args[@]}; a88+=2 ));do if [[ ${args[a88]} =~ (^${a33}\||\|${a33}:|\|${a33}$) ]];then echo "${args[a88]}";return 0;fi;done;return 1;};:args::field_positional() { local a10="${1:-1}";declare -p args &>/dev/null || return 1;for (( a88=0; a88 < ${#args[@]}; a88+=2 ));do if [[ ${args[a88]} != *"|"* && ${args[a88]} != '-' ]];then if is::array "$(args::field_name "${args[a88]}")" || (( --a10 == 0 ));then echo "${a88}";return 0;fi;fi;done;return 1;};args::field_name() { local a33="${1}";local a29="${2:-1}";a33="${a33/[|:]*}";a33="${a33#\#}";if (( a29 ));then a33="${a33//-/_}";fi;echo "${a33}";};:args::field_attrs() { local a33="${1}";declare -p a30 &>/dev/null || local -a a30;a30=( "" "" 0 "" 0 0 0 0 "" );local a67="+~!^:#";local a64="${a33#*[:]}";[ "${a64}" != "${a33}" ] || a64="";a30[0]="$(args::field_name "${a33}")";a30[8]="$(args::field_name "${a33}" 0)";[[ ${a30[0]:0:1} != "#" ]] || { a30[7]=1;};local -n a77="${a30[0]}";local -a a35;IFS='|' read -ra a35 <<< "${a33/[:]*}";[[ ${#a35[@]} -eq 1 ]] || { a30[1]="${a35[1]}";};if is::array "${a30[0]}";then a30[5]=1;! is::uninitialized "${a30[0]}" || a77=();! (( ${#a77[@]} )) || a30[4]=1;elif ! is::uninitialized "${a30[0]}";then a30[4]=1;fi;while (( ${#a64} > 0 ));do if [[ ${a64:0:1} == "+" ]];then [[ -z ${a30[3]} ]] || :args::_error "cannot have multiple types: ${a30[3]} and boolean";a30[2]=1;a64="${a64:1}";continue;fi;if [[ ${a64:0:1} == "~" ]];then ! (( a30[2] )) || :args::_error "already flagged as boolean";a64="${a64:1}";a30[3]="${a64/[$a67]*}";a64="${a64:${#a30[3]}}";continue;fi;if [[ ${a64:0:1} == "!" ]];then ! (( a30[6] )) || :args::_error "field already flagged as required";a30[6]=1;a64="${a64:1}";continue;fi;if [[ ${a64:0:1} == "^" ]];then a64="${a64:1}";continue;fi;if [[ ${a64:0:1} == "#" ]];then a30[7]=1;a64="${a64:1}";continue;fi;if [[ ${a64:0:1} == ":" ]];then a64="${a64:1}";continue;fi;echo ":args error: unknown modifier: ${a64:0:1}" >&2;exit 2;done;if [[ -z ${a30[3]} && ${a30[2]} -eq 0 ]];then a30[3]="string";fi;};:args::fieldf() { local a33="${1}";declare -p a30 &>/dev/null || { local -a a30;:args::field_attrs "${a33}";};[[ ${a33} == *"|"* ]] || { echo "${a30[8]} ${a30[3]}";return 0;};local -n a77="${a30[0]}";a20="   ";! (( a30[6] )) || a20=" ! ";if [[ -n ${a30[1]} ]];then a20+="-${a30[1]}, --${a30[8]}";else a20+="    --${a30[8]}";fi;a20+=" ";! (( a30[5] )) || a20+="...";a20+="${a30[3]}";if (( a30[4] )) && ! (( a30[2] ));then a20+=" (default: ${a77[*]})";fi;echo "${a20}";};fi;args::run() { local a69="${1}"; shift;for (( a88=0; a88<${#}; a88++ ));do local a78="${1}"; shift;local a60="${1}"; shift;if ! (( a69 )) || (( a78 ));then "${a60}";fi;done;};array::contains() { local -r a23="${1}"; shift;for a12 in "${@}";do [[ "${a12}" != "${a23}" ]] || return 0;done;return 1;};array::join() { local -r a7="${1}"; shift;local a25;printf -v a25 "${a7}%s" "${@}";echo "${a25:${#a7}}";};array::nth() { local -n out="${1}";local -r a76="${2}";shift 2;for (( a88=1; a88<=${#}; a88++ ));do (( a88 % a76 )) || out+=("${!a88}");done;};bash::version() { local a40="${1:-4}";local a42="${2:-3}";local a43="${3:-0}";if [[ "${BASH_VERSINFO[0]}" -lt "${a40}" ]];then return 1;elif [[ "${BASH_VERSINFO[0]}" -gt "${a40}" ]];then return 0;fi;if [[ "${BASH_VERSINFO[1]}" -lt "${a42}" ]];then return 1;elif [[ "${BASH_VERSINFO[1]}" -gt "${a42}" ]];then return 0;fi;if [[ "${BASH_VERSINFO[2]}" -lt "${a43}" ]];then return 1;fi;return 0;};binary::exists() { local a17="${1}";command -v "${a17}" &> /dev/null || { echo "${a17} is required to run this script" >&2;return 1;};};binary::github() { local path="${1}";local a66="${2}";local a58="${3}";local a81="${4:-}";curl -Lso /dev/stdout "https://github.com/${a66}/releases/download/${a58}" | { if [[ -n "${a81}" ]];then tar -xz -C "$(dirname "${path}")" "${a81}";else tee "${path}" &> /dev/null;fi;chmod +x "${path}";};};binary::arch() { local a45="${1:-0}";local -r a49="$(uname -m)";case "${a49}" in
x86_64|amd64)if (( a45 )); then echo "64-bit"; else echo "amd64"; fi;;
armv7l)echo "arm";;
aarch64)echo "arm64";;
*)echo "${a49}";;
esac;};binary::jq() { binary::exists "jq" 2>/dev/null || { local -r latest="$(github::latest "stedolan/jq")" system="$(uname -s)";binary::github "${PATH_BIN?}/jq" "stedolan/jq" "${latest}/jq-${system,,}-$(binary::arch)";};};binary::vale() { binary::exists "vale" 2>/dev/null || { local -r latest="$(github::latest "errata-ai/vale")" system="$(uname -s)";binary::github "${PATH_BIN?}/vale" "errata-ai/vale" "${latest}/vale_${latest:1}_$(uname -s)_$(binary::arch 1).tar.gz" "vale";};};docker::user() { local a82="${1:-"$(id -u)"}";local a75="${2:-"$(id -g)"}";local a68="${3:-"$(whoami)"}";local a61="${4:-"/workspace"}";local a44="${5:-"/bin/sh"}";local a54;a54="$(pwd)";a54="${a54#"${PATH_BASE:-}"}";if [[ "${a54}" == "$(pwd)" ]];then a54="${a61}";else a54="${a61}${a54}";fi;local _passwd _group;_passwd="$(mktemp /tmp/docker_passwd.XXXXXX)";_group="$(mktemp /tmp/docker_group.XXXXXX)";echo "${a68}:x:${a82}:${a75}::${a61}:${a44}" > "${_passwd}";echo "${a68}:x:${a75}:" > "${_group}";echo "-v ${_passwd}:/etc/passwd -v ${_group}:/etc/group";echo "-u ${a82}:${a75}";echo "-v ${PATH_BASE:-"$(pwd)"}:${a61}";echo "-w ${a54}";if [[ -n "${PATH_BIN:-}" && -d "${PATH_BIN}" ]];then local _abs_bin;_abs_bin="$(realpath "${PATH_BIN}" 2>/dev/null || echo "${PATH_BIN}")";echo "-v ${_abs_bin}:/usr/local/sbin:ro";echo "-e PATH_BIN=/usr/local/sbin";fi;};error::stacktrace() { local -r a51="${1:-${?}}";if (( a51 ));then echo -e "\n\033[38;5;196m■■ Stacktrace(${a51}): \e[1m${BASH_COMMAND}\e[22m";for (( a88 = 1; a88 <= ${#FUNCNAME[@]} - 2; a88++ ));do echo -e "${a88}. ${BASH_SOURCE[a88]}:${BASH_LINENO[a88-1]} ➜ ${FUNCNAME[a88]}()";done;echo -e "\033[0m";return "${a51}";fi;};:args::_error() { echo -e "Error: ${1}" >&2;exit 2;};:args::error() { echo -e "Error: ${1}\n" >&2;echo "  Run \"${0##*/} -h\" for more information." >&2;exit 2;};:args::error_usage() { echo -e "Error: ${1}\n" >&2;echo "  Run \"${0##*/} -h\" for more information." >&2;exit 2;};fmt::tty() { local a80="${1:-"$(cat)"}";if ! command -v fmt &>/dev/null || [[ ! -t 1 ]];then echo "${a80}";return 0;fi;local a52;a52="$(tput cols)";echo "${a80}" | fmt -w "${a52}";};github::latest() { local a66="${1}";curl -fsSLI -o /dev/null -w "%{url_effective}" "https://github.com/${a66}/releases/latest" | rev | cut -d'/' -f1 | rev;};declare -gA a1=();: "${__ARGSH_LIB_DIR:=${BASH_SOURCE[0]%/*}}";import() { local a79="${1}";(( ${a1["${a79}"]:-} )) || { [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: import ${a79}" >&2;a1["${a79}"]=1;if [[ ${a79:0:1} == "@" ]];then local _base="${PATH_BASE:-}";if [[ -z "${_base}" ]];then local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}";local _sdir; [[ "${_s}" == */* ]] && _sdir="${_s%/*}" || _sdir=".";_base="$(import::_find_git_root "$(cd "${_sdir}" 2>/dev/null && pwd)")" || { echo "import: @ prefix requires PATH_BASE or a git repository" >&2;exit 1;};fi;a79="${_base}/${a79:1}";elif [[ ${a79:0:1} == "^" ]];then local _scripts="" _mod="${a79:1}";_scripts="$(import::_resolve_scripts)";if [[ -n "${_scripts}" ]];then a79="${_scripts}/${_mod}";else local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}";local _dir;[[ "${_s}" == */* ]] && _dir="${_s%/*}" || _dir=".";a79="$(import::_walk_up "${_dir}" "${_mod}")" || { echo "import: cannot resolve ^${_mod} — set PATH_SCRIPTS or add '# argsh source=<path>'" >&2;exit 1;};fi;elif [[ ${a79:0:1} == "~" ]];then local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}";a79="${_s%/*}/${a79:1}";else local _s="${ARGSH_SOURCE:-${BASH_SOURCE[0]}}";a79="${_s%/*}/${a79}";if ! import::_file_exists "${a79}";then if [[ "${1}" != */* && "${1}" != *..* ]];then local _libs_dir;_libs_dir="$(import::_libs_dir)";local _lib="${_libs_dir}/${1}/${1}";if import::_file_exists "${_lib}";then a79="${_lib}";fi;fi;fi;if ! import::_file_exists "${a79}";then if [[ "${1}" != */* && "${1}" != *..* ]];then local _global="${XDG_DATA_HOME:-${HOME}/.local/share}/argsh/libs/${1}/${1}";if import::_file_exists "${_global}";then a79="${_global}";fi;fi;fi;fi;import::source "${a79}" || exit 1;};};import::_resolve_scripts() { if [[ -n "${PATH_SCRIPTS:-}" ]];then echo "${PATH_SCRIPTS}";return;fi;local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}";[[ -f "${_s}" ]] || return 0;local _dir _line;[[ "${_s}" == */* ]] && _dir="${_s%/*}" || _dir=".";_line="$(head -20 "${_s}" | grep -m1 '^# argsh source=' 2>/dev/null)" || return 0;local _path="${_line#*=}";_path="${_path#"${_path%%[![:space:]]*}"}";_path="${_path%"${_path##*[![:space:]]}"}";if [[ "${_path:0:1}" != "/" ]];then _path="${_dir}/${_path}";fi;_path="$(cd "${_path}" 2>/dev/null && pwd)" || return 0;echo "${_path}";};import::_file_exists() { local _ext;for _ext in "" ".sh" ".bash";do [[ -f "${1}${_ext}" ]] && return 0;done;return 1;};declare -g __ARGSH_LIBS_DIR="";import::_libs_dir() { if [[ -n "${__ARGSH_LIBS_DIR}" ]];then echo "${__ARGSH_LIBS_DIR}";return;fi;local _base="${PATH_BASE:-.}";if [[ -f "${_base}/.argsh.yaml" ]];then local _custom="" _line _in_defaults=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" && "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^defaults:[[:space:]]*$ ]]; then _in_defaults=1; continue; fi;if (( _in_defaults ));then [[ "${_line}" =~ ^[[:space:]] ]] || break;[[ "${_line}" =~ ^[[:space:]]+path_libs:[[:space:]]+(.*) ]] || continue;_custom="${BASH_REMATCH[1]}";_custom="${_custom%\"}" ; _custom="${_custom#\"}";_custom="${_custom%\'}" ; _custom="${_custom#\'}";_custom="${_custom%%[[:space:]]\#*}";_custom="${_custom%"${_custom##*[![:space:]]}"}";break;fi;done < "${_base}/.argsh.yaml";if [[ -n "${_custom}" ]];then if [[ "${_custom:0:1}" == "/" ]];then __ARGSH_LIBS_DIR="${_custom}";else __ARGSH_LIBS_DIR="${_base}/${_custom}";fi;echo "${__ARGSH_LIBS_DIR}";return;fi;fi;__ARGSH_LIBS_DIR="${_base}/.argsh/libs";echo "${__ARGSH_LIBS_DIR}";};import::_find_git_root() { local _d;_d="${1:-$(pwd)}";while [[ -n "${_d}" && "${_d}" != "/" ]];do [[ -e "${_d}/.git" ]] && { echo "${_d}"; return 0; };_d="${_d%/*}";done;return 1;};import::_walk_up() { local _dir _mod="${2}" _root _ext _abs;_abs="$(cd "${1}" 2>/dev/null && pwd)" || return 1;_dir="${_abs}";_root="$(import::_find_git_root "${_abs}" 2>/dev/null)" || _root="/";while [[ -n "${_dir}" && "${_dir}" != "/" ]];do for _ext in "" ".sh" ".bash";do [[ -f "${_dir}/${_mod}${_ext}" ]] && { echo "${_dir}/${_mod}";return 0;};done;[[ "${_dir}" != "${_root}" ]] || break;_dir="${_dir%/*}";done;return 1;};import::source() { local a79="${1}";for a74 in "" ".sh" ".bash";do if [[ -f "${a79}${a74}" ]];then [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: import resolved -> ${a79}${a74}" >&2;. "${a79}${a74}";return;fi;done;echo "Library not found ${a79}" >&2;return 1;};import::clear() { a1=();};is::tty() { [[ -t 1 ]];};is::array() { local _decl;_decl="$(declare -p "${1}" 2>/dev/null)" || return 1;[[ "${_decl}" == "declare -a"* ]];};is::uninitialized() { local a83="${1}";if is::array "${a83}";then [[ $(declare -p "${a83}") =~ ^declare\ -a\ ${a83}(=\(\))?$ ]];else [[ ! ${!a83+x} ]];fi;};is::set() { ! is::uninitialized "${1}";};[[ -n "$(type -t :usage 2>/dev/null)" ]] || import args;if [[ -z "${ARGSH_VERSION:-}" || "${ARGSH_VERSION:-}" == "unknown" ]];then ARGSH_VERSION="$(git -C "${BASH_SOURCE[0]%/*}" describe --tags --always --dirty 2>/dev/null)" || ARGSH_VERSION="unknown";fi;if [[ -z "${ARGSH_COMMIT_SHA:-}" || "${ARGSH_COMMIT_SHA:-}" == "unknown" ]];then ARGSH_COMMIT_SHA="$(git -C "${BASH_SOURCE[0]%/*}" rev-parse --short HEAD 2>/dev/null)" || ARGSH_COMMIT_SHA="unknown";fi;argsh::builtin::try() { if [[ -n "${1:-}" ]];then [[ -f "${1}" ]] || return 1;enable -f "${1}" "${__ARGSH_BUILTINS[@]}" 2>/dev/null || return 1;return 0;fi;__argsh_try_builtin;};argsh::builtin::location() { local _so _d;local -r _n="argsh.so";for _so in "${ARGSH_BUILTIN_PATH:-}" "${PATH_LIB:+${PATH_LIB}/${_n}}" "${PATH_BIN:+${PATH_BIN}/${_n}}" ;do [[ -n "${_so}" && -f "${_so}" ]] && { echo "${_so}"; return 0; };done;for _d in "${LD_LIBRARY_PATH:-}" "${BASH_LOADABLES_PATH:-}";do [[ -n "${_d}" ]] || continue;local IFS=:;for _so in ${_d};do [[ -n "${_so}" && -f "${_so}/${_n}" ]] && { echo "${_so}/${_n}"; return 0; };done;done;[[ -f "${HOME}/.local/lib/bash/${_n}" ]] && { echo "${HOME}/.local/lib/bash/${_n}"; return 0; };echo "not installed";return 1;};argsh::builtin::install_dir() { local _d;if [[ -n "${PATH_BIN:-}" && -d "${PATH_BIN}" && -w "${PATH_BIN}" ]];then echo "${PATH_BIN}"; return 0;fi;if [[ -n "${PATH_LIB:-}" && -d "${PATH_LIB}" && -w "${PATH_LIB}" ]];then echo "${PATH_LIB}"; return 0;fi;if [[ -n "${BASH_LOADABLES_PATH:-}" ]];then local IFS=:;for _d in ${BASH_LOADABLES_PATH};do [[ -d "${_d}" && -w "${_d}" ]] && { echo "${_d}"; return 0; };done;fi;_d="${HOME}/.local/lib/bash";mkdir -p "${_d}" 2>/dev/null || true;if [[ -d "${_d}" && -w "${_d}" ]];then echo "${_d}"; return 0;fi;return 1;};argsh::builtin::arch() { case "$(uname -m)" in
x86_64)echo "amd64";;
aarch64)echo "arm64";;
*)return 1;;
esac;};argsh::builtin::download() { local _force="${1:-0}" _dir _dest _tag _arch;if (( ! _force )) && argsh::builtin::try 2>/dev/null;then echo "argsh: builtins already installed" >&2;return 0;fi;[[ "$(uname -s)" == "Linux" ]] || { echo "argsh: builtins are only available for Linux (got $(uname -s))" >&2;return 1;};_arch="$(argsh::builtin::arch)" || { echo "argsh: unsupported architecture: $(uname -m)" >&2;echo "  Available: x86_64 (amd64), aarch64 (arm64)" >&2;return 1;};_dir="$(argsh::builtin::install_dir)" || { echo "argsh: no writable install path found for builtins" >&2;echo "  Run: argsh builtin install --path /your/writable/dir" >&2;return 1;};_dest="${_dir}/argsh.so";command -v curl &>/dev/null || { echo "argsh: curl is required to download builtins" >&2;return 1;};_tag="$(github::latest "arg-sh/argsh")" || { echo "argsh: failed to get latest release from GitHub" >&2;return 1;};local _libc="";if command -v ldd &>/dev/null && ldd --version 2>&1 | grep -qi musl;then _libc="-musl";fi;local _asset="argsh-linux${_libc}-${_arch}.so";local _tmp;_tmp="$(mktemp "${_dest}.download.XXXXXX")" || { echo "argsh: failed to create temporary download file in ${_dir}" >&2;return 1;};echo "argsh: downloading ${_asset} (${_tag})..." >&2;curl -fsSL -o "${_tmp}" "https://github.com/arg-sh/argsh/releases/download/${_tag}/${_asset}" || { echo "argsh: download failed" >&2;echo "  Asset ${_asset} may not exist for ${_tag}" >&2;rm -f "${_tmp}";return 1;};local _expected_sha _actual_sha _sha_cmd;_expected_sha="$( curl -fsSL "https://github.com/arg-sh/argsh/releases/download/${_tag}/sha256sum.txt" | grep -F -- "${_asset}" | head -1 | cut -d' ' -f1;)" || true;[[ "${_expected_sha}" =~ ^[0-9a-f]{64}$ ]] || _expected_sha="";if [[ -n "${_expected_sha}" ]];then if command -v sha256sum &>/dev/null;then _sha_cmd="sha256sum";elif command -v shasum &>/dev/null;then _sha_cmd="shasum -a 256";else echo "argsh: warning: no sha256sum/shasum available — skipping checksum verification" >&2;_expected_sha="";fi;fi;if [[ -n "${_expected_sha}" ]];then _actual_sha="$(${_sha_cmd} "${_tmp}" 2>/dev/null)" || true;_actual_sha="${_actual_sha%% *}";if [[ -z "${_actual_sha}" ]];then echo "argsh: warning: ${_sha_cmd} failed — skipping checksum verification" >&2;elif [[ "${_actual_sha}" != "${_expected_sha}" ]];then echo "argsh: SHA256 checksum mismatch for ${_asset}" >&2;echo "  expected: ${_expected_sha}" >&2;echo "  actual:   ${_actual_sha}" >&2;rm -f "${_tmp}";return 1;else [[ "${ARGSH_DEBUG:-}" != "1" ]] || echo "argsh:debug: SHA256 verified: ${_actual_sha}" >&2;fi;else [[ "${ARGSH_DEBUG:-}" != "1" ]] || echo "argsh:debug: SHA256 verification skipped (no checksum available)" >&2;fi;local _verify_err;_verify_err="$( (enable -f "${_tmp}" "${__ARGSH_BUILTINS[@]}") 2>&1 1>/dev/null;)" || { echo "argsh: downloaded file failed to load as builtin" >&2;[[ -n "${_verify_err}" ]] && echo "${_verify_err}" >&2;rm -f "${_tmp}";return 1;};local _mode="444";chmod "${_mode}" "${_tmp}" 2>/dev/null || true;mv -f "${_tmp}" "${_dest}" || { echo "argsh: failed to install to ${_dest}" >&2;rm -f "${_tmp}";return 1;};echo "argsh: installed to ${_dest}" >&2;return 0;};argsh::builtin() { case "${1:-}" in
install)shift; argsh::builtin::_install "${@}";;
update)shift; argsh::builtin::_install --force "${@}";;
status|"")local _loc _arch;_loc="$(argsh::builtin::location 2>/dev/null)" || _loc="not installed";_arch="$(argsh::builtin::arch 2>/dev/null)" || _arch="unsupported";echo "argsh builtin: ${_loc}";echo "  platform: $(uname -s | tr '[:upper:]' '[:lower:]')/${_arch}";echo "  loaded:   $(( ${ARGSH_BUILTIN:-0} )) (ARGSH_BUILTIN=${ARGSH_BUILTIN:-0})";echo "";echo "Usage: argsh builtin [install|update|status] [--force] [--path DIR]";echo "       Set ARGSH_BUILTIN_PATH env var to control builtin search path.";;
*)echo "argsh: unknown builtin subcommand: ${1}" >&2;echo "Usage: argsh builtin [install|update|status] [--force] [--path DIR]" >&2;return 1;;
esac;};argsh::builtins() { argsh::builtin "${@}"; };declare -g __ARGSH_LIB_REGISTRY="${ARGSH_LIB_REGISTRY:-ghcr.io/arg-sh/libs}";argsh::lib::_semver_cmp() { local _v1="${1}" _op="${2}" _v2="${3}";local _maj1 _min1 _pat1 _maj2 _min2 _pat2;IFS='.' read -r _maj1 _min1 _pat1 <<< "${_v1%%[-+]*}";IFS='.' read -r _maj2 _min2 _pat2 <<< "${_v2%%[-+]*}";: "${_maj1:=0}" "${_min1:=0}" "${_pat1:=0}";: "${_maj2:=0}" "${_min2:=0}" "${_pat2:=0}";local _r=0;if [[ ${_maj1} -gt ${_maj2} ]]; then _r=1;elif [[ ${_maj1} -lt ${_maj2} ]]; then _r=-1;elif [[ ${_min1} -gt ${_min2} ]]; then _r=1;elif [[ ${_min1} -lt ${_min2} ]]; then _r=-1;elif [[ ${_pat1} -gt ${_pat2} ]]; then _r=1;elif [[ ${_pat1} -lt ${_pat2} ]]; then _r=-1;fi;case "${_op}" in
ge)[[ ${_r} -ge 0 ]];;
gt)[[ ${_r} -gt 0 ]];;
le)[[ ${_r} -le 0 ]];;
lt)[[ ${_r} -lt 0 ]];;
eq)[[ ${_r} -eq 0 ]];;
*)return 1;;
esac;};argsh::lib::_semver_satisfies() { local _ver="${1}" _constraint="${2}";_ver="${_ver#v}";_constraint="${_constraint#v}";local _op="" _target="";case "${_constraint}" in
">="*)_op="ge"; _target="${_constraint#>=}";;
">"*)_op="gt"; _target="${_constraint#>}";;
"<="*)_op="le"; _target="${_constraint#<=}";;
"<"*)_op="lt"; _target="${_constraint#<}";;
"="*)_op="eq"; _target="${_constraint#=}";;
"^"*)_target="${_constraint#^}";local _maj="${_target%%.*}" _rest="${_target#*.}";local _min="${_rest%%.*}";if [[ "${_maj}" == "0" ]];then argsh::lib::_semver_cmp "${_ver}" "ge" "${_target}" && argsh::lib::_semver_cmp "${_ver}" "lt" "0.$(( _min + 1 )).0";else argsh::lib::_semver_cmp "${_ver}" "ge" "${_target}" && argsh::lib::_semver_cmp "${_ver}" "lt" "$(( _maj + 1 )).0.0";fi;return;;
"~"*)_target="${_constraint#\~}";local _maj="${_target%%.*}" _rest="${_target#*.}";local _min="${_rest%%.*}";argsh::lib::_semver_cmp "${_ver}" "ge" "${_target}" && argsh::lib::_semver_cmp "${_ver}" "lt" "${_maj}.$(( _min + 1 )).0";return;;
*)_op="eq"; _target="${_constraint}";;
esac;_target="${_target#v}";argsh::lib::_semver_cmp "${_ver}" "${_op}" "${_target}";};argsh::lib::_check_requires() { local _dest="${1}";[[ -f "${_dest}/argsh-plugin.yml" ]] || return 0;local _req;_req="$(argsh::lib::_yaml_get "requires.argsh" "${_dest}/argsh-plugin.yml")";[[ -n "${_req}" ]] || return 0;local _current="${ARGSH_VERSION:-}";_current="${_current#v}";_current="${_current%%-*}";[[ "${_current}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || return 0;if ! argsh::lib::_semver_satisfies "${_current}" "${_req}";then echo "argsh: warning: library requires argsh ${_req} but current version is ${_current}" >&2;fi;};argsh::lib::_yaml_clean() { local _v="${1}";_v="${_v#"${_v%%[![:space:]]*}"}";if [[ "${_v}" != \"* && "${_v}" != \'* ]];then _v="${_v%%[[:space:]]\#*}";fi;_v="${_v%\"}" ; _v="${_v#\"}";_v="${_v%\'}" ; _v="${_v#\'}";_v="${_v%"${_v##*[![:space:]]}"}";echo "${_v}";};argsh::lib::_yaml_get() { local _path="${1}" _file="${2}";[[ -f "${_file}" ]] || return 0;local _key1 _key2="";if [[ "${_path}" == *.* ]];then _key1="${_path%%.*}";_key2="${_path#*.}";else _key1="${_path}";fi;local _line _in_section=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" ]] || continue;[[ "${_line}" != \#* ]] || continue;if [[ -z "${_key2}" && "${_line}" =~ ^${_key1}:[[:space:]]+(.*) ]];then argsh::lib::_yaml_clean "${BASH_REMATCH[1]}";return 0;elif [[ -n "${_key2}" ]];then if [[ "${_line}" =~ ^${_key1}:[[:space:]]*$ ]];then _in_section=1; continue;fi;if (( _in_section ));then [[ "${_line}" =~ ^[[:space:]] ]] || break;if [[ "${_line}" =~ ^[[:space:]]+${_key2}:[[:space:]]+(.*) ]];then argsh::lib::_yaml_clean "${BASH_REMATCH[1]}";return 0;fi;fi;fi;done < "${_file}";};argsh::lib::_yaml_entries() { local _section="${1}" _file="${2}";[[ -f "${_file}" ]] || return 0;local _line _in_section=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" ]] || continue;[[ "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^${_section}:[[:space:]]*$ ]];then _in_section=1; continue;fi;if (( _in_section ));then [[ "${_line}" =~ ^[[:space:]] ]] || break;if [[ "${_line}" =~ ^[[:space:]]+([^[:space:]:]+):[[:space:]]*(.*) ]];then local _k="${BASH_REMATCH[1]}";_k="${_k%\"}" ; _k="${_k#\"}";echo "${_k}=$(argsh::lib::_yaml_clean "${BASH_REMATCH[2]}")";fi;fi;done < "${_file}";};argsh::lib::_lock_get() { local _entry="${1}" _field="${2}" _file="${3}";[[ -f "${_file}" ]] || return 0;local _line _in_libs=0 _in_entry=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" ]] || continue;[[ "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^libs:[[:space:]]*$ ]];then _in_libs=1; continue;fi;if (( _in_libs ));then [[ "${_line}" =~ ^[[:space:]] ]] || break;local _stripped="${_line#"${_line%%[![:space:]]*}"}";local _ek="${_stripped%%:*}";_ek="${_ek%\"}" ; _ek="${_ek#\"}";if [[ "${_ek}" == "${_entry}" ]];then _in_entry=1; continue;fi;if (( _in_entry ));then if [[ "${_line}" =~ ^[[:space:]][[:space:]][^[:space:]] ]];then break;fi;if [[ "${_stripped}" =~ ^${_field}:[[:space:]]+(.*) ]];then local _val="${BASH_REMATCH[1]}";_val="${_val%\"}" ; _val="${_val#\"}";echo "${_val}";return 0;fi;fi;fi;done < "${_file}";};declare -gr __ARGSH_GLOBAL_LIBS="${XDG_DATA_HOME:-${HOME}/.local/share}/argsh/libs";argsh::lib::dir() { local _dir="${PATH_BASE:-.}";if [[ -f "${_dir}/.argsh.yaml" ]];then local _custom;_custom="$(argsh::lib::_yaml_get "defaults.path_libs" "${_dir}/.argsh.yaml")";if [[ -n "${_custom}" ]];then if [[ "${_custom:0:1}" == "/" ]]; then echo "${_custom}"; else echo "${_dir}/${_custom}"; fi;return;fi;fi;echo "${_dir}/.argsh/libs";};argsh::lib::resolve() { local _ref="${1}";local _provider _name _registry;if [[ "${_ref}" == *@* ]];then _provider="${_ref%%@*}";_name="${_ref#*@}";else _provider="argsh";_name="${_ref}";fi;if [[ ! "${_provider}" =~ ^[a-zA-Z0-9_-]+$ ]];then echo "argsh: invalid provider name: ${_provider}" >&2;return 1;fi;if [[ "${_provider}" == "argsh" ]];then _registry="${__ARGSH_LIB_REGISTRY}";else local _dir="${PATH_BASE:-.}";if [[ -f "${_dir}/.argsh.yaml" ]];then local _line _in_registries=0 _in_provider=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" && "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^registries:[[:space:]]*$ ]]; then _in_registries=1; continue; fi;if (( _in_registries ));then [[ "${_line}" =~ ^[[:space:]] ]] || break;if [[ "${_line}" =~ ^[[:space:]]+${_provider}:[[:space:]]*$ ]]; then _in_provider=1; continue; fi;if (( _in_provider ));then [[ "${_line}" =~ ^[[:space:]]{4,} ]] || { _in_provider=0; continue; };local _stripped="${_line#"${_line%%[![:space:]]*}"}";if [[ "${_stripped}" =~ ^endpoint:[[:space:]]+(.*) ]];then _registry="$(argsh::lib::_yaml_clean "${BASH_REMATCH[1]}")";break;fi;fi;fi;done < "${_dir}/.argsh.yaml";fi;if [[ -z "${_registry}" ]];then echo "argsh: unknown registry provider: ${_provider}" >&2;return 1;fi;fi;echo "${_registry}";echo "${_name}";};argsh::lib::_curl_fallback() { local _name="${1}" _tag="${2}" _dest="${3}";local _normalized_tag="${_tag#v}";local _url="https://github.com/arg-sh/libs/releases/download/${_name}%2Fv${_normalized_tag}/${_name}-${_normalized_tag}.tar.gz";if [[ "${_tag}" == "latest" ]];then local _latest_tag;local -a _curl_args=(-fsSL);if [[ -n "${GITHUB_TOKEN:-${GH_TOKEN:-}}" ]];then _curl_args+=(-H "Authorization: token ${GITHUB_TOKEN:-${GH_TOKEN}}");fi;_latest_tag="$(curl "${_curl_args[@]}" "https://api.github.com/repos/arg-sh/libs/releases?per_page=100" | grep -o "\"tag_name\": *\"${_name}/v[^\"]*\"" | head -1 | sed 's/.*"\('"${_name}"'\/v[^"]*\)".*/\1/')" || _latest_tag="";if [[ -z "${_latest_tag}" ]];then echo "argsh: no release found for ${_name}" >&2;return 1;fi;_normalized_tag="${_latest_tag#*/v}";_url="https://github.com/arg-sh/libs/releases/download/${_name}%2Fv${_normalized_tag}/${_name}-${_normalized_tag}.tar.gz";fi;local _tmpfile; _tmpfile="$(mktemp)";if ! curl -fsSL "${_url}" -o "${_tmpfile}";then echo "argsh: failed to download ${_name}" >&2;rm -f "${_tmpfile}";return 1;fi;local _entry _unsafe=0;while IFS= read -r _entry;do _entry="${_entry#./}";[[ -n "${_entry}" ]] || continue;if [[ "${_entry}" == /* || "${_entry}" == ".." || "${_entry}" == ../* || "${_entry}" == */../* || "${_entry}" == */.. ]];then _unsafe=1; break;fi;done < <(tar -tzf "${_tmpfile}" 2>/dev/null) || _unsafe=1;if (( ! _unsafe ));then local _line;while IFS= read -r _line;do case "${_line}" in
l*|h*)_unsafe=1; break;;
esac;done < <(tar -tvzf "${_tmpfile}" 2>/dev/null);fi;if (( _unsafe ));then echo "argsh: tarball contains unsafe paths, refusing to extract" >&2;rm -f "${_tmpfile}";return 1;fi;if ! tar xzf "${_tmpfile}" -C "${_dest}" --strip-components=1 --no-same-owner;then echo "argsh: failed to extract ${_name}" >&2;rm -f "${_tmpfile}";return 1;fi;rm -f "${_tmpfile}";local _arch _libc="" _suffix;_arch="$(argsh::builtin::arch 2>/dev/null)" || _arch="";if [[ -n "${_arch}" ]];then command -v ldd &>/dev/null && ldd --version 2>&1 | grep -qi musl && _libc="-musl";_suffix="-linux${_libc}-${_arch}.so";local _so _canonical;for _so in "${_dest}"/*-linux-*.so;do [[ -f "${_so}" ]] || continue;if [[ "${_so}" == *"${_suffix}" ]];then _canonical="${_so%"${_suffix}"}.so";mv "${_so}" "${_canonical}";else rm -f "${_so}";fi;done;fi;};argsh::lib::_write_lock_entry() { local _key="${1}" _ref="${2}" _digest="${3}";local _dir="${PATH_BASE:-.}";local _lock="${LOCK_FILE:-${_dir}/.argsh.lock}";if [[ ! -f "${_lock}" ]];then printf '%s\n' "# auto-generated by argsh — do not edit" "libs:" > "${_lock}";fi;argsh::lib::_remove_lock_entry "${_key}";grep -q '^libs:' "${_lock}" 2>/dev/null || echo "libs:" >> "${_lock}";printf '  "%s":\n    ref: %s\n    digest: %s\n' "${_key}" "${_ref}" "${_digest}" >> "${_lock}";};argsh::lib::_remove_lock_entry() { local _key="${1}";local _dir="${PATH_BASE:-.}";local _lock="${LOCK_FILE:-${_dir}/.argsh.lock}";[[ -f "${_lock}" ]] || return 0;local _tmp _line _in_entry=0 _matched=0;_tmp="$(mktemp "${_lock}.XXXXXX")";while IFS= read -r _line || [[ -n "${_line}" ]];do local _stripped="${_line#"${_line%%[![:space:]]*}"}";local _ek="${_stripped%%:*}";_ek="${_ek%\"}" ; _ek="${_ek#\"}";if [[ "${_line}" =~ ^[[:space:]][[:space:]][^[:space:]] && "${_ek}" == "${_key}" ]];then _in_entry=1; _matched=1; continue;fi;if (( _in_entry ));then if [[ "${_line}" =~ ^[[:space:]]{4,} ]]; then continue; fi;_in_entry=0;fi;printf '%s\n' "${_line}";done < "${_lock}" > "${_tmp}";if (( _matched )); then mv "${_tmp}" "${_lock}"; else rm -f "${_tmp}"; fi;};argsh::lib::_install_deps() { local _dest="${1}" _global="${2:-0}";[[ -f "${_dest}/argsh-plugin.yml" ]] || return 0;local _lib_dir;if (( _global ));then _lib_dir="${__ARGSH_GLOBAL_LIBS}";else _lib_dir="$(argsh::lib::dir)";fi;local _line _in_depends=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" && "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^depends:[[:space:]]*$ ]]; then _in_depends=1; continue; fi;if (( _in_depends ));then [[ "${_line}" =~ ^[[:space:]] ]] || break;if [[ "${_line}" =~ ^[[:space:]]+-[[:space:]]+(.*) ]];then local _dep="${BASH_REMATCH[1]}";_dep="${_dep%\"}" ; _dep="${_dep#\"}";_dep="${_dep%\'}" ; _dep="${_dep#\'}";_dep="${_dep%%[[:space:]]\#*}";_dep="${_dep%"${_dep##*[![:space:]]}"}";[[ -n "${_dep}" ]] || continue;local _dep_name="${_dep}";_dep_name="${_dep_name%@[0-9v]*}";_dep_name="${_dep_name%@latest}";[[ "${_dep_name}" != *@* ]] || _dep_name="${_dep_name#*@}";if [[ -d "${_lib_dir}/${_dep_name}" ]];then continue;fi;echo "argsh: installing dependency ${_dep}..." >&2;local -a _add_args=();(( _global )) && _add_args+=(--global);argsh::lib::add "${_add_args[@]}" "${_dep}" || { echo "argsh: warning: failed to install dependency ${_dep}" >&2;};fi;fi;done < "${_dest}/argsh-plugin.yml";};argsh::lib::add() { local _global=0 _expect_digest="";while [[ "${1:-}" == --* ]];do case "${1}" in
--global)_global=1; shift;;
--expect-digest)shift; [[ -n "${1:-}" && "${1:-}" != --* ]] || { echo "argsh lib add: --expect-digest requires a value" >&2; return 1; }; _expect_digest="${1}"; shift;;
*)echo "argsh lib add: unknown option: ${1}" >&2; return 1;;
esac;done;local _ref="${1:-}";[[ -n "${_ref}" ]] || { echo "argsh lib add: specify a library (e.g. argsh@data)" >&2; return 1; };local _version="";if [[ "${_ref}" == *@*@* ]];then _version="${_ref##*@}";_ref="${_ref%@*}";elif [[ "${_ref}" == *@* ]];then local _after="${_ref#*@}";if [[ "${_after}" =~ ^v?[0-9] || "${_after}" == "latest" ]];then _version="${_after}";_ref="${_ref%%@*}";fi;fi;local _registry _name;{ read -r _registry; read -r _name; } < <(argsh::lib::resolve "${_ref}") || { echo "argsh lib add: failed to resolve '${_ref}'" >&2; return 1;};if [[ ! "${_name}" =~ ^[a-zA-Z0-9_-]+$ ]];then echo "argsh: invalid library name: ${_name}" >&2;return 1;fi;local _tag="${_version:-latest}";local _oci_ref="${_registry}/${_name}:${_tag}";local _lib_dir;if (( _global ));then _lib_dir="${__ARGSH_GLOBAL_LIBS}";else _lib_dir="$(argsh::lib::dir)";fi;local _dest="${_lib_dir}/${_name}";echo "argsh: downloading ${_name} (${_tag})..." >&2;__LIB_PULL_DIGEST="";mkdir -p "${_lib_dir}";local _tmp_dest _tmpfile="";_tmp_dest="$(mktemp -d "${_lib_dir}/.tmp.XXXXXX")";if [[ "$(type -t lib::pull 2>/dev/null)" == "builtin" ]];then local _host="${_registry%%/*}";local _repo_prefix="${_registry#*/}";[[ "${_host}" != "${_repo_prefix}" ]] || _repo_prefix="";local _repo="${_name}";[[ -z "${_repo_prefix}" ]] || _repo="${_repo_prefix}/${_name}";lib::pull "${_host}" "${_repo}" "${_tag}" "${_tmp_dest}" || { echo "argsh: OCI pull failed, trying GitHub releases fallback..." >&2;rm -rf "${_tmp_dest}"; _tmp_dest="$(mktemp -d "${_lib_dir}/.tmp.XXXXXX")";argsh::lib::_curl_fallback "${_name}" "${_tag}" "${_tmp_dest}" || { rm -rf "${_tmp_dest}"; return 1;};};else argsh::lib::_curl_fallback "${_name}" "${_tag}" "${_tmp_dest}" || { rm -rf "${_tmp_dest}"; return 1;};fi;if [[ -n "${_expect_digest}" ]];then if [[ -z "${__LIB_PULL_DIGEST:-}" ]];then echo "argsh: warning: digest verification unavailable (no OCI digest from pull)" >&2;elif [[ "${__LIB_PULL_DIGEST}" != "${_expect_digest}" ]];then echo "argsh: digest mismatch for ${_name}" >&2;echo "  expected: ${_expect_digest}" >&2;echo "  got:      ${__LIB_PULL_DIGEST}" >&2;rm -rf "${_tmp_dest}";return 1;fi;fi;if [[ -d "${_dest}" ]];then local _old="${_dest}.old.$$";mv "${_dest}" "${_old}";if ! mv "${_tmp_dest}" "${_dest}";then mv "${_old}" "${_dest}" 2>/dev/null || true;rm -rf "${_tmp_dest}";echo "argsh: failed to install ${_name}" >&2;return 1;fi;rm -rf "${_old}";else mv "${_tmp_dest}" "${_dest}";fi;echo "argsh: installed ${_name} to ${_dest}" >&2;argsh::lib::_check_requires "${_dest}";argsh::lib::_install_deps "${_dest}" $(( _global ));if (( _global ));then local _digest="${__LIB_PULL_DIGEST:-}";if [[ -z "${_digest}" ]];then local _sha_cmd="sha256sum";command -v sha256sum &>/dev/null || _sha_cmd="shasum -a 256";_digest="sha256:$(tar cf - --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner -C "${_lib_dir}" "${_name}" 2>/dev/null | ${_sha_cmd} | cut -d' ' -f1)";fi;local _lock_key="${_ref}";[[ "${_lock_key}" == *@* ]] || _lock_key="argsh@${_lock_key}";LOCK_FILE="${__ARGSH_GLOBAL_LIBS}/.argsh-global.lock" argsh::lib::_write_lock_entry "${_lock_key}" "${_oci_ref}" "${_digest}";return 0;fi;local _dir="${PATH_BASE:-.}";if [[ -f "${_dir}/.argsh.yaml" ]];then local _entry="${_ref}";[[ "${_ref}" == *@* ]] || _entry="argsh@${_ref}";if grep -q "^  \"\{0,1\}${_entry}\"\{0,1\}:" "${_dir}/.argsh.yaml" 2>/dev/null;then local _tmp_yaml; _tmp_yaml="$(mktemp "${_dir}/.argsh.yaml.XXXXXX")";while IFS= read -r _yline || [[ -n "${_yline}" ]];do if [[ "${_yline}" =~ ^[[:space:]]+\"?${_entry}\"?: ]];then printf '  %s: "%s"\n' "${_entry}" "${_version:-latest}";else printf '%s\n' "${_yline}";fi;done < "${_dir}/.argsh.yaml" > "${_tmp_yaml}";mv "${_tmp_yaml}" "${_dir}/.argsh.yaml";elif grep -q '^libs:' "${_dir}/.argsh.yaml" 2>/dev/null;then local _tmp_yaml; _tmp_yaml="$(mktemp "${_dir}/.argsh.yaml.XXXXXX")";local _in_libs=0 _inserted=0;while IFS= read -r _yline || [[ -n "${_yline}" ]];do printf '%s\n' "${_yline}";if [[ "${_yline}" =~ ^libs:[[:space:]]*$ ]]; then _in_libs=1; fi;if (( _in_libs && ! _inserted ));then if [[ "${_yline}" =~ ^libs: ]] || [[ "${_yline}" =~ ^[[:space:]] ]]; then :;else printf '  %s: "%s"\n' "${_entry}" "${_version:-latest}";_inserted=1;fi;fi;done < "${_dir}/.argsh.yaml" > "${_tmp_yaml}";(( _inserted )) || printf '  %s: "%s"\n' "${_entry}" "${_version:-latest}" >> "${_tmp_yaml}";mv "${_tmp_yaml}" "${_dir}/.argsh.yaml";else printf '\nlibs:\n  %s: "%s"\n' "${_entry}" "${_version:-latest}" >> "${_dir}/.argsh.yaml";fi;fi;local _digest="${__LIB_PULL_DIGEST:-}";if [[ -z "${_digest}" ]];then local _sha_cmd="sha256sum";command -v sha256sum &>/dev/null || _sha_cmd="shasum -a 256";_digest="sha256:$(tar cf - --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner -C "${_lib_dir}" "${_name}" 2>/dev/null | ${_sha_cmd} | cut -d' ' -f1)";fi;local _lock_key="${_ref}";[[ "${_lock_key}" == *@* ]] || _lock_key="argsh@${_lock_key}";argsh::lib::_write_lock_entry "${_lock_key}" "${_oci_ref}" "${_digest}";};argsh::lib::list() { local _lib_dir;if [[ "${1:-}" == "--global" ]];then _lib_dir="${__ARGSH_GLOBAL_LIBS}";else _lib_dir="$(argsh::lib::dir)";fi;if [[ ! -d "${_lib_dir}" ]];then echo "No libraries installed (${_lib_dir} not found)";return;fi;local _d;for _d in "${_lib_dir}"/*/;do [[ -d "${_d}" ]] || continue;local _name _version="?";_name="$(basename "${_d}")";if [[ -f "${_d}/argsh-plugin.yml" ]];then _version="$(argsh::lib::_yaml_get "version" "${_d}/argsh-plugin.yml")";[[ -n "${_version}" ]] || _version="?";fi;echo "${_name} (${_version})";done;};argsh::lib::remove() { local _global=0;if [[ "${1:-}" == "--global" ]];then _global=1; shift;fi;local _name="${1:-}";[[ -n "${_name}" ]] || { echo "argsh lib remove: specify a library name" >&2; return 1; };if [[ ! "${_name}" =~ ^[a-zA-Z0-9_-]+$ ]];then echo "argsh: invalid library name: ${_name}" >&2;return 1;fi;local _lib_dir;if (( _global ));then _lib_dir="${__ARGSH_GLOBAL_LIBS}";else _lib_dir="$(argsh::lib::dir)";fi;local _dest="${_lib_dir}/${_name}";if [[ ! -d "${_dest}" ]];then echo "argsh: library '${_name}' not found in ${_lib_dir}" >&2;return 1;fi;rm -rf "${_dest}";echo "argsh: removed ${_name}";if (( _global ));then LOCK_FILE="${__ARGSH_GLOBAL_LIBS}/.argsh-global.lock" argsh::lib::_remove_lock_entry "${_name}";LOCK_FILE="${__ARGSH_GLOBAL_LIBS}/.argsh-global.lock" argsh::lib::_remove_lock_entry "argsh@${_name}";else argsh::lib::_remove_lock_entry "${_name}";argsh::lib::_remove_lock_entry "argsh@${_name}";fi;};argsh::lib::install() { local _dir="${PATH_BASE:-.}";if [[ -f "${_dir}/.argsh.lock" ]];then local _lib _oci_ref _failed=0;local _entry_key="" _entry_digest="" _in_libs=0;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" && "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^libs:[[:space:]]*$ ]]; then _in_libs=1; continue; fi;if (( _in_libs )) && [[ "${_line}" =~ ^[^[:space:]] ]]; then break; fi;(( _in_libs )) || continue;if [[ "${_line}" =~ ^[[:space:]][[:space:]]([^[:space:]:\"]+|\"[^\"]+\"):$ ]];then _entry_key="${BASH_REMATCH[1]}";_entry_key="${_entry_key%\"}" ; _entry_key="${_entry_key#\"}";_entry_digest="";continue;fi;if [[ -n "${_entry_key}" && "${_line}" =~ ^[[:space:]]{4,}digest:[[:space:]]+(.*) ]];then _entry_digest="${BASH_REMATCH[1]}";_entry_digest="${_entry_digest%\"}" ; _entry_digest="${_entry_digest#\"}";fi;if [[ -n "${_entry_key}" && "${_line}" =~ ^[[:space:]]{4,}ref:[[:space:]]+(.*) ]];then _oci_ref="${BASH_REMATCH[1]}";_oci_ref="${_oci_ref%\"}" ; _oci_ref="${_oci_ref#\"}";echo "Installing ${_entry_key} (locked: ${_oci_ref})...";local _tag="${_oci_ref##*:}";local _ref="${_entry_key}";[[ -z "${_tag}" || "${_tag}" == "${_oci_ref}" ]] || _ref="${_entry_key}@${_tag}";local -a _add_args=();[[ -z "${_entry_digest}" ]] || _add_args+=(--expect-digest "${_entry_digest}");argsh::lib::add "${_add_args[@]}" "${_ref}" || { echo "argsh: failed to install ${_entry_key}" >&2; _failed=1; };_entry_key="";fi;done < "${_dir}/.argsh.lock";return "${_failed}";fi;if [[ ! -f "${_dir}/.argsh.yaml" ]];then echo "argsh: no .argsh.yaml found" >&2;return 1;fi;local _lib _version _ref _failed=0;while IFS='=' read -r _lib _version;do [[ -n "${_lib}" ]] || continue;_version="${_version#^}";_version="${_version#~}";echo "Installing ${_lib} (${_version})..." >&2;_ref="${_lib}";[[ -z "${_version}" || "${_version}" == "latest" ]] || _ref="${_lib}@${_version}";argsh::lib::add "${_ref}" || { echo "argsh: failed to install ${_lib}" >&2; _failed=1; };done < <(argsh::lib::_yaml_entries "libs" "${_dir}/.argsh.yaml");return "${_failed}";};argsh::lib::update() { local _global=0;if [[ "${1:-}" == "--global" ]];then _global=1; shift;fi;if (( _global ));then local _glock="${__ARGSH_GLOBAL_LIBS}/.argsh-global.lock";if [[ ! -f "${_glock}" ]];then echo "argsh: no global lockfile found (${_glock})" >&2;return 1;fi;local _entry_key="" _in_libs=0 _failed=0 _line;while IFS= read -r _line || [[ -n "${_line}" ]];do [[ -n "${_line}" && "${_line}" != \#* ]] || continue;if [[ "${_line}" =~ ^libs:[[:space:]]*$ ]]; then _in_libs=1; continue; fi;if (( _in_libs )) && [[ "${_line}" =~ ^[^[:space:]] ]]; then break; fi;(( _in_libs )) || continue;if [[ "${_line}" =~ ^[[:space:]][[:space:]]([^[:space:]:\"]+|\"[^\"]+\"):$ ]];then _entry_key="${BASH_REMATCH[1]}";_entry_key="${_entry_key%\"}" ; _entry_key="${_entry_key#\"}";echo "Updating ${_entry_key}...";argsh::lib::add --global "${_entry_key}" || { echo "argsh: failed to update ${_entry_key}" >&2; _failed=1; };fi;done < "${_glock}";return "${_failed}";fi;local _dir="${PATH_BASE:-.}";if [[ ! -f "${_dir}/.argsh.yaml" ]];then echo "argsh: no .argsh.yaml found" >&2;return 1;fi;local _lib _failed=0;while IFS='=' read -r _lib _;do [[ -n "${_lib}" ]] || continue;echo "Updating ${_lib}...";argsh::lib::add "${_lib}" || { echo "argsh: failed to update ${_lib}" >&2; _failed=1; };done < <(argsh::lib::_yaml_entries "libs" "${_dir}/.argsh.yaml");return "${_failed}";};argsh::lib::publish() { local _registry="";while [[ "${1:-}" == --* ]];do case "${1}" in
--registry)shift; _registry="${1:-}"; shift;;
*)echo "argsh lib publish: unknown option: ${1}" >&2; return 1;;
esac;done;if [[ ! -f "argsh-plugin.yml" ]];then echo "argsh lib publish: no argsh-plugin.yml in current directory" >&2;return 1;fi;local _name _version;_name="$(argsh::lib::_yaml_get "name" argsh-plugin.yml)";_version="$(argsh::lib::_yaml_get "version" argsh-plugin.yml)";if [[ -z "${_name}" || -z "${_version}" ]];then echo "argsh lib publish: argsh-plugin.yml must have 'name' and 'version' fields" >&2;return 1;fi;if [[ ! "${_name}" =~ ^[a-zA-Z0-9_-]+$ ]];then echo "argsh: invalid library name: ${_name}" >&2;return 1;fi;if [[ ! "${_version}" =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[a-zA-Z0-9.]+)?$ ]];then echo "argsh lib publish: version '${_version}' is not valid semver (expected x.y.z)" >&2;return 1;fi;if [[ -z "${_registry}" ]];then _registry="${__ARGSH_LIB_REGISTRY}";fi;if [[ "$(type -t lib::push 2>/dev/null)" != "builtin" ]];then echo "argsh lib publish: requires argsh builtin (.so) with OCI push support" >&2;echo "  Run: argsh builtin install" >&2;return 1;fi;echo "argsh: publishing ${_name} v${_version} to ${_registry}..." >&2;lib::push "${_registry}" "${_name}" "${_version}" "." || { echo "argsh lib publish: push failed" >&2;return 1;};echo "argsh: published ${_registry}/${_name}:${_version}" >&2;echo "  digest: ${__LIB_PUSH_DIGEST:-unknown}" >&2;};argsh::lib::search() { local _filter="${1:-}";local -a _curl_args=(-fsSL);if [[ -n "${GITHUB_TOKEN:-${GH_TOKEN:-}}" ]];then _curl_args+=(-H "Authorization: token ${GITHUB_TOKEN:-${GH_TOKEN}}");fi;local _response;_response="$(curl "${_curl_args[@]}" "https://api.github.com/repos/arg-sh/libs/releases?per_page=100")" || { echo "argsh search: failed to query GitHub API" >&2;return 1;};if [[ -z "${_response}" ]];then echo "argsh search: no releases found" >&2;return 1;fi;local -A _latest=();local -a _order=();local _tag _name _version;while IFS= read -r _tag;do [[ "${_tag}" == */* ]] || continue;_name="${_tag%%/*}";_version="${_tag#*/v}";if [[ -n "${_filter}" && "${_name}" != *"${_filter}"* ]];then continue;fi;if [[ -z "${_latest["${_name}"]:-}" ]];then _latest["${_name}"]="${_version}";_order+=("${_name}");fi;done < <(grep -o '"tag_name": *"[^"]*"' <<< "${_response}" | sed 's/"tag_name": *"//; s/"$//');if [[ ${#_order[@]} -eq 0 ]];then if [[ -n "${_filter}" ]];then echo "No libraries found matching '${_filter}'";else echo "No libraries found";fi;return 0;fi;echo "Available libraries:";local _n;for _n in "${_order[@]}";do echo "  ${_n} (${_latest["${_n}"]})";done;};argsh::discover_dirs() { local -a _raw_dirs=();_search_dirs=();local _d _existing _skip _rd _re;if [[ -n "${PATH_TESTS:-}" ]];then IFS=';' read -ra _raw_dirs <<< "${PATH_TESTS}";fi;_raw_dirs+=( "${BASH_SOURCE[0]%/*}" "${PATH_BASE:-.}" "${PATH_BASE:-.}/test" "${PATH_BASE:-.}/tests" "${PATH_BASE:-.}/libraries" );for _d in "${_raw_dirs[@]}";do [[ -d "${_d}" ]] || continue;_skip=0;_rd="$(realpath "${_d}" 2>/dev/null || echo "${_d}")";for _existing in "${_search_dirs[@]}";do _re="$(realpath "${_existing}" 2>/dev/null || echo "${_existing}")";[[ "${_rd}" != "${_re}" ]] || { _skip=1; break; };done;(( _skip )) || _search_dirs+=("${_d}");done;};argsh::discover_files() { local -a _search_dirs=();argsh::discover_dirs;local _d _f _pattern;for _d in "${_search_dirs[@]}";do [[ -d "${_d}" ]] || continue;for _pattern in "${@}";do for _f in "${_d}"/${_pattern};do [[ -f "${_f}" ]] || continue;_found_files+=("${_f}");done;done;done;};argsh::status() { echo "argsh ${ARGSH_VERSION:-unknown} (${ARGSH_COMMIT_SHA:-unknown})";echo "  script: $(realpath "${BASH_SOURCE[0]}" 2>/dev/null || echo "${BASH_SOURCE[0]}")";echo "";local _loc _arch _so_status;_loc="$(argsh::builtin::location 2>/dev/null)" || _loc="not installed";_arch="$(argsh::builtin::arch 2>/dev/null)" || _arch="unsupported";if (( ${ARGSH_BUILTIN:-0} ));then _so_status="loaded";else _so_status="not loaded";fi;echo "Builtin (.so):";echo "  status:       ${_so_status}";if (( ${ARGSH_BUILTIN:-0} ));then echo "  version:      ${__ARGSH_BUILTIN_VERSION:-unknown}";echo "  commit:       ${__ARGSH_BUILTIN_COMMIT:-unknown}";fi;echo "  path:         ${_loc}";echo "  architecture: $(uname -s | tr '[:upper:]' '[:lower:]')/${_arch}";echo "";echo "Shell:";echo "  bash: ${BASH_VERSION:-unknown}";echo "";echo "Features:";if (( ${ARGSH_BUILTIN:-0} ));then echo "  mcp:        available (builtin)";echo "  completion: available (builtin)";echo "  docgen:     available (builtin)";else echo "  mcp:        requires builtin";echo "  completion: requires builtin";echo "  docgen:     requires builtin";fi;echo "";local -a _found_files=();argsh::discover_files "*.bats";if (( ${#_found_files[@]} > 0 ));then echo "Tests: ${#_found_files[@]} .bats file(s)";local _f;for _f in "${_found_files[@]}";do echo "  ${_f}";done;else echo "Tests: none found";fi;local -a _cov_files=();local _d _cov_file;for _d in "${PATH_BASE:-.}" "${PATH_BASE:-.}"/*/;do _d="${_d%/}";[[ -f "${_d}/coverage.json" ]] && _cov_files+=("${_d}/coverage.json");done;if (( ${#_cov_files[@]} > 0 ));then echo "Coverage:";for _cov_file in "${_cov_files[@]}";do local _pct="?" _date="?";_pct="$(grep -o '"percent_covered"[^"]*"[^"]*"' "${_cov_file}" | tail -1)" && [[ "${_pct}" =~ \"([0-9.]+)\"$ ]] && _pct="${BASH_REMATCH[1]}";_date="$(grep -o '"date"[^"]*"[^"]*"' "${_cov_file}")" && [[ "${_date}" =~ \"([^\"]+)\"$ ]] && _date="${BASH_REMATCH[1]}";echo "  ${_cov_file##"${PATH_BASE:-.}"/}: ${_pct}% (${_date})";done;else echo "Coverage: no coverage.json found";fi;};argsh::_docker_forward() { binary::exists docker 2>/dev/null || { echo "argsh: this command requires either the tool installed locally or Docker" >&2;return 1;};local tty="";if [[ -t 0 ]];then tty="-it";elif [[ -p /dev/stdin ]];then tty="-i";fi;local -r a36="${ARGSH_DOCKER_IMAGE:-ghcr.io/arg-sh/argsh:latest}";local -a _env_candidates=();local _var _name;while IFS='=' read -r _var _;do _name="${_var#ARGSH_ENV_}";[[ -n "${_name}" ]] || continue;[[ "${_name}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue;_env_candidates+=("${_name}=${!_var}");done < <(compgen -e ARGSH_ENV_ 2>/dev/null || :);( local -a _docker_env_flags=();local _kv;for _kv in "${_env_candidates[@]}";do if export "${_kv}" 2>/dev/null;then _docker_env_flags+=(-e "${_kv%%=*}");fi;done;docker run --rm ${tty} $(docker::user) -e "BATS_LOAD" -e "ARGSH_SOURCE" -e "PATH_TESTS" -e "PATH_SCRIPTS" -e "GIT_COMMIT_SHA=$(git rev-parse HEAD 2>/dev/null || :)" -e "GIT_VERSION=$(git describe --tags --dirty 2>/dev/null || :)" "${_docker_env_flags[@]}" "${a36}" "${@}";);};argsh::minify() { if ! binary::exists minifier 2>/dev/null;then argsh::_docker_forward minify "${@}";return;fi;local template out="/dev/stdout";local -a files ignore_variable args=( 'files'              "Files to minify, can be a glob pattern" 'template|t:~file'   "Path to a template file to use for the minified file" 'out|o'              "Path to the output file" 'ignore-variable|i'  "Ignores specific variable names from obfuscation" );:args "Minify Bash files" "${@}";! is::uninitialized files || { :args::error_usage "No files to minify";return 1;};( local _content _tout;_content="$(mktemp)";_tout="$(mktemp)";trap "rm -f ${_content} ${_tout}" EXIT;local _f _file;local -a _glob;for _f in "${files[@]}";do if [[ -d "${_f}" ]];then _glob=("${_f}"/*.{sh,bash});else _glob=(${_f});fi;for _file in "${_glob[@]}";do [[ -e "${_file}" ]] || continue;{ cat "${_file}";echo;} >>"${_content}";done;done;local -a _iVars=();if ! is::uninitialized ignore_variable && (( ${#ignore_variable[@]} ));then _iVars=(-I "$(array::join "," "${ignore_variable[@]}")");fi;minifier -i "${_content}" -o "${_tout}" -O "${_iVars[@]}";local -r data="$(cat "${_tout}")";if [[ -z "${template:-}" ]];then echo -n "${data}" >"${out}";exit 0;fi;binary::exists envsubst 2>/dev/null || { echo "argsh: envsubst is required for -t/--template (install gettext)" >&2;exit 1;};local commit_sha="${GIT_COMMIT_SHA:-}";local version="${GIT_VERSION:-}";export data commit_sha version;envsubst '$data,$commit_sha,$version' <"${template}" >"${out}";);};argsh::lint() { local only_argsh=0;local only_shellcheck=0;local -a files args=( 'only-argsh|:+'      "Skip shellcheck, run only argsh-lint" 'only-shellcheck|:+' "Skip argsh-lint, run only shellcheck" 'files'              "Files to lint, can be a glob pattern" );:args "Lint Bash files" "${@}";if (( only_argsh )) && (( only_shellcheck ));then echo "argsh lint: --only-argsh and --only-shellcheck are mutually exclusive" >&2;return 2;fi;if (( ! only_argsh )) && ! binary::exists shellcheck 2>/dev/null;then argsh::_docker_forward lint "${@}";return;fi;if (( ! only_shellcheck )) && ! binary::exists argsh-lint 2>/dev/null;then argsh::_docker_forward lint "${@}";return;fi;if is::uninitialized files;then local -a _found_files=();argsh::discover_files "*.sh" "*.bash" "*.bats";local -a _search_dirs=();argsh::discover_dirs;local _d _f;for _d in "${_search_dirs[@]}";do [[ -d "${_d}" ]] || continue;for _f in "${_d}"/*;do [[ -f "${_f}" ]] || continue;local _basename="${_f##*/}";[[ "${_basename}" != *.* ]] || continue;local _shebang _is_shell=0;_shebang="$(head -1 "${_f}" 2>/dev/null || :)";local _interp="${_shebang#\#!}";_interp="${_interp# }";case "${_interp}" in
*/env|*/env\ *)_interp="${_interp#*env}";_interp="${_interp# }";[[ "${_interp}" != -S* ]] || { _interp="${_interp#-S}"; _interp="${_interp# }"; };;
esac;local _first="${_interp%% *}";_first="${_first##*/}";case "${_first}" in
bash|sh|argsh)_is_shell=1;;
esac;(( _is_shell )) && _found_files+=("${_f}");done;done;if (( ${#_found_files[@]} == 0 ));then echo "No files to lint (set PATH_TESTS or pass files as arguments)" >&2;return 1;fi;files=("${_found_files[@]}");fi;local _file _f;local -a _glob _expanded=();for _f in "${files[@]}";do if [[ -d "${_f}" ]];then _glob=("${_f}"/*.{sh,bash,bats});else _glob=(${_f});fi;for _file in "${_glob[@]}";do [[ -e "${_file}" ]] || continue;_expanded+=("${_file}");done;done;local _rc=0;if (( ! only_argsh ));then for _file in "${_expanded[@]}";do echo "Linting ${_file}" >&2;shellcheck "${_file}" || _rc=1;done;fi;if (( ! only_shellcheck ));then for _file in "${_expanded[@]}";do echo "argsh-lint ${_file}" >&2;argsh-lint "${_file}" || _rc=1;done;fi;return "${_rc}";};argsh::test() { if ! binary::exists bats 2>/dev/null;then argsh::_docker_forward test "${@}";return;fi;local -a path args=( 'path'  "Path to the bats test files" );:args "Run tests" "${@}";if is::uninitialized path;then local -a _found_files=();argsh::discover_files "*.bats";if (( ${#_found_files[@]} == 0 ));then echo "No test files found (set PATH_TESTS or pass files as arguments)" >&2;return 1;fi;path=("${_found_files[@]}");fi;[[ -z "${BATS_LOAD:-}" ]] || echo "Running tests for ${BATS_LOAD}" >&2;bats "${path[@]}";};argsh::coverage() { if ! binary::exists kcov 2>/dev/null || ! binary::exists jq 2>/dev/null;then argsh::_docker_forward coverage "${@}";return;fi;local out="./coverage" min=75;local -a tests=(".") args=( 'tests'     "Path to the bats test files" 'out|o'     "Path to the output directory" 'min|:~int' "Minimum coverage required" );:args "Generate coverage report for your Bash scripts" "${@}";echo "Generating coverage report for: ${tests[*]}" >&2;kcov --clean --bash-dont-parse-binary-dir --include-pattern=.sh --exclude-pattern=tests --include-path=. "${out}" bats "${tests[@]}" >/dev/null 2>&1 || { echo "Failed to generate coverage report";echo "Run tests with 'argsh test' to see what went wrong";return 1;} >&2;cp "${out}"/bats.*/coverage.json "${out}/coverage.json";local coverage;coverage="$(jq -r '.percent_covered | tonumber | floor' "${out}/coverage.json")";echo "Coverage is ${coverage}% of required ${min}%";(( coverage >= min )) || return 1;};argsh::docs() { if ! binary::exists shdoc 2>/dev/null;then argsh::_docker_forward docs "${@}";return;fi;local in out prefix="";local -a args=( 'in'      "Path to the source files to generate documentation from, can be a glob pattern" 'out'     "Path to the output directory" 'prefix'  "Prefix for each md file" );:args "Generate documentation" "${@}";[[ -d "${out}" ]] || { :args::error_usage "out is not a directory";return 1;};local -a a3=(-o "${out}");[[ -z "${prefix}" ]] || a3+=(-p "${prefix}");shdoc "${a3[@]}" ${in};};argsh::main() { local -a a47=( '-'                          "Tools" 'minify:-argsh::minify'      "Minify Bash files" 'lint:-argsh::lint'          "Lint Bash files" 'test:-argsh::test'          "Run tests" 'coverage:-argsh::coverage'  "Generate coverage report for your Bash scripts" 'docs:-argsh::docs'          "Generate documentation" '-'                          "Libraries" 'add:-argsh::lib::add'       "Add a library" 'remove:-argsh::lib::remove' "Remove a library" 'list:-argsh::lib::list'     "List installed libraries" 'install:-argsh::lib::install' "Install libraries from lockfile" 'update:-argsh::lib::update' "Update installed libraries" 'publish:-argsh::lib::publish' "Publish a library to a registry" 'search:-argsh::lib::search' "Search available libraries" '-'                          "Runtime" 'builtin:-argsh::builtin'    "Manage native builtins (.so)" 'status:-argsh::_status_cmd' "Show argsh runtime status" );:usage "Enhance your Bash scripting by promoting structure and maintainability,"$'\n'"making it easier to write, understand,"$'\n'"and maintain even complex scripts." "${@}";"${a47[@]}";};argsh::_status_cmd() { declare -gi ARGSH_BUILTIN=0;if (( ${_argsh_no_builtin:-0} == 0 )) && declare -p __ARGSH_BUILTINS &>/dev/null;then argsh::builtin::try && ARGSH_BUILTIN=1;fi;argsh::status "${@}";};argsh::builtin::_install() { local _force=0 _dest_dir="";while [[ "${1:-}" == --* ]];do case "${1}" in
--force)_force=1; shift;;
--path)shift;if [[ -z "${1:-}" || "${1}" == --* ]];then echo "argsh: --path requires a directory argument" >&2;return 1;fi;_dest_dir="${1}"; shift;;
*)echo "argsh: unknown option: ${1}" >&2; return 1;;
esac;done;if [[ -n "${_dest_dir}" ]];then [[ -d "${_dest_dir}" && -w "${_dest_dir}" ]] || { echo "argsh: directory not writable: ${_dest_dir}" >&2;return 1;};PATH_BIN="${_dest_dir}" argsh::builtin::download "${_force}";else argsh::builtin::download "${_force}";fi;};argsh::shebang() { local -a _argsh_imports=();local _argsh_no_builtin=0;while [[ "${1:-}" == -* ]];do case "${1}" in
--help|-h)(argsh::main --help) || true;return 0;;
--import|-i)shift;[[ -n "${1:-}" ]] || { echo "argsh: --import requires an argument" >&2; return 1; };_argsh_imports+=("${1}");shift;;
--no-builtin)_argsh_no_builtin=1;shift;;
--version)echo "argsh ${ARGSH_VERSION:-unknown} (${ARGSH_COMMIT_SHA:-unknown})";return 0;;
--)shift; break;;
*)break;;
esac;done;if [[ -z "${1:-}" ]];then (argsh::main --help) || true;return 0;fi;local -r a58="${1}";if [[ "${BASH_SOURCE[-1]}" != "${a58}" && ! -f "${a58}" ]];then case "${a58}" in
*/*|*.sh|*.bash)echo "argsh: file not found: ${a58}" >&2;return 1;;
esac;fi;if [[ "${BASH_SOURCE[-1]}" == "${a58}" || ! -f "${a58}" ]];then : "${ARGSH_SOURCE:=argsh}";export ARGSH_SOURCE;if [[ "${a58}" == "builtins" ]];then shift;argsh::builtin "${@}";return;fi;argsh::main "${@}";return;fi;: "${ARGSH_SOURCE="${a58}"}";export ARGSH_SOURCE;bash::version 4 3 0 || { echo "This script requires bash 4.3.0 or later";return 1;} >&2;declare -gi ARGSH_BUILTIN=0;if (( ! _argsh_no_builtin ));then [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: searching for argsh.so..." >&2;if argsh::builtin::try;then ARGSH_BUILTIN=1;[[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: loaded builtins from $(argsh::builtin::location 2>/dev/null || echo 'unknown')" >&2;else [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: builtins not found locally" >&2;if [[ "${ARGSH_NO_AUTO_DOWNLOAD:-}" != "1" ]];then [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: attempting auto-download of builtins" >&2;argsh::builtin::download 0 && argsh::builtin::try && ARGSH_BUILTIN=1;else [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: auto-download disabled (ARGSH_NO_AUTO_DOWNLOAD=1)" >&2;fi;fi;fi;local _lib;for _lib in "${_argsh_imports[@]}";do import "${_lib}";done;__argsh_trace_init "${@:2}";shift;. "${a58}";};string::drop-index() { local a26="${1}";local a37="${2}";local a22="${3:-1}";echo "${a26:0:a37}${a26:a37+a22}";};string::random() { local a22="${1:-42}";local a32="${2:-"a-zA-Z0-9"}";local a80;until [[ "${a80:-}" =~ ^[[:alpha:]] ]];do a80=$(tr -dc "${a32}" < /dev/urandom | fold -w "${a22}" | head -n 1 || :);done;echo "${a80}";};string::indent() { local a26="${1:-'-'}";local a21="${2:-0}";local a62 a39;[[ ${a26} != '-' ]] || a26="$(cat)";mapfile -t a39 < <(echo "${a26}");for a62 in "${a39[@]}";do a62="$(string::trim-left "${a62}")";(( a21 == 0 )) || printf "%${a21}s" " ";echo "${a62}";done;};string::trim-left() { local a26="${1}";local a32="${2:-" "$'\n'$'\t'}";[[ -n ${a26:-} ]] || return 0;[[ ${a26} != '-' ]] || a26="$(cat)";while [[ -n "${a26}" ]];do [[ ${a32} == *"${a26:0:1}"* ]] || break;a26="${a26:1}";done;echo "${a26}";};string::trim-right() { local a26="${1:-'-'}";local a32="${2:-" "$'\n'$'\t'}";[[ ${a26} != '-' ]] || a26="$(cat)";while [[ -n "${a26}" ]];do [[ ${a32} == *"${a26: -1}"* ]] || break;a26="${a26:0: -1}";done;echo "${a26}";};string::trim() { local a26="${1:-'-'}";local a32="${2:-" "$'\n'$'\t'}";[[ ${a26} != '-' ]] || a26="$(cat)";echo "${a26}" | string::trim-left - "${a32}" | string::trim-right - "${a32}";};to::string() { local a48="${1}";echo "${a48}";};to::boolean() { local a48="${1}";case "${a48}" in
""|"false"|"0")a48="0";;
*)a48="1";;
esac;echo "${a48}";};to::int() { local a48="${1}";[[ ${a48} =~ ^-?[0-9]+$ ]] || return 1;echo "${a48}";};to::float() { local a48="${1}";[[ ${a48} =~ ^-?[0-9]+(\.[0-9]+)?$ ]] || return 1;echo "${a48}";};to::stdin() { local a48="${1}";[[ ${a48} != "-" ]] || a48="$(cat)";echo "${a48}";};to::file() { local a48="${1}";[[ -f "${a48}" ]] || return 1;echo "${a48}";};if [[ -z "${ARGSH_TRACE:-}" ]];then __argsh_trace_init()  { :; };__argsh_trace_args()  { :; };__argsh_trace_usage() { :; };else declare -g  __ARGSH_TRACE_FILE="${ARGSH_TRACE}";declare -gi __ARGSH_TRACE_START=0;declare -gi __ARGSH_TRACE_STEP=0;declare -gi __ARGSH_TRACE_DEPTH=0;declare -ga __ARGSH_TRACE_STACK=();__argsh_trace_ms() { if [[ -n "${EPOCHREALTIME:-}" ]];then local a90="${EPOCHREALTIME%.*}";local a87="${EPOCHREALTIME#*.}";echo $(( a90 * 1000 + 10#${a87:0:3} ));else date +%s%3N 2>/dev/null || echo $(( $(date +%s) * 1000 ));fi;};__argsh_trace_write() { printf '%s\n' "${*}" >> "${__ARGSH_TRACE_FILE}";};__argsh_trace_init() { __ARGSH_TRACE_START="$(__argsh_trace_ms)";__ARGSH_TRACE_STEP=0;__ARGSH_TRACE_DEPTH=0;__ARGSH_TRACE_STACK=();{ echo "# Process Trace";echo "";echo "- **Script**: ${ARGSH_SOURCE:-unknown}";echo "- **Date**: $(date -Iseconds 2>/dev/null || date)";echo "- **Args**: \`${*}\`";echo "- **PID**: $$";echo "";echo "## Execution";echo "";} > "${__ARGSH_TRACE_FILE}";set -o functrace;local _prev_debug _prev_exit;_prev_debug="$(trap -p DEBUG | sed "s/^trap -- '//;s/' DEBUG$//" 2>/dev/null)" || _prev_debug="";_prev_exit="$(trap -p EXIT | sed "s/^trap -- '//;s/' EXIT$//" 2>/dev/null)" || _prev_exit="";if [[ -n "${_prev_debug}" ]];then trap "${_prev_debug}; __argsh_trace_debug_hook" DEBUG;else trap '__argsh_trace_debug_hook' DEBUG;fi;if [[ -n "${_prev_exit}" ]];then trap '__argsh_trace_saved_exit=$?; __argsh_trace_exit_hook $__argsh_trace_saved_exit; (exit $__argsh_trace_saved_exit); '"${_prev_exit}" EXIT;else trap '__argsh_trace_exit_hook $?' EXIT;fi;};__argsh_trace_debug_hook() { local _depth=$(( ${#FUNCNAME[@]} - 2 ));(( _depth >= 0 )) || _depth=0;case "${FUNCNAME[1]:-}" in
__argsh_trace_*)return 0;;
esac;local _func="${FUNCNAME[1]:-main}";local _now;if (( _depth > __ARGSH_TRACE_DEPTH ));then _now="$(__argsh_trace_ms)";__ARGSH_TRACE_STACK+=("${_func}:${_now}");__ARGSH_TRACE_DEPTH=${_depth};(( ++__ARGSH_TRACE_STEP ));local _indent="";local _i;for (( _i=0; _i < _depth; _i++ )); do _indent+="  "; done;__argsh_trace_write "${_indent}- **[${__ARGSH_TRACE_STEP}]** \`${_func}\` enter (+$(( _now - __ARGSH_TRACE_START ))ms)";elif (( _depth < __ARGSH_TRACE_DEPTH ));then _now="$(__argsh_trace_ms)";while (( ${#__ARGSH_TRACE_STACK[@]} > _depth ));do local _top="${__ARGSH_TRACE_STACK[-1]}";unset '__ARGSH_TRACE_STACK[-1]';local _top_func="${_top%%:*}";local _top_start="${_top##*:}";local _elapsed=$(( _now - _top_start ));local _indent="";local _d=$(( ${#__ARGSH_TRACE_STACK[@]} + 1 ));local _i;for (( _i=0; _i < _d; _i++ )); do _indent+="  "; done;__argsh_trace_write "${_indent}- \`${_top_func}\` exit (${_elapsed}ms)";done;__ARGSH_TRACE_DEPTH=${_depth};fi;};__argsh_trace_args() { local _title="${1:-}";local _now;_now="$(__argsh_trace_ms)";local _indent="";local _i;for (( _i=0; _i <= __ARGSH_TRACE_DEPTH; _i++ )); do _indent+="  "; done;__argsh_trace_write "";__argsh_trace_write "${_indent}<details><summary>Variables after <code>:args \"${_title}\"</code> (+$(( _now - __ARGSH_TRACE_START ))ms)</summary>";__argsh_trace_write "";__argsh_trace_write "${_indent}\`\`\`";declare -p args &>/dev/null 2>&1 || { __argsh_trace_write "${_indent}\`\`\`"; __argsh_trace_write "${_indent}</details>"; __argsh_trace_write ""; return 0; };local _fi _fname _fval;for (( _fi=0; _fi < ${#args[@]}; _fi+=2 ));do [[ "${args[_fi]}" != "-" ]] || continue;_fname="${args[_fi]}";_fname="${_fname/[|:]*}";_fname="${_fname#\#}";_fname="${_fname//-/_}";[[ -n "${_fname}" ]] || continue;_fval="$(declare -p "${_fname}" 2>/dev/null)" || _fval="${_fname}=(unset)";_fval="${_fval#declare -* }";__argsh_trace_write "${_indent}${_fval}";done;__argsh_trace_write "${_indent}\`\`\`";__argsh_trace_write "${_indent}</details>";__argsh_trace_write "";};__argsh_trace_usage() { local _func="${1:-}" _cmd="${2:-}";local _now;_now="$(__argsh_trace_ms)";local _indent="";local _i;for (( _i=0; _i <= __ARGSH_TRACE_DEPTH; _i++ )); do _indent+="  "; done;__argsh_trace_write "${_indent}- dispatch: \`${_cmd}\` -> \`${_func}\` (+$(( _now - __ARGSH_TRACE_START ))ms)";};__argsh_trace_exit_hook() { local _exit_code="${1:-$?}";local _now;_now="$(__argsh_trace_ms)";local _elapsed=$(( _now - __ARGSH_TRACE_START ));while (( ${#__ARGSH_TRACE_STACK[@]} > 0 ));do local _top="${__ARGSH_TRACE_STACK[-1]}";unset '__ARGSH_TRACE_STACK[-1]';local _top_func="${_top%%:*}";local _top_start="${_top##*:}";local _d=${#__ARGSH_TRACE_STACK[@]};local _indent="" _i;for (( _i=0; _i <= _d; _i++ )); do _indent+="  "; done;__argsh_trace_write "${_indent}- \`${_top_func}\` exit ($(( _now - _top_start ))ms)";done;{ echo "";echo "## Summary";echo "";echo "- **Steps**: ${__ARGSH_TRACE_STEP}";echo "- **Duration**: ${_elapsed}ms";echo "- **Exit code**: ${_exit_code}";} >> "${__ARGSH_TRACE_FILE}";};fi;: "${LOUP_REPO:=github.com/kernpilot/lok8s}";: "${LOUP_URL:=https://get.lok8s.io}";: "${LOUP_B_INSTALL_URL:=https://get.binary.help}";_say()  { printf '%b\n' "${*}" >&2; };_info() { _say "  \033[36m•\033[0m ${*}"; };_ok()   { _say "  \033[32m✓\033[0m ${*}"; };_warn() { _say "  \033[33m!\033[0m ${*}"; };_die()  { _say "  \033[31m✗\033[0m ${*}"; exit 1; };loup::interactive() { (( non_interactive )) && return 1; [[ -e /dev/tty ]]; };loup::confirm() { loup::interactive || return 0;local a70;read -r -p "  ${1} [Y/n] " a70 </dev/tty || return 0;[[ -z "${a70}" || "${a70}" =~ ^[Yy] ]];};loup::detect_mode() { local a86="${1}";[[ -d "${a86}/.lok8s" ]] && { echo update; return; };[[ -f "${a86}/.bin/b.yaml" ]] && grep -q "${LOUP_REPO#*/}" "${a86}/.bin/b.yaml" 2>/dev/null && { echo update; return; };echo bootstrap;};loup::ensure_b() { command -v b &>/dev/null && return 0;[[ -x "${PATH_BIN}/b" ]] && { export PATH="${PATH_BIN}:${PATH}"; return 0; };command -v curl &>/dev/null || _die "curl is required to install b";_info "installing b (binary manager) → ${PATH_BIN}";mkdir -p "${PATH_BIN}";B_INSTALL_DIR="${PATH_BIN}" sh -c "$(curl -fsSL "${LOUP_B_INSTALL_URL}")" || _die "failed to install b from ${LOUP_B_INSTALL_URL} (install it manually: https://binary.help)";export PATH="${PATH_BIN}:${PATH}";command -v b &>/dev/null || _die "b is still not on PATH after install";_ok "b installed";};loup::verify_registered() { [[ -f "${PWD}/.bin/b.yaml" ]] || _die "no ${PWD}/.bin/b.yaml — the env was never registered (run: b env add ${LOUP_REPO}#${profile} --version ${git_ref})";grep -q "${LOUP_REPO#*/}" "${PWD}/.bin/b.yaml" 2>/dev/null || _die ".bin/b.yaml registered no lok8s env — nothing here can update (run: b env add ${LOUP_REPO}#${profile} --version ${git_ref})";};loup::verify_materialised() { local -a a15=();[[ -d "${PWD}/.lok8s" ]] || a15+=(".lok8s/ (vendored framework)");[[ -f "${PWD}/.lok8s/lo" ]] || a15+=(".lok8s/lo (CLI entrypoint)");local a11="${PATH_BIN%/}";a11="${a11##*/}";local a71;for a71 in argsh kubectl jq yq envsubst;do [[ -x "${PATH_BIN}/${a71}" ]] || command -v "${a71}" &>/dev/null || a15+=("${a11}/${a71}");done;(( ${#a15[@]} == 0 )) || _die "b install reported success but the env is incomplete — missing: ${a15[*]}"$'\n'"Re-run with 'b install --force', or check 'b env status' and 'b install --dry-run'.";};loup::install_self() { local a56="${PATH_BIN}/lo-up";mkdir -p "${PATH_BIN}";if command -v curl &>/dev/null && curl -fsSL "${LOUP_URL}" -o "${a56}" 2>/dev/null;then chmod +x "${a56}" 2>/dev/null || true;_ok "lo-up → ${a56#"${PWD}/"}";else _warn "couldn't persist lo-up into ${a56} (continuing)";fi;};main() { local non_interactive=0 profile git_ref dir;local -a args=( 'non-interactive|y:+'  'Run without prompts (CI / unattended)' 'profile|p'            'b env profile: core|kustomize|local|capi|kubeone (default: local)' 'git-ref|r'            'lok8s git ref/tag to pin (default: main)' 'dir|d'                'Target directory (default: current directory)' );:args "lo-up — install or update a lok8s project environment" "${@}";profile="${profile:-local}"; git_ref="${git_ref:-main}"; dir="${dir:-${PWD}}";[[ -d "${dir}" ]] || _die "directory not found: ${dir}";cd "${dir}" || _die "cannot enter ${dir}";export PATH_BASE="${PWD}";export PATH_BIN="${PATH_BIN:-${PWD}/.bin}";local mode; a63="$(loup::detect_mode "${PWD}")";_say "";_say "  \033[1;36mlo-up\033[0m  \033[2m${a63} · ${PWD}\033[0m";loup::ensure_b;if [[ "${a63}" == "bootstrap" ]];then loup::confirm "Bootstrap a lok8s '${profile}' env here (${LOUP_REPO}#${profile}@${git_ref})?" || _die "aborted";_info "adding profile '${profile}' from ${LOUP_REPO}#${git_ref}";b env add "${LOUP_REPO}#${profile}" --version "${git_ref}" || _die "b env add failed";else _info "updating the lok8s env (${LOUP_REPO})";fi;loup::verify_registered;loup::install_self;_info "installing binaries + syncing the framework (b install)…";b install || _die "b install failed";loup::verify_materialised;_ok "lok8s env ready";_say "";_say "  Next:";_say "    \033[2m${PATH_BIN##*/}/lo-up\033[0m   \033[2m# re-run anytime to update\033[0m";_say "    \033[2mlo use <domain> && lo up\033[0m";_say "";};[[ "${0}" != "${BASH_SOURCE[0]}" && "${ARGSH_SOURCE:-}" != "${BASH_SOURCE[0]}" ]] || main "${@}";
