#!/bin/bash -v

cd "$(dirname "$0")"

mkdir -p build
cd build
emcmake cmake  -DCMAKE_INTERPROCEDURAL_OPTIMIZATION:BOOL=TRUE -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=TRUE -DBUILD_TESTS:BOOL=FALSE -DBUILD_PERF_TESTS:BOOL=FALSE -DBUILD_opencv_apps:BOOL=FALSE -DBUILD_DOCS:BOOL=FALSE -DWITH_TBB:BOOL=TRUE -DWITH_CUDA:BOOL=FALSE -DWITH_IPP:BOOL=FALSE -DWITH_EIGEN:BOOL=FALSE -DOPENCV_EXTRA_MODULES_PATH=$PWD/../../../opencv_contrib/modules -DBUILD_opencv_ts:BOOL=FALSE -DBUILD_opencv_java:BOOL=FALSE -DBUILD_opencv_python2:BOOL=FALSE -DBUILD_opencv_python3:BOOL=FALSE -DBUILD_SHARED_LIBS:BOOL=FALSE -DCMAKE_BUILD_TYPE:String="Release" -DCMAKE_CXX_STANDARD:String="11" -DBUILD_ITT:BOOL=FALSE -DCV_ENABLE_INTRINSICS:BOOL=FALSE -DWITH_OPENCL:BOOL=OFF -DBUILD_JPEG:BOOL=TRUE -DBUILD_PNG:BOOL=TRUE -DBUILD_TIFF:BOOL=OFF -DWITH_TIFF:BOOL=OFF -DEMGU_CV_WITH_TIFF:BOOL=OFF -DEMGU_CV_WITH_TESSERACT:BOOL=FALSE -DWITH_PTHREADS_PF:BOOL=OFF -DEMGU_CV_WITH_DEPTHAI:BOOL=OFF $PWD/../../..
emmake make cvextern VERBOSE=1
#-DEMSCRIPTEN_GENERATE_BITCODE_STATIC_LIBRARIES:BOOL=TRUE -DEMSCRIPTEN_GENERATE_BITCODE_ST:BOOL=TRUE
cd ..
emcc -flto -r -o ../../libs/libcvextern.bc build/bin/webgl/*.bc ../../libs/webgl/*.bc 
