
xrc:mod:lib     version     build/deb build/apk build/rpm build/arch

___x_cmd_version_build(){
    [ $# -gt 0 ] ||     set  --  --help

    local version="" versum="" tgzfile="" mode="" name=""
    local builddir="${builddir:-.build}"
    while [ $# -gt 0 ]; do
        case "$1" in
            --version)      version="$2";  arg:2:shift        ;;
            --versum)       versum="$2";   arg:2:shift        ;;
            --tgzfile)      tgzfile="$2";  arg:2:shift        ;;
            --docker)
                            mode="docker" ;  shift  ;;
            which)
                            shift; ___x_cmd_version_build_which "$@" ; return $? ;;
            deb|apk|rpm|arch)
                            name="$1";       shift  ;;
            --help|-h)      ___x_cmd help -m version build; return   ;;
            *)              ___x_cmd help -m version build >&2 ; return 64    ;;
        esac
    done

    [ -n "$name" ] || N=version M="Please specify package type: deb|rpm|apk|arch" log:ret:1

    ___x_cmd_version_build_pre___   "$version" "$tgzfile" "$builddir"   || return 1
    ___x_cmd_version_build_"$name"  "$version" "$versum" "$mode" "$builddir"
}

___x_cmd_version_build_which_(){
    local name="$1";    [ -n "$name" ] || N=version M="Please specify package type" log:ret:1
    local version="$2"; [ -n "$version" ] || N=version M="Please specify version" log:ret:1

    local dir="$___X_CMD_VERSION_BUILD_DATA/release/${version}/${name}"
    [ -d "$dir" ] || N=version M="Not found [version=$version] [type=$name] package" log:ret:1

    local filename; ___x_cmd fsiter --file1_ "$dir"; filename="$x_"
    x_="${dir}/${filename}"
}

___x_cmd_version_build_which(){
    local name="$1";
    local version="$2"
    local x_
    ___x_cmd_version_build_which_ "$name" "$version" || return $?
    printf "%s\n" "$x_"
}


___x_cmd_version_build_info___(){
    case "$1" in
        url)        printf "%s\n" "https://www.x-cmd.com" ;;
        license)    printf "%s\n" "Apache-2.0" ;;
        summary)    printf "%s\n" "Bootstrap 1000+ command line tools in seconds" ;;
        desc)       printf "%s\n" "Ultimate Integration of POSIX SHELL AWK. A super weapon built for the super engineer" ;;
        *) ;;
    esac
}

___x_cmd_version_build_pre___(){
    local version="$1"
    local tgzfile="$2"
    local builddir="$3"
    if [ -n "$tgzfile" ] && [ -f "$tgzfile" ]; then
        ___x_cmd mkdirp "$builddir"
        ___x_cmd_cmds cp "$tgzfile" "$builddir/x-cmd-${version}.tgz"
    else
        local tgzfilepath="$builddir/x-cmd-${version}.tgz"
        ___x_cmd ensurefp "$tgzfilepath" || return 1
        ___x_cmd curl --fail "https://oss.resource.x-cmd.com/x-cmd/release/dist/$version/allinone.tgz" > "$tgzfilepath"
    fi
}
