# shellcheck shell=dash

___x_cmd_ws___searchpath_(){
    x_=
    ___X_CMD_LOCATEFOLDER_MODE=-d ___x_cmd locatefolder_ . "$1"
}

___x_cmd_ws_execfile(){
    local x_
    ___x_cmd_ws___searchpath_ . ".x-cmd" || N=ws M=".x-cmd directory not found -> $PWD" log:ret:1

    if [ $# -ge 1 ] && [ -f  "$x_/.x-cmd/$1" ]; then
        ___x_cmd_ws___exe_workspace "$x_/.x-cmd/$1" "$@"
        return
    fi

    if [ -f  "$x_/.x-cmd/X" ]; then
        ___x_cmd_ws___exe_workspace "$x_/.x-cmd/X"  "$@"
        return
    fi

    N=ws M="[Resource Name=$1] or X file Not found in the workspace -> $x_/.x-cmd/$1 or $x_/.x-cmd/X" log:ret:1
}

___x_cmd_ws___exe_workspace(){
    local filepath="${1:?filepath}"
    if [ -L "$filepath" ]; then
        local x_
        ___x_cmd readlink_recursive_ "$filepath"
        ___x_cmd_abspath_ "$x_"
        filepath="$x_"
    fi

    shift

    local x_;   ___x_cmd_abspath_ "$filepath"
    ___X_CMD_WSROOT="${x_%/.x-cmd/*}" ___x_cmd runfile "$filepath" "$@"

    local code=$?
    if [ $code -eq 126 ]; then
        ws:error    "Cannot find out how to execute the file -> '$filepath'"
        ws:warn     "Please specify the execution engine. For more information, consult 'x help'"
    fi
    return $code
}
