name: Build

on: [push,pull_request,workflow_dispatch]

env:
  BUILD_TYPE: Release
  DATA_ROOT: ${{ github.workspace }}/data
  DATA_BRANCH: master
  GENERATOR_PLATFORM:

jobs:
  build:
    strategy:
       matrix:
         os: [ubuntu-latest, windows-latest, macos-latest]
         shared_libs_flag: [ON,OFF]
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v3

    - name: ubuntu-dependencies
      if: startsWith(matrix.os, 'ubuntu')
      run: |
        sudo apt-get install -y gcc-10 g++-10
        sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 \
          --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
        echo DATA_BRANCH=linux-release >> $GITHUB_ENV
        sudo apt install exiftool


    - name: macos-dependencies
      if: startsWith(matrix.os, 'macos')
      run: |
        echo DATA_BRANCH=osx >> $GITHUB_ENV
        brew upgrade
        brew install exiftool
        #perl -MImage::ExifTool -e ""

    - name: windows-dependencies
      if: startsWith(matrix.os, 'windows')
      run: |
        echo "C:/Users/runneradmin/AppData/Roaming/Python/Python39/Scripts" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
        echo $env:GITHUB_PATH
        $generator= "-DCMAKE_GENERATOR_PLATFORM=x64"
        echo "Generator: ${generator}"
        echo "GENERATOR_PLATFORM=$generator" >> $env:GITHUB_ENV
        choco install wget 7zip --no-progress

    - name: dependencies
      run: |
        cmake -E make_directory ${{ github.workspace }}/build
        git clone --depth=1 --branch=${{ env.DATA_BRANCH }} https://github.com/GrokImageCompression/grok-test-data.git ${{ env.DATA_ROOT }}

    - name: configure cmake
      # Use a bash shell so we can use the same syntax for environment variable
      # access regardless of the host operating system
      shell: bash
      working-directory: ${{ github.workspace }}/build
      # Note the current convention is to use the -S and -B options here to specify source 
      # and build directories, but this is only available with CMake 3.13 and higher.  
      # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
      run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBUILD_SHARED_LIBS=${{ matrix.shared_libs_flag }} -DBUILD_TESTING:BOOL=ON -DGRK_DATA_ROOT=$DATA_ROOT $GENERATOR_PLATFORM

    - name: build
      working-directory: ${{ github.workspace }}/build
      shell: bash
      # Execute the build.  You can specify a specific target with "--target <NAME>"
      run: cmake --build . --config $BUILD_TYPE

    - name: test
      working-directory: ${{ github.workspace }}/build
      shell: bash
      #if: "!startsWith(matrix.os, 'windows')"
      run: ctest --output-on-failure -C $BUILD_TYPE

    - name: package
      if: ${{  matrix.shared_libs_flag == 'ON' }}
      working-directory: ${{ github.workspace }}/build
      shell: bash
      run: |
        cmake -Wno-dev -D CPACK_GENERATOR:STRING=ZIP -D CPACK_PACKAGE_FILE_NAME:STRING=grok-${{ matrix.os }} .
        cmake --build . --config $BUILD_TYPE --target package
        7z x grok-${{ matrix.os }}.zip

    - name: publish
      uses: actions/upload-artifact@v3
      with:
        name: grok-${{ matrix.os }}
        path: ${{ github.workspace }}/build/grok-${{ matrix.os }}

    - name: Upload binaries to release
      uses: softprops/action-gh-release@v1
      if: ${{startsWith(github.ref, 'refs/tags/') }}
      with:
          files: build/grok-${{ matrix.os }}.zip 

