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

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

    local version="" versum="" tgzfile="" dist="" 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        ;;
            --dist)         dist="$2";     arg:2:shift        ;;
            --docker)
                            mode="docker" ;  shift  ;;
            which)
                            shift; ___x_cmd_version_build_which "$@" ; return $? ;;
            test)
                            shift; ___x_cmd_version_build_test  "$@" ; 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

    if [ -n "$tgzfile" ] && [ -n "$dist" ]; then
        N=version M="Cannot use --tgzfile and --dist together" log:ret:1
    fi

    ___x_cmd_version_build_pre___   "$version" "$tgzfile" "$dist" "$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 dist="$3"
    local builddir="$4"
    local tgzfilepath="$builddir/x-cmd-${version}.tgz"

    if [ -n "$tgzfile" ] && [ -f "$tgzfile" ]; then
        ___x_cmd mkdirp "$builddir"
        ___x_cmd_cmds cp "$tgzfile" "$tgzfilepath"
    elif [ -n "$dist" ]; then
        [ -d "$dist" ] || N=version M="Not found dist directory: $dist" log:ret:1
        ___x_cmd mkdirp "$builddir"
        ___x_cmd rmrf "$tgzfilepath"
        local abs_tgzfilepath; abs_tgzfilepath="$(cd "$builddir" && pwd)/x-cmd-${version}.tgz" || return 1
        x:info "Pack dist directory $dist to $abs_tgzfilepath"
        (
            ___x_cmd_cd0 "$dist" || return 1
            [ ! -f LICENSE ] || mv LICENSE LICENSE.txt
            {
                printf "%s\n" .x-cmd
                printf "%s\n" *
            } | command grep -v README | command grep -v changelog | ___X_CMD_ZUZ_QUIET=1 x z "$abs_tgzfilepath" >/dev/null
        ) || return 1
        [ -f "$abs_tgzfilepath" ] || return 1
    else
        ___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
}
