#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

env_dir=./third_party/env/onnx
src_dir=./third_party/source/onnx

case "${OSTYPE}" in
    msys*|cygwin*|mingw*) python="winpty python";;
    *) python="python";;
esac

venv() {
    env_dir=./third_party/env/onnx
    [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
    case "${OSTYPE}" in
        msys*|cygwin*|mingw*) source ${env_dir}/Scripts/activate;;
        *) source ${env_dir}/bin/activate;;
    esac
    ${python} -m pip install --quiet --upgrade pip setuptools wheel
}

clean() {
    echo "onnx clean"
    rm -rf "${env_dir}"
    rm -rf "${src_dir}"
    rm -rf "./third_party/bin/protobuf"
}

sync() {
    echo "onnx sync"
    onnx_src_dir="${src_dir}/onnx"
    if [ ! -d "${onnx_src_dir}" ]; then
        git clone --quiet --depth=1 --branch main --single-branch https://github.com/onnx/onnx.git "${onnx_src_dir}"
    else
        git -C "${onnx_src_dir}" fetch --quiet --depth=1 origin main
        git -C "${onnx_src_dir}" reset --quiet --hard FETCH_HEAD
        git -C "${onnx_src_dir}" gc --quiet --prune=all
    fi
    popd > /dev/null
    mkdir -p "./third_party/source/onnx/onnxruntime/core/flatbuffers/schema"
    curl --silent --show-error --location --output "./third_party/source/onnx/onnxruntime/core/flatbuffers/schema/ort.fbs" "https://github.com/microsoft/onnxruntime/raw/main/onnxruntime/core/flatbuffers/schema/ort.fbs"
}

install() {
    echo "onnx install"
    onnx_src_dir="${src_dir}/onnx"
    venv
    case "${OSTYPE}" in
        msys*|cygwin*|mingw*)
            # winget install -e --id=Microsoft.VisualStudio.2026.BuildTools --override "--passive --wait --add Microsoft.VisualStudio.Workload.VCTools;includeRecommended"
            protobuf_version=$(grep -m1 "protobuf==" ${onnx_src_dir}/pyproject.toml | sed -E 's/.*protobuf==([0-9.]+).*/\1/')
            protoc_dir="$(pwd)/third_party/bin/protobuf/v${protobuf_version}"
            programfiles_x86=$(env | grep "^ProgramFiles(x86)=" | cut -d '=' -f 2)
            vswhere="${programfiles_x86}\Microsoft Visual Studio\Installer\vswhere.exe"
            build_tools_dir=$("${vswhere}" -latest -products '*' -property installationPath)
            case "$(uname -s)" in
                *ARM64*) cmake_arch="ARM64" ;;
                *) cmake_arch="x64" ;;
            esac
            cmake_dir="${build_tools_dir}\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
            protobuf="protobuf==${protobuf_version}"
            ${python} -m pip install --quiet --upgrade ${protobuf}
            export PATH="$(cygpath -u "${cmake_dir}")":${PATH}
            if [ ! -f "${protoc_dir}/bin/protoc.exe" ]; then
                rm -rf ${protoc_dir}
                git init --quiet ${protoc_dir}/src
                git -C ${protoc_dir}/src fetch --quiet --depth=1 https://github.com/protocolbuffers/protobuf.git v${protobuf_version}
                git -C ${protoc_dir}/src checkout --quiet FETCH_HEAD
                git -C ${protoc_dir}/src submodule update --quiet --init --recursive --depth=1
                pushd "${protoc_dir}/src" > /dev/null
                "${cmake_dir}\cmake.exe" -Wno-deprecated -A ${cmake_arch} -Dprotobuf_MSVC_STATIC_RUNTIME=OFF -Dprotobuf_BUILD_SHARED_LIBS=OFF -Dprotobuf_BUILD_TESTS=OFF -Dprotobuf_BUILD_EXAMPLES=OFF -DABSL_PROPAGATE_CXX_STD=ON -DCMAKE_INSTALL_PREFIX="$(cygpath -w "${protoc_dir}")" . > /dev/null
                "${cmake_dir}\cmake.exe" --build . --config Release > /dev/null
                "${cmake_dir}\cmake.exe" --install . --config Release > /dev/null
                popd > /dev/null
            fi
            export PATH="${protoc_dir}\bin":${PATH}
            export USE_MSVC_STATIC_RUNTIME=0
            ;;
    esac
    ${python} -m pip install --quiet "${onnx_src_dir}"
    ${python} -m pip install --quiet --upgrade flatbuffers numpy packaging protobuf sympy
    ${python} -m pip install --quiet --upgrade onnxruntime --pre --extra-index https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/
    deactivate
}

schema() {
    echo "onnx schema"
    onnx_src_dir="${src_dir}/onnx"
    [[ $(grep -U $'\x0D' ./source/onnx-proto.js) ]] && crlf=1
    node ./tools/protoc.js --binary --text --json --root onnx --out ./source/onnx-proto.js --path ${onnx_src_dir} onnx/onnx-ml.proto onnx/onnx-operators-ml.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/onnx-proto.js ./source/onnx-proto.js
    fi
    [[ $(grep -U $'\x0D' ./source/onnx-schema.js) ]] && crlf=1
    node ./tools/flatc.js --root ort --out ./source/onnx-schema.js ${src_dir}/onnxruntime/core/flatbuffers/schema/ort.fbs
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/onnx-schema.js ./source/onnx-schema.js
    fi
}

metadata() {
    echo "onnx metadata"
    [[ $(grep -U $'\x0D' ./source/onnx-metadata.json) ]] && crlf=1
    venv
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    ${python} ./tools/onnx_script.py
    deactivate
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/onnx-metadata.json ./source/onnx-metadata.json
    fi
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "install") install;;
        "schema") schema;;
        "metadata") metadata;;
    esac
done
