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

if [[ "$CACHEDIR" = "" ]]; then
	echo No prebuilt binary directory specified, building with default options...
	cd build
	cmake -DUSE_OPENMP=ON -DUSE_CUDA=$USE_CUDA -DBUILD_TORCH=ON ..
	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
		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
				echo Skip
				#cp -v $CACHEDIR/tensoradapter_pytorch_cu$CUDA_VER/*.so build/tensoradapter/pytorch
				#cp -v $CACHEDIR/dgl_sparse_cu$CUDA_VER/*.so build/dgl_sparse
			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
			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
