#!/bin/bash

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

bold() {
    echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" 
}

clean() {
    bold "cntk clean"
    rm -rf "./third_party/src/cntk"
}

sync() {
    bold "cntk sync"
    case ${OSTYPE} in
        linux*|darwin*)
            [ -d "./third_party/src/cntk" ] || git clone --quiet --recursive https://github.com/Microsoft/CNTK.git "./third_party/src/cntk"
            pushd "./third_party/src/cntk" > /dev/null
            git pull --quiet --prune
            git submodule sync --quiet
            git submodule update --quiet --init --recursive
            popd > /dev/null
        ;;
        *)
            [ -d "./third_party/src/cntk" ] || git clone --quiet https://github.com/Microsoft/CNTK.git "./third_party/src/cntk"
            pushd "./third_party/src/cntk" > /dev/null
            git pull --quiet --prune
            popd > /dev/null
        ;;
    esac
}

schema() {
    bold "cntk schema"
    [[ $(grep -U $'\x0D' ./src/cntk-proto.js) ]] && crlf=1
    npx pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case -r cntk -o ./src/cntk-proto.js ./third_party/src/cntk/Source/CNTKv2LibraryDll/proto/CNTK.proto
    node ./tools/update_pbjs.js array ./src/cntk-proto.js value float 1
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./src/cntk-proto.js ./src/cntk-proto.js
    fi
}

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