git submodule init
git submodule update
mkdir build
mkdir -p build/tensoradapter/pytorch
mkdir -p build/dgl_sparse
mkdir -p build/graphbolt

if [[ "$CACHEDIR" = "" ]]; then
  echo No prebuilt binary directory specified, building with default options...
  cd build
  cmake -DBUILD_TYPE=release -DUSE_CUDA=$USE_CUDA ..
  make -j4
  cd ..
else
  if [[ "$OSTYPE" = "darwin"* ]]; then
    echo Copying prebuilt CPU binary...
    cp $CACHEDIR/libdgl.dylib build
    if [[ -e $CACHEDIR/tensoradapter_pytorch_cpu ]]; then
      cp -v $CACHEDIR/tensoradapter_pytorch_cpu/*.so build/tensoradapter/pytorch
      cp -v $CACHEDIR/dgl_sparse_cpu/*.so build/dgl_sparse
      cp -v $CACHEDIR/graphbolt_cpu/*.so build/graphbolt
    fi
  else
    if [[ $USE_CUDA = 'ON' ]]; then
      echo Copying prebuilt CUDA $CUDA_VER binary...
      cp -v $CACHEDIR/libdgl.so.cu$CUDA_VER build/libdgl.so
      if [[ -e $CACHEDIR/tensoradapter_pytorch_cu$CUDA_VER ]]; then
        cp -v $CACHEDIR/tensoradapter_pytorch_cu$CUDA_VER/*.so build/tensoradapter/pytorch
        cp -v $CACHEDIR/dgl_sparse_cu$CUDA_VER/*.so build/dgl_sparse
        cp -v $CACHEDIR/graphbolt_cu$CUDA_VER/*.so build/graphbolt
      fi
    else
      echo Copying prebuilt CPU binary...
      cp -v $CACHEDIR/libdgl.so.cpu build/libdgl.so
      if [[ -e $CACHEDIR/tensoradapter_pytorch_cpu ]]; then
        cp -v $CACHEDIR/tensoradapter_pytorch_cpu/*.so build/tensoradapter/pytorch
        cp -v $CACHEDIR/dgl_sparse_cpu/*.so build/dgl_sparse
        cp -v $CACHEDIR/graphbolt_cpu/*.so build/graphbolt
      fi
    fi
  fi
fi
cd python
$PYTHON update_version.py
$PYTHON setup.py install --single-version-externally-managed --record=record.txt
rm -f ../build/libdgl.so ../build/tensoradapter/pytorch/*.so
rm -f ../build/libdgl.so ../build/dgl_sparse/*.so
rm -f ../build/graphbolt/*.so
