# shellcheck    shell=dash

xrc shim

# ___x_cmd_pkg_sphere_shim_gen         \
#     --mode <[app,adaptive]>          \
#     --code <[sh,bat]>                \
#     --var <path_name1=value>         \
#     --var <path_name2=value>         \
#     --bin_dir <bin_dir>              \
#     --bin_file <bin_file>            || return $?
___x_cmd_pkg_sphere_shim_gen(){
    local source="$___X_CMD_PKG___META_TGT"
    local default_target="$___X_CMD_PKG___META_TGT/shim_bin"
    local adaptive_target="$___X_CMD_PKG___META_TGT/adaptive_shim_bin"

    local ___X_CMD_PKG_SHIM_BIN_OPTS=''
    local mode=; local code=; local target=; local x_=
    while [ $# -gt 0 ]; do
        case "$1" in
            --mode)     mode=$2;                shift 2 ;;
            --code)     code=$2;                shift 2 ;;
            --var)
                        ___x_cmd cmdstr ___X_CMD_PKG_SHIM_BIN_OPTS "${2}"
                                                shift 2 ;;
            --bin_dir)
                        source="$___X_CMD_PKG___META_TGT/${2}"
                        ! [ "${source%/*}" = "${source%?}" ] || source="$___X_CMD_PKG___META_TGT"
                                                shift 2 ;;
            --bin_file) shift 1;                break ;;
            *)                                  return 1 ;;
        esac
    done

    ___x_cmd mkdirp  "$default_target"
    ! [ "$mode" = "adaptive" ] || ___x_cmd mkdirp  "$adaptive_target"
    pkg:info                    \
        --source "$source/"           \
        --shim_bin "$default_target/" \
        --adaptive_shim_bin "$adaptive_target/"  "Generating shim bin file"
    local i; for i in "$@"; do

        [ -f "$source/$i" ] || {
            pkg:error "Not found: [$source/$i]"
            return 1
        }

        if [ "$i" != "${i%.exe*}" ] && [ "$code" = "bat" ]; then
            target="${i%.exe*}.bat"
        elif [ "$i" != "${i%.exe*}" ] && [ "$code" = "sh" ]; then
            target="${i%".exe"*}"
        else
            target="$i"
        fi
        target="${target##*/}"

        case "$code" in
            bat) eval set -- ___x_cmd_shim__gen_batcode_local "$___X_CMD_PKG_SHIM_BIN_OPTS"  -- "$source/$i" ;;

            sh)  eval set -- ___x_cmd_shim__gen_shcode_local  "$___X_CMD_PKG_SHIM_BIN_OPTS"  -- "$source/$i" ;;
        esac
        local IFS=" "; pkg:debug "cmd -> $*"; "$@" > "$default_target/$target" || return $?
        ___x_cmd_cmds chmod +x  "$source/$i" "$default_target/$target"

        if [ "$mode" = "adaptive" ]; then
            case "$code" in
                bat) eval set -- ___x_cmd_shim__gen_batcode_varset "$___X_CMD_PKG_SHIM_BIN_OPTS"  -- "$source/$i" ;;

                sh)  eval set -- ___x_cmd_shim__gen_shcode_varset  "$___X_CMD_PKG_SHIM_BIN_OPTS"  -- "$source/$i" ;;
            esac
            IFS=" "; pkg:debug "cmd -> $*"; "$@" > "$adaptive_target/$target" || return $?
            ___x_cmd_cmds chmod +x "$adaptive_target/$target"
        fi
    done
}
