#!/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=("$(a88="${ARGSH_SOURCE:-"${0}"}"; echo "${a88##*/}")");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 a44="${1}"; shift;local -a a45=("${@}");:usage::text "${a44}";exit 0;};fi;if ! (( ARGSH_BUILTIN ));then :usage::suggest() { local a36="${1}"; shift;local -a a12=("${@}");local a48="" a5=999;local a86 a4 a82 a55;local -a _sug_aliases=();for (( a86=0; a86 < ${#a12[@]}; a86+=2 ));do a4="${a12[a86]/:*}";a4="${a4/@*/}";a4="${a4#\#}";IFS='|' read -ra _sug_aliases <<< "${a4}";for a82 in "${_sug_aliases[@]}";do a55="$(:usage::levenshtein "${a36}" "${a82}")";if (( a55 < a5 ));then a5="${a55}";a48="${a82}";fi;done;done;if (( a5 > 0 && a5 <= 2 ));then local a13=${#a36};(( ${#a48} > a13 )) && a13=${#a48};if (( a5 * 100 <= a13 * 40 ));then echo "${a48}";fi;fi;};:usage::levenshtein() { local a82="${1}" a83="${2}";local a25=${#a82} a29=${#a83};if (( a25 == 0 )); then echo "${a29}"; return; fi;if (( a29 == 0 )); then echo "${a25}"; return; fi;local -a a63;local a87;for (( a87=0; a87 <= a29; a87++ )); do a63[a87]="${a87}"; done;local a86 a17 a51 a14;for (( a86=1; a86 <= a25; a86++ ));do a17="${a63[0]}";a63[0]="${a86}";for (( a87=1; a87 <= a29; a87++ ));do if [[ "${a82:a86-1:1}" == "${a83:a87-1:1}" ]];then a51=0;else a51=1;fi;a14=$(( a17 + a51 ));(( a63[a87] + 1 < a14 )) && a14=$(( a63[a87] + 1 ));(( a63[a87-1] + 1 < a14 )) && a14=$(( a63[a87-1] + 1 ));a17="${a63[a87]}";a63[a87]="${a14}";done;done;echo "${a63[a29]}";};:usage() { local a44="${1}"; shift;declare -p a45 &>/dev/null || local -a a45=();declare -p args &>/dev/null || local -a args=();:args::_dedup_inherited;[[ $(( ${#a45[@]} % 2 )) -eq 0 ]] || :args::_error "usage must be an associative array";if [[ -z ${1:-} || ${1} == "-h" || ${1} == "--help" ]];then a45=(":usage::help" "${a44}" "${a45[@]}");return 0;fi;if ! (( ${#COMMANDNAME[@]} )) && [[ ${1:-} == "--argsh" ]];then echo "https://arg.sh ${ARGSH_COMMIT_SHA:-} ${ARGSH_VERSION:-}";exit 0;fi;local -A a39=();local -a a69=("${@}");local a70 a31="";while (( ${#a69[@]} ));do if [[ ${a69[0]:0:1} != "-" ]];then [[ -z "${a70:-}" ]] || break;a70="${a69[0]}";a69=("${a69[@]:1}");continue;fi;:args::parse_flag || break;a39["${a31}"]=1;done;:args::check_required_flags;local a58;for (( a86=0; a86 < ${#a45[@]}; a86+=2 ));do local _entry_cmd="${a45[a86]/:*}";_entry_cmd="${_entry_cmd/@*/}";IFS='|' read -ra _aliases <<< "${_entry_cmd}";for a26 in "${_aliases[@]}";do a26="${a26#\#}";[[ "${a70}" == "${a26}" ]] || continue;a31="${a45[a86]#\#}";a58="${a45[a86]/*:-}";a58="${a58#\#}";[[ "${a58}" == "${a45[a86]}" ]] || break 2;a58="${a58/|*}";a58="${a58/@*/}";break 2;done;done;if [[ -z "${a58:-}" ]];then if [[ -z "${a70:-}" ]];then a45=(":usage::help" "${a44}" "${a45[@]}");return 0;fi;local _sug;_sug="$(:usage::suggest "${a70}" "${a45[@]}")";if [[ -n "${_sug}" ]];then :args::error_usage "Invalid command: ${a70}. Did you mean '${_sug}'?";else :args::error_usage "Invalid command: ${a70}";fi;fi;local a9=0;[[ "${a31}" != *":-"* ]] || a9=1;if (( a9 ));then declare -F -- "${a58}" >/dev/null 2>&1 || { local _sug;_sug="$(:usage::suggest "${a70}" "${a45[@]}")";if [[ -n "${_sug}" ]];then :args::error_usage "Invalid command: ${a70}. Did you mean '${_sug}'?";else :args::error_usage "Invalid command: ${a70}";fi;};else local a16="${FUNCNAME[1]:-}";if [[ -n "${a16}" ]] && declare -F -- "${a16}::${a58}" >/dev/null 2>&1;then a58="${a16}::${a58}";elif [[ "${a16}" == *"::"* ]] && declare -F -- "${a16##*::}::${a58}" >/dev/null 2>&1;then a58="${a16##*::}::${a58}";elif declare -F -- "argsh::${a58}" >/dev/null 2>&1;then a58="argsh::${a58}";elif declare -F -- "${a58}" >/dev/null 2>&1;then :;else local _sug;_sug="$(:usage::suggest "${a70}" "${a45[@]}")";if [[ -n "${_sug}" ]];then :args::error_usage "Invalid command: ${a70}. Did you mean '${_sug}'?";else :args::error_usage "Invalid command: ${a70}";fi;fi;fi;local _cmd_name="${a31/[|:]*}";COMMANDNAME+=("${_cmd_name/@*/}");declare -F __argsh_trace_usage &>/dev/null && __argsh_trace_usage "${a58}" "${a70}";a45=("${a58}" "${a69[@]}");};:usage::text() { local a44="${1:-}";string::indent "${a44}";echo;echo "Usage: ${COMMANDNAME[*]} <command> [args]";[[ ${a45[0]:-} == '-' ]] || echo -e "\nAvailable Commands:";for (( a86=0; a86 < ${#a45[@]}; a86+=2 ));do [[ "${a45[a86]:0:1}" != "#" ]] || continue;[[ "${a45[a86]}" != "-" ]] || { echo;echo "${a45[a86+1]}";continue;};local _disp_name="${a45[a86]/[:|]*}";printf "  %-${ARGSH_FIELD_WIDTH}s %s\n" "${_disp_name/@*/}" "${a45[a86+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 a44="${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 a32=0 a31="" a86 a0=1;local -A a39=();local -a a69=("${@}");while (( ${#a69[@]} ));do if [[ ${a69[0]:0:1} != "-" ]];then local _pos_name _pos_value;a86="$(:args::field_positional "${a0}")" || :args::error_usage "too many arguments: ${a69[0]}";a31="${args[a86]}";_pos_name="$(args::field_name "${a31}")";_pos_value="$(:args::field_value "${a69[0]}")" || exit "${?}";local -n a74="${_pos_name}";if is::array "${_pos_name}";then (( a32 )) || { a74=();a32=1;};a74+=("${_pos_value}");else a74="${_pos_value}";fi;a69=("${a69[@]:1}");(( ++a0 ));continue;fi;if [[ "${a69[0]}" == "-h" || "${a69[0]}" == "--help" ]];then :args::text;exit 0;fi;:args::parse_flag || :args::error_usage "unknown flag: ${a69[0]}";a39["${a31}"]=1;done;if a86="$(:args::field_positional "${a0}")";then a31="$(args::field_name "${args[a86]}")";if is::uninitialized "${a31}" && ! is::array "${a31}";then :args::error_usage "missing required argument: ${a31}";fi;fi;:args::check_required_flags;[[ ${#a69[@]} -eq 0 ]] || :args::error_usage "too many arguments: ${a69[*]}";declare -F __argsh_trace_args &>/dev/null && __argsh_trace_args "${a44}";};:args::text() { declare -p args &>/dev/null || return 0;local -a a2=() a22=();:args::positional;string::indent "${a44}";echo;echo "Usage:";echo "  ${COMMANDNAME[*]} ${a22[*]}";(( ${#a2[@]} == 0 )) || { echo;echo "Arguments:";for a86 in "${a2[@]}";do [[ ${args[a86]} != "-" ]] || continue;a53="$( printf "   %-${ARGSH_FIELD_WIDTH}s%s" " " "${args[a86+1]}" | fmt::tty;)";printf "   %-${ARGSH_FIELD_WIDTH}s%s\n" "$(:args::fieldf "${args[a86]}")" "$(string::trim-left "${a53}")";done;};:args::text_flags;echo;};:args::text_flags() { local -a args=("${args[@]}");local -a a33=();array::contains 'help|h:+' "${args[@]}" || args+=('help|h:+' "Show this help message");:args::flags;(( ${#a33[@]} )) || return 0;[[ "${args[${a33[0]}]}" == "-" ]] || echo -e "\nOptions:";for a86 in "${a33[@]}";do [[ "${args[a86]:0:1}" != "#" && "${args[a86]}" != *":#"* ]] || continue;[[ "${args[a86]}" != "-" ]] || { echo;echo "${args[a86+1]}";continue;};:args::fieldf "${args[a86]}";{ echo -n "           ";echo -e "${args[a86+1]}\n";} | fmt::tty;done;};:args::flags() { declare -p args &>/dev/null || local -a args;declare -p a33 &>/dev/null || local -a a33;for (( a86=0; a86 < "${#args[@]}"; a86+=2 ));do if [[ ${args[a86]} == *"|"* || ${args[a86]} == '-' ]];then a33+=("${a86}");fi;done;};:args::positional() { declare -p args &>/dev/null || local -a args;declare -p a2 &>/dev/null || local -a a2;declare -p a22 &>/dev/null || local -a a22;local a74;for (( a86=0; a86 < "${#args[@]}"; a86+=2 ));do [[ ${args[a86]} != *"|"* && ${args[a86]} != '-' ]] || continue;a74="$(args::field_name "${args[a86]}")";a2+=("${a86}");if is::array "${a74}";then a22+=("...${a74}");continue;fi;if ! is::uninitialized "${a74}";then a22+=("[${a74}]");continue;fi;a22+=("<${a74}>");done;};:args::parse_flag() { declare -p a69 a31 &>/dev/null || return 1;local a57="${a69[0]/=*}";if [[ ${a57:0:2} == "--" ]];then a31="$(:args::field_lookup "${a57:2}")" || return "${?}";elif [[ ${a57:0:1} == "-" ]];then a57="${a57:0:2}";a31="$(:args::field_lookup "${a57:1}")" || return "${?}";fi;:args::field_set_flag "${a31}";};:args::check_required_flags() { declare -p a39 args &>/dev/null || return 1;local a31;local -a a28;for (( a86=0; a86 < ${#args[@]}; a86+=2 ));do a31="${args[a86]}";:args::field_attrs "${a31}";if (( a28[2] )) && ! (( a28[4] ));then local -n a74="${a28[0]}";a74=0;fi;if (( a28[6] )) && [[ ${args[a86]} == *"|"* ]] && [[ -z ${a39[${args[a86]}]:-} ]];then :args::error_usage "missing required flag: ${args[a86]/|*}";fi;done;};:args::field_set_flag() { local a31="${1}";declare -p a69 a57 &>/dev/null || return 1;local -a a28;:args::field_attrs "${a31}";local -n a74="${a28[0]}";local a8 a6;if (( a28[2] ));then a8=1;if [[ ${a57:0:2} == "--" ]];then a69=("${a69[@]:1}");else a69[0]="-${a69[0]:2}";[[ ${a69[0]} != "-" ]] || a69=("${a69[@]:1}");fi;fi;[[ -n ${a8:-} ]] || { a6="${a69[0]/${a57}}";if [[ ${a6} == "" ]];then (( ${#a69[@]} > 1 )) || :args::error "missing value for flag: ${a28[0]}";a8="${a69[1]}";a69=("${a69[@]:1}");else [[ "${a6:0:1}" != "=" ]] || a6="${a6:1}";a8="${a6}";fi;a8="$(:args::field_value "${a8}")" || exit "${?}";a69=("${a69[@]:1}");};if (( a28[5] ));then a74+=("${a8}");else a74="${a8}";fi;};:args::field_value() { local a46="${1}";declare -p a31 &>/dev/null || return 1;declare -p a28 &>/dev/null || { local -a a28;:args::field_attrs "${a31}";};declare -f "to::${a28[3]}" &>/dev/null || :args::_error "unknown type: ${a28[3]}";"to::${a28[3]}" "${a46}" "${a28[0]}" || :args::error_usage "invalid type (${a28[3]}): ${a46}";};:args::field_lookup() { local a31="${1}";declare -p args &>/dev/null || return 1;for (( a86=0; a86 < ${#args[@]}; a86+=2 ));do if [[ ${args[a86]} =~ (^${a31}\||\|${a31}:|\|${a31}$) ]];then echo "${args[a86]}";return 0;fi;done;return 1;};:args::field_positional() { local a10="${1:-1}";declare -p args &>/dev/null || return 1;for (( a86=0; a86 < ${#args[@]}; a86+=2 ));do if [[ ${args[a86]} != *"|"* && ${args[a86]} != '-' ]];then if is::array "$(args::field_name "${args[a86]}")" || (( --a10 == 0 ));then echo "${a86}";return 0;fi;fi;done;return 1;};args::field_name() { local a31="${1}";local a27="${2:-1}";a31="${a31/[|:]*}";a31="${a31#\#}";if (( a27 ));then a31="${a31//-/_}";fi;echo "${a31}";};:args::field_attrs() { local a31="${1}";declare -p a28 &>/dev/null || local -a a28;a28=( "" "" 0 "" 0 0 0 0 "" );local a65="+~!^:#";local a62="${a31#*[:]}";[ "${a62}" != "${a31}" ] || a62="";a28[0]="$(args::field_name "${a31}")";a28[8]="$(args::field_name "${a31}" 0)";[[ ${a28[0]:0:1} != "#" ]] || { a28[7]=1;};local -n a74="${a28[0]}";local -a a33;IFS='|' read -ra a33 <<< "${a31/[:]*}";[[ ${#a33[@]} -eq 1 ]] || { a28[1]="${a33[1]}";};if is::array "${a28[0]}";then a28[5]=1;! is::uninitialized "${a28[0]}" || a74=();! (( ${#a74[@]} )) || a28[4]=1;elif ! is::uninitialized "${a28[0]}";then a28[4]=1;fi;while (( ${#a62} > 0 ));do if [[ ${a62:0:1} == "+" ]];then [[ -z ${a28[3]} ]] || :args::_error "cannot have multiple types: ${a28[3]} and boolean";a28[2]=1;a62="${a62:1}";continue;fi;if [[ ${a62:0:1} == "~" ]];then ! (( a28[2] )) || :args::_error "already flagged as boolean";a62="${a62:1}";a28[3]="${a62/[$a65]*}";a62="${a62:${#a28[3]}}";continue;fi;if [[ ${a62:0:1} == "!" ]];then ! (( a28[6] )) || :args::_error "field already flagged as required";a28[6]=1;a62="${a62:1}";continue;fi;if [[ ${a62:0:1} == "^" ]];then a62="${a62:1}";continue;fi;if [[ ${a62:0:1} == "#" ]];then a28[7]=1;a62="${a62:1}";continue;fi;if [[ ${a62:0:1} == ":" ]];then a62="${a62:1}";continue;fi;echo ":args error: unknown modifier: ${a62:0:1}" >&2;exit 2;done;if [[ -z ${a28[3]} && ${a28[2]} -eq 0 ]];then a28[3]="string";fi;};:args::fieldf() { local a31="${1}";declare -p a28 &>/dev/null || { local -a a28;:args::field_attrs "${a31}";};[[ ${a31} == *"|"* ]] || { echo "${a28[8]} ${a28[3]}";return 0;};local -n a74="${a28[0]}";a18="   ";! (( a28[6] )) || a18=" ! ";if [[ -n ${a28[1]} ]];then a18+="-${a28[1]}, --${a28[8]}";else a18+="    --${a28[8]}";fi;a18+=" ";! (( a28[5] )) || a18+="...";a18+="${a28[3]}";if (( a28[4] )) && ! (( a28[2] ));then a18+=" (default: ${a74[*]})";fi;echo "${a18}";};fi;args::run() { local a67="${1}"; shift;for (( a86=0; a86<${#}; a86++ ));do local a75="${1}"; shift;local a58="${1}"; shift;if ! (( a67 )) || (( a75 ));then "${a58}";fi;done;};array::contains() { local -r a21="${1}"; shift;for a11 in "${@}";do [[ "${a11}" != "${a21}" ]] || return 0;done;return 1;};array::join() { local -r a7="${1}"; shift;local a23;printf -v a23 "${a7}%s" "${@}";echo "${a23:${#a7}}";};array::nth() { local -n out="${1}";local -r a73="${2}";shift 2;for (( a86=1; a86<=${#}; a86++ ));do (( a86 % a73 )) || out+=("${!a86}");done;};bash::version() { local a38="${1:-4}";local a40="${2:-3}";local a41="${3:-0}";if [[ "${BASH_VERSINFO[0]}" -lt "${a38}" ]];then return 1;elif [[ "${BASH_VERSINFO[0]}" -gt "${a38}" ]];then return 0;fi;if [[ "${BASH_VERSINFO[1]}" -lt "${a40}" ]];then return 1;elif [[ "${BASH_VERSINFO[1]}" -gt "${a40}" ]];then return 0;fi;if [[ "${BASH_VERSINFO[2]}" -lt "${a41}" ]];then return 1;fi;return 0;};binary::exists() { local a15="${1}";command -v "${a15}" &> /dev/null || { echo "${a15} is required to run this script" >&2;return 1;};};binary::github() { local path="${1}";local a64="${2}";local a56="${3}";local a78="${4:-}";curl -Lso /dev/stdout "https://github.com/${a64}/releases/download/${a56}" | { if [[ -n "${a78}" ]];then tar -xz -C "$(dirname "${path}")" "${a78}";else tee "${path}" &> /dev/null;fi;chmod +x "${path}";};};binary::arch() { local a43="${1:-0}";local -r a47="$(uname -m)";case "${a47}" in
x86_64|amd64)if (( a43 )); then echo "64-bit"; else echo "amd64"; fi;;
armv7l)echo "arm";;
aarch64)echo "arm64";;
*)echo "${a47}";;
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 a80="${1:-"$(id -u)"}";local a72="${2:-"$(id -g)"}";local a66="${3:-"$(whoami)"}";local a59="${4:-"/workspace"}";local a42="${5:-"/bin/sh"}";local a52;a52="$(pwd)";a52="${a52#"${PATH_BASE:-}"}";if [[ "${a52}" == "$(pwd)" ]];then a52="${a59}";else a52="${a59}${a52}";fi;local _passwd _group;_passwd="$(mktemp /tmp/docker_passwd.XXXXXX)";_group="$(mktemp /tmp/docker_group.XXXXXX)";echo "${a66}:x:${a80}:${a72}::${a59}:${a42}" > "${_passwd}";echo "${a66}:x:${a72}:" > "${_group}";echo "-v ${_passwd}:/etc/passwd -v ${_group}:/etc/group";echo "-u ${a80}:${a72}";echo "-v ${PATH_BASE:-"$(pwd)"}:${a59}";echo "-w ${a52}";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 a49="${1:-${?}}";if (( a49 ));then echo -e "\n\033[38;5;196m■■ Stacktrace(${a49}): \e[1m${BASH_COMMAND}\e[22m";for (( a86 = 1; a86 <= ${#FUNCNAME[@]} - 2; a86++ ));do echo -e "${a86}. ${BASH_SOURCE[a86]}:${BASH_LINENO[a86-1]} ➜ ${FUNCNAME[a86]}()";done;echo -e "\033[0m";return "${a49}";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 a77="${1:-"$(cat)"}";if ! command -v fmt &>/dev/null || [[ ! -t 1 ]];then echo "${a77}";return 0;fi;local a50;a50="$(tput cols)";echo "${a77}" | fmt -w "${a50}";};github::latest() { local a64="${1}";curl -fsSLI -o /dev/null -w "%{url_effective}" "https://github.com/${a64}/releases/latest" | rev | cut -d'/' -f1 | rev;};declare -gA a1=();: "${__ARGSH_LIB_DIR:=${BASH_SOURCE[0]%/*}}";import() { local a76="${1}";(( ${a1["${a76}"]:-} )) || { [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: import ${a76}" >&2;a1["${a76}"]=1;if [[ ${a76: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;a76="${_base}/${a76:1}";elif [[ ${a76:0:1} == "^" ]];then local _scripts="" _mod="${a76:1}";_scripts="$(import::_resolve_scripts)";if [[ -n "${_scripts}" ]];then a76="${_scripts}/${_mod}";else local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}";local _dir;[[ "${_s}" == */* ]] && _dir="${_s%/*}" || _dir=".";a76="$(import::_walk_up "${_dir}" "${_mod}")" || { echo "import: cannot resolve ^${_mod} — set PATH_SCRIPTS or add '# argsh source=<path>'" >&2;exit 1;};fi;elif [[ ${a76:0:1} == "~" ]];then local _s="${ARGSH_SOURCE:-${BASH_SOURCE[-1]}}";a76="${_s%/*}/${a76:1}";else local _s="${ARGSH_SOURCE:-${BASH_SOURCE[0]}}";a76="${_s%/*}/${a76}";if ! import::_file_exists "${a76}";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 a76="${_lib}";fi;fi;fi;if ! import::_file_exists "${a76}";then if [[ "${1}" != */* && "${1}" != *..* ]];then local _global="${XDG_DATA_HOME:-${HOME}/.local/share}/argsh/libs/${1}/${1}";if import::_file_exists "${_global}";then a76="${_global}";fi;fi;fi;fi;import::source "${a76}" || 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 a76="${1}";for a71 in "" ".sh" ".bash";do if [[ -f "${a76}${a71}" ]];then [[ "${ARGSH_DEBUG:-}" == "1" ]] && echo "argsh:debug: import resolved -> ${a76}${a71}" >&2;. "${a76}${a71}";return;fi;done;echo "Library not found ${a76}" >&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 a81="${1}";if is::array "${a81}";then [[ $(declare -p "${a81}") =~ ^declare\ -a\ ${a81}(=\(\))?$ ]];else [[ ! ${!a81+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 a79="";if [[ -t 0 ]];then a79="-it";elif [[ -p /dev/stdin ]];then a79="-i";fi;local -r a34="${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 ${a79} $(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[@]}" "${a34}" "${@}";);};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 a45=( '-'                          "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." "${@}";"${a45[@]}";};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 a56="${1}";if [[ "${BASH_SOURCE[-1]}" != "${a56}" && ! -f "${a56}" ]];then case "${a56}" in
*/*|*.sh|*.bash)echo "argsh: file not found: ${a56}" >&2;return 1;;
esac;fi;if [[ "${BASH_SOURCE[-1]}" == "${a56}" || ! -f "${a56}" ]];then : "${ARGSH_SOURCE:=argsh}";export ARGSH_SOURCE;if [[ "${a56}" == "builtins" ]];then shift;argsh::builtin "${@}";return;fi;argsh::main "${@}";return;fi;: "${ARGSH_SOURCE="${a56}"}";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;. "${a56}";};string::drop-index() { local a24="${1}";local a35="${2}";local a20="${3:-1}";echo "${a24:0:a35}${a24:a35+a20}";};string::random() { local a20="${1:-42}";local a30="${2:-"a-zA-Z0-9"}";local a77;until [[ "${a77:-}" =~ ^[[:alpha:]] ]];do a77=$(tr -dc "${a30}" < /dev/urandom | fold -w "${a20}" | head -n 1 || :);done;echo "${a77}";};string::indent() { local a24="${1:-'-'}";local a19="${2:-0}";local a60 a37;[[ ${a24} != '-' ]] || a24="$(cat)";mapfile -t a37 < <(echo "${a24}");for a60 in "${a37[@]}";do a60="$(string::trim-left "${a60}")";(( a19 == 0 )) || printf "%${a19}s" " ";echo "${a60}";done;};string::trim-left() { local a24="${1}";local a30="${2:-" "$'\n'$'\t'}";[[ -n ${a24:-} ]] || return 0;[[ ${a24} != '-' ]] || a24="$(cat)";while [[ -n "${a24}" ]];do [[ ${a30} == *"${a24:0:1}"* ]] || break;a24="${a24:1}";done;echo "${a24}";};string::trim-right() { local a24="${1:-'-'}";local a30="${2:-" "$'\n'$'\t'}";[[ ${a24} != '-' ]] || a24="$(cat)";while [[ -n "${a24}" ]];do [[ ${a30} == *"${a24: -1}"* ]] || break;a24="${a24:0: -1}";done;echo "${a24}";};string::trim() { local a24="${1:-'-'}";local a30="${2:-" "$'\n'$'\t'}";[[ ${a24} != '-' ]] || a24="$(cat)";echo "${a24}" | string::trim-left - "${a30}" | string::trim-right - "${a30}";};to::string() { local a46="${1}";echo "${a46}";};to::boolean() { local a46="${1}";case "${a46}" in
""|"false"|"0")a46="0";;
*)a46="1";;
esac;echo "${a46}";};to::int() { local a46="${1}";[[ ${a46} =~ ^-?[0-9]+$ ]] || return 1;echo "${a46}";};to::float() { local a46="${1}";[[ ${a46} =~ ^-?[0-9]+(\.[0-9]+)?$ ]] || return 1;echo "${a46}";};to::stdin() { local a46="${1}";[[ ${a46} != "-" ]] || a46="$(cat)";echo "${a46}";};to::file() { local a46="${1}";[[ -f "${a46}" ]] || return 1;echo "${a46}";};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 a88="${EPOCHREALTIME%.*}";local a85="${EPOCHREALTIME#*.}";echo $(( a88 * 1000 + 10#${a85: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 a68;read -r -p "  ${1} [Y/n] " a68 </dev/a79 || return 0;[[ -z "${a68}" || "${a68}" =~ ^[Yy] ]];};loup::detect_mode() { local a84="${1}";[[ -d "${a84}/.lok8s" ]] && { echo update; return; };[[ -f "${a84}/.bin/b.yaml" ]] && grep -q 'kernpilot/lok8s' "${a84}/.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::install_self() { local a54="${PATH_BIN}/lo-up";mkdir -p "${PATH_BIN}";if command -v curl &>/dev/null && curl -fsSL "${LOUP_URL}" -o "${a54}" 2>/dev/null;then chmod +x "${a54}" 2>/dev/null || true;_ok "lo-up → ${a54#"${PWD}/"}";else _warn "couldn't persist lo-up into ${a54} (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; a61="$(loup::detect_mode "${PWD}")";_say "";_say "  \033[1;36mlo-up\033[0m  \033[2m${a61} · ${PWD}\033[0m";loup::ensure_b;if [[ "${a61}" == "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::install_self;_info "installing binaries + syncing the framework (b install)…";b install || _die "b install failed";_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 "${@}";
