name: Checks

on: [push, pull_request]

jobs:
  Build:
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        environement: [gcc, utf8, minified, minimal, minimal-with-xml, cmake, cmake-dll]
        exclude:
          - os: macos-latest
            environement: minified
          - os: macos-latest
            environement: minimal
          - os: macos-latest
            environement: minimal-with-xml
          - os: macos-latest
            environement: cmake-dll
          - os: windows-latest
            environement: gcc
          - os: windows-latest
            environement: utf8
          - os: windows-latest
            environement: minified
          - os: windows-latest
            environement: minimal
          - os: windows-latest
            environement: minimal-with-xml
        include:
          - environement: gcc
            CXXFLAGS: ""
            RUN_TESTS: "all"
          - environement: cmake
            CXXFLAGS: ""
            RUN_TESTS: "all"
          - environement: minified
            CXXFLAGS: "-DMEDIAINFO_ADVANCED_NO -DMEDIAINFO_ARCHIVE_NO -DMEDIAINFO_DEMUX_NO -DMEDIAINFO_DVDIF_ANALYZE_NO -DMEDIAINFO_EVENTS_NO -DMEDIAINFO_LIBCURL_NO -DMEDIAINFO_LIBMMS_NO -DMEDIAINFO_MD5_NO -DMEDIAINFO_MINIMAL_YES -DMEDIAINFO_MPEGTS_DUPLICATE_NO -DMEDIAINFO_N19_NO -DMEDIAINFO_OTHERTEXT_NO -DMEDIAINFO_TRACE_NO -DMEDIAINFO_READTHREAD_NO -DMEDIAINFO_EXPORT_NO -DMEDIAINFO_TEXT_YES"
            RUN_TESTS: "no"
          - environement: minimal
            CXXFLAGS: "-DMEDIAINFO_ADVANCED_NO -DMEDIAINFO_CONFORMANCE_NO -DMEDIAINFO_REFERENCES_NO -DMEDIAINFO_FILTER_NO -DMEDIAINFO_DUPLICATE_NO -DMEDIAINFO_MACROBLOCKS_NO -DMEDIAINFO_TRACE_NO -DMEDIAINFO_TRACE_FFV1CONTENT_NO -DMEDIAINFO_IBI_NO -DMEDIAINFO_DIRECTORY_NO -DMEDIAINFO_LIBCURL_NO -DMEDIAINFO_LIBMMS_NO -DMEDIAINFO_DVDIF_ANALYZE_NO -DMEDIAINFO_MPEGTS_DUPLICATE_NO -DMEDIAINFO_READTHREAD_NO -DMEDIAINFO_MD5_NO -DMEDIAINFO_SHA1_NO -DMEDIAINFO_SHA2_NO -DMEDIAINFO_EVENTS_NO -DMEDIAINFO_DEMUX_NO -DMEDIAINFO_AES_NO -DMEDIAINFO_FIXITY_NO -DMEDIAINFO_READER_NO -DMEDIAINFO_NEXTPACKET_NO"
            RUN_TESTS: "no"
          - environement: minimal-with-xml
            CXXFLAGS: "-DMEDIAINFO_ADVANCED_NO -DMEDIAINFO_CONFORMANCE_NO -DMEDIAINFO_REFERENCES_NO -DMEDIAINFO_FILTER_NO -DMEDIAINFO_DUPLICATE_NO -DMEDIAINFO_MACROBLOCKS_NO -DMEDIAINFO_TRACE_NO -DMEDIAINFO_TRACE_FFV1CONTENT_NO -DMEDIAINFO_IBI_NO -DMEDIAINFO_DIRECTORY_NO -DMEDIAINFO_LIBCURL_NO -DMEDIAINFO_LIBMMS_NO -DMEDIAINFO_DVDIF_ANALYZE_NO -DMEDIAINFO_MPEGTS_DUPLICATE_NO -DMEDIAINFO_READTHREAD_NO -DMEDIAINFO_MD5_NO -DMEDIAINFO_SHA1_NO -DMEDIAINFO_SHA2_NO -DMEDIAINFO_EVENTS_NO -DMEDIAINFO_DEMUX_NO -DMEDIAINFO_AES_NO -DMEDIAINFO_FIXITY_NO -DMEDIAINFO_NEXTPACKET_NO -DMEDIAINFO_ALL_NO -DMEDIAINFO_MIXML_YES"
            RUN_TESTS: "mixml"
      fail-fast: false
    runs-on: ${{ matrix.os }}
    env:
      S3_KEY: ${{ secrets.S3_KEY }}
      S3_PASS: ${{ secrets.S3_PASS }}
      CXXFLAGS: -Werror -Wno-error=deprecated-declarations ${{ matrix.CXXFLAGS }}
      RUN_TESTS: ${{ matrix.RUN_TESTS }}
    steps:
      - name: Checkout
        uses: actions/checkout@v5
      - name: Dependencies
        if: ${{ runner.os != 'Windows' }}
        run: |
          if [ "$RUNNER_OS" == "Linux" ]; then
            sudo apt-get update -y
            sudo apt-get install -y libxml2-utils zlib1g-dev libcurl4-gnutls-dev libmms-dev ${{ matrix.environement == 'cmake-dll' && 'g++-mingw-w64-x86-64' || '' }}
          fi
          if [ "$RUNNER_OS" == "macOS" ]; then
            brew install libtool automake
          fi
      - name: zlib
        if: ${{ (runner.os == 'Windows') || matrix.environement == 'cmake-dll' }}
        run: |
          git -C .. clone --depth=1 https://github.com/MediaArea/zlib.git
      - name: ZenLib
        run: |
          git -C .. clone --depth=1 https://github.com/MediaArea/ZenLib.git
      - name: Configure
        if: ${{ (matrix.environement != 'cmake') && (matrix.environement != 'cmake-dll') }}
        run: |
          pushd ../ZenLib/Project/GNU/Library
            autoreconf -if
            ./configure --enable-silent-rules --enable-static ${{ matrix.environement == 'utf8' && '--disable-unicode' || '' }}
            make
          popd
          cd Project/GNU/Library
          autoreconf -if
          ./configure --enable-silent-rules --with-libcurl=runtime
      - name: Build
        if: ${{ (matrix.environement != 'cmake') && (matrix.environement != 'cmake-dll') }}
        run: |
          cd Project/GNU/Library
          make -j4
      - name: Check files
        if: ${{ (matrix.environement != 'cmake') && (matrix.environement != 'cmake-dll') }}
        run: |
          cd Project/GNU/Library
          make clone_regression_files
      - name: Check
        if: ${{ (matrix.environement != 'cmake') && (matrix.environement != 'cmake-dll') }}
        run: |
          sudo npm install -g jsonlint
          cd Project/GNU/Library
          make check
      - name: CMake Build
        if: ${{ (matrix.environement == 'cmake') || (matrix.environement == 'cmake-dll') }}
        shell: bash
        run: |
          if [ "$RUNNER_OS" == "Windows" ]; then
            export CXXFLAGS="/std:c++20 /WX /MP"
          fi
          # cmake toolchain file for mingw-w64 x86_64 builds on Ubuntu
          if [ "$RUNNER_OS" == "Linux" ] && [ "${{ matrix.environement }}" == "cmake-dll" ]; then
            git clone --depth=1 https://gist.github.com/8cf73f7f12ba270aa8192d6911972fe8.git toolchain
            export CMAKE_TOOLCHAIN_FILE=${{ github.workspace }}/toolchain/mingw-w64-x86_64.cmake
          fi
          mkdir Project/CMake/Build
          cd Project/CMake/Build
          cmake -DBUILD_ZENLIB=1 -DZLIB_BUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release ${{ matrix.environement == 'cmake-dll' && '-DBUILD_SHARED_LIBS=ON' || '' }} ..
          cmake --build . ${{ runner.os == 'Windows' && '--config Release' || '-j4' }}

  MSBuild:
    runs-on: windows-latest
    strategy:
      matrix:
        architecture: [ Win32, x64, ARM64 ]
      fail-fast: false
    steps:
      - name: Checkout zlib
        uses: actions/checkout@v5
        with:
          repository: MediaArea/zlib
          path: zlib
      - name: Checkout ZenLib
        uses: actions/checkout@v5
        with:
          repository: MediaArea/ZenLib
          path: ZenLib
      - name: Checkout MediaInfoLib
        uses: actions/checkout@v5
        with:
          path: MediaInfoLib
      - name: Add msbuild to PATH
        uses: microsoft/setup-msbuild@v2
        with:
          msbuild-architecture: x64
      - name: Build
        run: msbuild -p:Configuration=Release -p:Platform=${{ matrix.architecture }} ${{ github.workspace }}\MediaInfoLib\Project\MSVC2022\MediaInfoLib.sln -verbosity:quiet -warnaserror
