#!/bin/bash

set -e

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

clean() {
    echo "xmodel clean"
    rm -rf "./third_party/source/xmodel"
}

sync() {
    echo "xmodel sync"
    mkdir -p ./third_party/source/xmodel/src/vai_runtime/xir/src/xir/proto
    curl --silent --show-error --location --output "./third_party/source/xmodel/src/vai_runtime/xir/src/xir/proto/graph_proto_v2.proto" "https://github.com/Xilinx/Vitis-AI/raw/master/src/vai_runtime/xir/src/xir/proto/graph_proto_v2.proto"
}


schema() {
    echo "xmodel schema"
    [[ $(grep -U $'\x0D' ./source/xmodel-proto.js) ]] && crlf=1
    node ./tools/protoc.js --binary --text --root xmodel --out ./source/xmodel-proto.js ./third_party/source/xmodel/src/vai_runtime/xir/src/xir/proto/graph_proto_v2.proto
    rm -rf ${temp}
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/xmodel-proto.js ./source/xmodel-proto.js
    fi
}

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