#!/bin/bash

set -e

root=$(cd $(dirname ${0})/..; pwd)
build=${root}/build
tools=${root}/tools
third_party=${root}/third_party

python=${python:-python}
pip=${pip:-pip}

identifier=coremltools
repository=https://github.com/apple/${identifier}.git

mkdir -p ${third_party}

if [ -d "${third_party}/${identifier}" ]; then
    git -C "${third_party}/${identifier}" fetch -p --quiet
    git -C "${third_party}/${identifier}" reset --quiet --hard origin/master
else
    echo "Clone ${repository}..."
    git -C "${third_party}" clone --recursive ${repository}
fi

export PYTHONUSERBASE=${build}/third_party/pypi/${identifier}
export PATH=$PATH:${PYTHONUSERBASE}/bin
rm -rf ${PYTHONUSERBASE}
${pip} install --quiet --user onnx
${pip} install --quiet --user sklearn
${pip} install --quiet --user ${third_party}/${identifier}

${python} ${tools}/coreml-converter.py $@
