#!/bin/bash

# Install dependency
OS_TYPE=`grep "^ID=" /etc/os-release | awk -F'=' '{print $2}'`
if [ "${OS_TYPE}" = "ubuntu" ]; then
    # compiler
    apt install -y build-essential
    # Required
    apt install -y libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
fi

# Download
rm -rf /tmp/opencv
mkdir -p /tmp/opencv
cd /tmp/opencv
git clone https://github.com/opencv/opencv.git -b 4.3.0
#git clone https://github.com/opencv/opencv_contrib.git -b 4.3.0

# Build
cd opencv
mkdir build
cd build/
cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/home/miniconda3/envs/ci \
  -D PYTHON3_EXECUTABLE=/home/miniconda3/envs/ci/bin/python3 \
  -D PYTHON3_INCLUDE_DIR=/home/miniconda3/envs/ci/include/python3.7m \
  -D PYTHON3_LIBRARY=/home/miniconda3/envs/ci/lib/libpython3.7m.so \
  -D PYTHON3_PACKAGES_PATH=/home/miniconda3/envs/ci/lib/python3.7/site-packages \
  -D PYTHON3_NUMPY_PATH=/home/miniconda3/envs/ci/lib/python3.7/site-packages \
  -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/miniconda3/envs/ci/lib/python3.7/site-packages/numpy/core/include \
  -D BUILD_opencv_python3=ON \
  -D BUILD_opencv_python2=OFF \
  -D WITH_PROTOBUF=OFF \
  -D WITH_WEBP=OFF \
  -D WITH_IPP=OFF \
  -D WITH_ADE=OFF \
  -D BUILD_ZLIB=ON \
  -D BUILD_JPEG=ON \
  -D BUILD_PNG=ON \
  -D BUILD_OPENEXR=ON \
  -D BUILD_TESTS=OFF \
  -D BUILD_PERF_TESTS=OFF \
  -D CMAKE_SKIP_RPATH=TRUE \
  -D WITH_FFMPEG=OFF \
  -D WITH_TIFF=ON \
  -D BUILD_TIFF=OFF \
  -D WITH_JASPER=OFF \
  -D BUILD_JASPER=OFF \
  -D BUILD_SHARED_LIBS=OFF \
  ..
make -j2

# Copy lib
cp -a lib/python3/cv2.cpython-37m-aarch64-linux-gnu.so /home/miniconda3/envs/ci/lib/python3.7/site-packages/
