name: hdf4 CMake cygwin

# Triggers the workflow on a call from another workflow
on:
  workflow_call:
    inputs:
      snap_name:
        description: 'The name in the source tarballs'
        type: string
        required: false
        default: hdfsrc
      file_base:
        description: "The common base name of the source tarballs"
        required: true
        type: string
      use_environ:
        description: 'Environment to locate files'
        type: string
        required: true
        default: snapshots

permissions:
  contents: read

jobs:
  cygwin_build_and_test:
    name: "cygwin-${{ inputs.build_mode }}"
    runs-on: windows-latest
    timeout-minutes: 30
    steps:
      - name: Set git to use LF
        run: |
          git config --global core.autocrlf input

      - name: Get Sources
        uses: actions/checkout@v4.1.7

      - name: Install Cygwin
        uses: cygwin/cygwin-install-action@master
        with:
          packages: cmake gcc-fortran libjpeg-devel make ninja zlib-devel

      - name: Set file base name (Cygwin)
        id: set-file-base
        run: |
          FILE_NAME_BASE=$(echo "${{ inputs.file_base }}")
          echo "FILE_BASE=$FILE_NAME_BASE" >> $GITHUB_OUTPUT
          if [[ '${{ inputs.use_environ }}' == 'release' ]]
          then
            SOURCE_NAME_BASE=$(echo "${{ inputs.snap_name }}")
          else
            SOURCE_NAME_BASE=$(echo "hdfsrc")
          fi
          echo "SOURCE_BASE=$SOURCE_NAME_BASE" >> $GITHUB_OUTPUT
        shell: C:\cygwin\bin\bash.exe -eo pipefail -o igncr '{0}'

      # Get files created by release script
      - name: Get zip-tarball (Cygwin)
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
              name: zip-tarball
              path: ${{ github.workspace }}

      - name: List files for the space (Cygwin)
        run: |
            ls -l ${{ github.workspace }}
            ls ${{ runner.workspace }}

      - name: Uncompress source (Cygwin)
        working-directory: ${{ github.workspace }}
        run: 7z x ${{ steps.set-file-base.outputs.FILE_BASE }}.zip
        shell: C:\cygwin\bin\bash.exe -eo pipefail -o igncr '{0}'

      - name: Copy script files for the space (Cygwin)
        run: |
          cp ${{ github.workspace }}/${{ steps.set-file-base.outputs.SOURCE_BASE }}/config/cmake/scripts/CTestScript.cmake ${{ runner.workspace }}/hdf4
          cp ${{ github.workspace }}/${{ steps.set-file-base.outputs.SOURCE_BASE }}/config/cmake/scripts/HDF4config.cmake ${{ runner.workspace }}/hdf4

      - name: List files for the hdf4 (Cygwin)
        run: |
            ls ${{ runner.workspace }}/hdf4

      - name: Create options file (Cygwin)
        uses: "DamianReeves/write-file-action@master"
        with:
          path: ${{ runner.workspace }}/hdf4/HDF4options.cmake
          write-mode: overwrite
          contents: |
            set (CTEST_DROP_SITE_INIT "my.cdash.org")
            # Change following line to submit to your CDash dashboard to a different CDash project
            #set (CTEST_DROP_LOCATION_INIT "/submit.php?project=HDF4")
            set (MODEL "GHDaily")
            set (GROUP "GHDaily")
            set (SITE_BUILDNAME_SUFFIX "${{ steps.set-file-base.outputs.FILE_BASE }}")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} --log-level=VERBOSE")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DBUILD_SHARED_LIBS:BOOL=OFF")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DHDF4_BUILD_JAVA:BOOL=OFF")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DHDF4_BUILD_FORTRAN:BOOL=OFF")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DJPEG_USE_LOCALCONTENT:BOOL=OFF")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DLIBAEC_USE_LOCALCONTENT:BOOL=OFF")
            set (ADD_BUILD_OPTIONS "${ADD_BUILD_OPTIONS} -DZLIB_USE_LOCALCONTENT:BOOL=OFF")

      - name: Run ctest (Cygwin)
        shell: C:\cygwin\bin\bash.exe -eo pipefail -o igncr '{0}'
        run: |
          export PATH=/usr/bin:$PATH
          cd "${{ runner.workspace }}/hdf4"
          ctest -S HDF4config.cmake,CTEST_SITE_EXT=GH-${{ github.event.repository.full_name }}-CYG,LOCAL_SUBMIT=ON,NINJA=TRUE,BUILD_GENERATOR=Unix,CTEST_SOURCE_NAME=${{ steps.set-file-base.outputs.SOURCE_BASE }} -C Release -VV -O hdf4.log
        continue-on-error: true

      # Save log files created by ctest script
      - name: Save log (Cygwin)
        uses: actions/upload-artifact@v4
        with:
            name: gcc-cygwin-log
            path: ${{ runner.workspace }}/hdf4/hdf4.log
            if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
