on:
  workflow_dispatch:
  schedule:
  - cron: 0 6 * * *
  push:
    branches:
    - main
    paths:
    - '.github/workflows/daily.yml'
  pull_request:
    branches:
    - main
    paths:
    - '.github/workflows/daily.yml'

name: Daily
permissions:
  contents: read
jobs:
  change_log:
    name: 'Create ChangeLog.md'
    runs-on: ubuntu-24.04

    steps:
    - name: Checkout
      uses: actions/checkout@v5
      with:
        fetch-depth: 0

    - name: Install dependencies
      run: npm install -g auto-changelog

    - name: Create ChangeLog.md
      run: |
        export NEXT_VERSION=$(grep -oP "PACKAGE_VERSION='\K[0-9\.-]*" configure)
        git tag $NEXT_VERSION
        auto-changelog --sort-commits date
        mkdir artifacts
        mv ChangeLog.md artifacts/ChangeLog.md

    - name: Upload ChangeLog.md
      uses: actions/upload-artifact@v5
      with:
        name: ChangeLog
        path: artifacts

  build_linux:
    name: 'Linux Q${{matrix.quantum}}-x64 hdri=${{matrix.hdri}} (${{matrix.modules}})'
    container:
      image: ubuntu:20.04
    runs-on: ubuntu-24.04

    strategy:
      fail-fast: false
      matrix:
        quantum: [ 8, 16 ]
        hdri: [ yes, no ]
        modules: [ 'with-modules', 'without-modules' ]
        exclude:
          - quantum: 8
            hdri: yes

    steps:
    - name: Install dependencies
      run: |
        set -e
        export DEBIAN_FRONTEND=noninteractive
        apt update
        apt-get install -y autoconf gcc g++ libfontconfig1-dev libfreetype6-dev libltdl-dev make pkg-config

    - name: Clone msttcorefonts
      uses: actions/checkout@v5
      with:
        repository: ImageMagick/msttcorefonts
        ref: refs/heads/main

    - name: Install msttcorefonts
      run: |
        set -e
        ./install.sh

    - uses: actions/checkout@v5

    - name: Configure ImageMagick
      run: |
        export CFLAGS="-Wno-deprecated-declarations"
        ./configure --with-quantum-depth=${{matrix.quantum}} --enable-hdri=${{matrix.hdri}} --${{matrix.modules}}

    - name: Build ImageMagick
      run: |
        make

    - name: Test ImageMagick
      run: |
        make check || exit_code=$?
        if [ "$exit_code" != "0" ] ; then cat ./test-suite.log ; fi
        exit $exit_code

  build_windows:
    name: Windows ${{matrix.quantum}}${{matrix.hdri_flag}}-${{matrix.architecture}} (${{matrix.buildType}})
    needs:
      - change_log
    runs-on: windows-2022

    strategy:
      fail-fast: false
      matrix:
        architecture: [ x64, x86 ]
        buildType: [ dynamic, static ]
        quantum: [ Q8, Q16, Q32 ]
        hdri: [ hdri, noHdri ]
        exclude:
          - quantum: Q8
            hdri: hdri
          - quantum: Q32
            hdri: noHdri
        include:
          - hdri: hdri
            hdri_flag: -HDRI

    steps:
    - name: Clone ImageMagick
      uses: actions/checkout@v5
      with:
        path: ImageMagick

    - name: Download configure
      shell: cmd
      run: |
        ImageMagick\.github\build\windows\download-configure.cmd

    - name: Download dependencies
      shell: cmd
      run: |
        ImageMagick\.github\build\windows\download-dependencies.cmd windows-${{matrix.architecture}}-${{matrix.buildType}}-openMP.zip

    - name: Download ChangeLog.md
      uses: actions/download-artifact@v6
      with:
        name: ChangeLog
        path: ImageMagick-Windows\ImageMagick

    - name: Configure ImageMagick
      shell: cmd
      working-directory: Configure
      run: |
        Configure.Release.x64.exe /noWizard /VS2022 /deprecated /${{matrix.architecture}} /${{matrix.buildType}} /${{matrix.quantum}} /${{matrix.hdri}}

    - name: Build ImageMagick
      shell: cmd
      run: |
        call "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat"
        msbuild /m /t:Rebuild /p:Configuration=Release,Platform=${{matrix.architecture}}
