name: Ubuntu-dep-src

# https://www.jeffgeerling.com/blog/2020/running-github-actions-workflow-on-schedule-and-other-events
on:
  push:
  pull_request:
  schedule:
    - cron: '0 2 * * SUN'

jobs:
  build-ubuntu-dep-src:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04, ubuntu-22.04]

    steps:
    # https://github.com/marketplace/actions/cancel-workflow-action
    - name: Cancel Previous Runs
      uses: styfle/cancel-workflow-action@0.9.1
      with:
        access_token: ${{ github.token }}

    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Print system information
      run: lscpu

    - name: Print OS information
      run: lsb_release -a

    - name: Install dependencies for ubuntu 18.04 and 20.04
      if: matrix.os != 'ubuntu-22.04'
      run: |
        sudo apt-get update && sudo apt-get install -y libdc1394-22-dev

    - name: Install dependencies for ubuntu 22.04
      if: matrix.os == 'ubuntu-22.04'
      run: |
        sudo apt-get update && sudo apt-get install -y libdc1394-dev

    - name: Install common dependencies for ubuntu
      run: |
        sudo apt-get update
        sudo apt-get install -y libx11-dev libv4l-dev gfortran liblapack-dev libeigen3-dev
        sudo apt-get install -y libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev
        sudo apt-get install -y mesa-common-dev mesa-utils freeglut3-dev libflann-dev libboost-all-dev
        sudo apt-get install -y nlohmann-json3-dev

    - name: Install dependencies for ubuntu 22.04
      if: matrix.os == 'ubuntu-22.04'
      run: |
        sudo apt-get update && sudo apt-get install -y libx11-dev libdc1394-dev libv4l-dev gfortran liblapack-dev libeigen3-dev
        sudo apt-get update && sudo apt-get install -y libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev

    - name: Build OpenBLAS from source
      run: |
        pwd
        echo $GITHUB_WORKSPACE
        git clone --depth 1 https://github.com/xianyi/OpenBLAS.git ${HOME}/OpenBLAS
        cd ${HOME}/OpenBLAS
        mkdir install
        make -j$(nproc)
        make -j$(nproc) install PREFIX=$(pwd)/install
        echo "OpenBLAS_HOME=$(pwd)/install" >> $GITHUB_ENV
        echo $OpenBLAS_HOME

    - name: Build VTK from source
      run: |
        pwd
        git clone --recursive --depth 1 https://github.com/Kitware/VTK.git ${HOME}/VTK
        cd ${HOME}/VTK
        mkdir build && cd build && mkdir install
        cmake .. -DVTK_ANDROID_BUILD=OFF -DVTK_BUILD_DOCUMENTATION=OFF -DVTK_BUILD_EXAMPLES=OFF -DVTK_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release \
          -DVTK_GROUP_ENABLE_Imaging=DONT_WANT -DVTK_GROUP_ENABLE_MPI=DONT_WANT -DVTK_GROUP_ENABLE_Web=DONT_WANT -DCMAKE_INSTALL_PREFIX=$(pwd)/install
        make -j$(nproc) install
        echo "VTK_DIR=$(pwd)/install" >> $GITHUB_ENV
        echo $VTK_DIR

    - name: Build OpenCV from source
      run: |
        pwd
        git clone --depth 1 https://github.com/opencv/opencv.git ${HOME}/opencv
        cd ${HOME}/opencv
        mkdir build && cd build && mkdir install
        cmake .. -DBUILD_EXAMPLES=OFF -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(pwd)/install
        make -j$(nproc) install
        echo "OpenCV_DIR=$(pwd)/install" >> $GITHUB_ENV
        echo $OpenCV_DIR

    - name: Build librealsense2 from source
      run: |
        pwd
        git clone --depth 1 https://github.com/IntelRealSense/librealsense.git ${HOME}/librealsense
        cd ${HOME}/librealsense
        mkdir build && cd build && mkdir install
        cmake .. -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(pwd)/install
        make -j$(nproc) install
        echo "REALSENSE2_DIR=$(pwd)/install" >> $GITHUB_ENV
        echo $REALSENSE2_DIR

    - name: Build PCL from source
      run: |
        pwd
        git clone --depth 1 https://github.com/PointCloudLibrary/pcl.git ${HOME}/pcl
        cd ${HOME}/pcl
        mkdir build && cd build && mkdir install
        cmake .. -DBUILD_tools=OFF -DBUILD_global_tests=OFF -DPCL_DISABLE_GPU_TESTS=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$(pwd)/install
        make -j$(nproc) install
        echo "PCL_DIR=$(pwd)/install" >> $GITHUB_ENV
        echo $PCL_DIR

    - name: Clone visp-images
      env:
        BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
      # https://remarkablemark.org/blog/2022/09/25/check-git-branch-exists-in-remote-repository/
      run: |
        git clone --depth 1 https://github.com/lagadic/visp-images ${HOME}/visp-images
        echo "VISP_INPUT_IMAGE_PATH=$HOME/visp-images" >> $GITHUB_ENV
        echo ${VISP_INPUT_IMAGE_PATH}

    - name: Configure CMake
      run: |
        pwd
        mkdir build && cd build && mkdir install
        cmake .. -DCMAKE_INSTALL_PREFIX=$(pwd)/install
        cat ViSP-third-party.txt

    - name: Compile
      working-directory: build
      run: |
        make -j$(nproc) install
        echo "VISP_DIR=$(pwd)/install" >> $GITHUB_ENV
        echo $VISP_DIR

    - name: Run unit tests
      working-directory: build
      run: ctest -j$(nproc) --output-on-failure

    - name: Clone camera_localization
      run: |
        git clone --depth 1 https://github.com/lagadic/camera_localization ${HOME}/camera_localization

    - name: Build camera_localization with ViSP as 3rdparty
      run: |
        cd ${HOME}/camera_localization
        mkdir camera_localization-build
        cd camera_localization-build
        cmake ..
        make -j$(nproc)

    - name: Clone visp_started
      run: |
        git clone --depth 1 https://github.com/lagadic/visp_started ${HOME}/visp_started

    - name: Build visp_started with ViSP as 3rdparty
      run: |
        cd ${HOME}/visp_started
        mkdir visp_started-build
        cd visp_started-build
        cmake ..
        make -j$(nproc)
