name: hdf4 examples bintest runs

# Triggers the workflow on a call from another workflow
on:
  workflow_call:
    inputs:
      build_mode:
        description: "release vs. debug build"
        required: true
        type: string

permissions:
  contents: read

jobs:
  test_binary_win:
  # Windows w/ MSVC + CMake
  #
    name: "Windows MSVC Binary Test"
    runs-on: windows-latest
    steps:
      - name: Install Dependencies (Windows)
        run: choco install ninja

      - name: Set up JDK 19
        uses: actions/setup-java@v4
        with:
          java-version: '19'
          distribution: 'temurin'

      - name: Enable Developer Command Prompt
        uses: ilammy/msvc-dev-cmd@v1.13.0

      # Get files created by cmake-ctest script
      - name: Get published binary (Windows)
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
              name: zip-vs2022_cl-${{ inputs.build_mode }}-binary
              path: ${{ github.workspace }}/hdf4

      - name: Uncompress hdf4 binary (Win)
        working-directory: ${{ github.workspace }}/hdf4
        run: 7z x HDF-*-win64.zip
        shell: bash

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

      - name: create hdf4 location (Win)
        working-directory: ${{ github.workspace }}/hdf4
        run: |
          New-Item -Path "${{ github.workspace }}/HDF_Group/HDF" -ItemType Directory
          Copy-Item -Path "${{ github.workspace }}/hdf4/HDF*/*" -Destination "${{ github.workspace }}/HDF_Group/HDF" -Recurse
        shell: pwsh

      - name: List files for the space (Win)
        run: ls -l ${{ github.workspace }}/HDF_Group/HDF

      - name: set hdf4lib name
        id: set-hdf4lib-name
        run: |
          HDF4DIR="${{ github.workspace }}/HDF_Group/HDF"
          echo "HDF4_ROOT=$HDF4DIR$FILE_NAME_HDF4" >> $GITHUB_OUTPUT
        shell: bash

      - name: List files for the binaries (Win)
        run: |
            ls -l ${{ github.workspace }}/HDF_Group/HDF

      - name: using powershell
        shell: pwsh
        run: Get-Location

      - name: List files for the space (Windows)
        run: |
              Get-ChildItem -Path ${{ github.workspace }}
              Get-ChildItem -Path ${{ runner.workspace }}
        shell: pwsh

      - name: Run ctest (Windows)
        env:
          HDF4_ROOT: ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}
        run: |
          cd "${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/HDF4Examples"
          cmake --workflow --preset=ci-StdShar-MSVC --fresh
        shell: bash

  test_binary_linux:
  # Linux (Ubuntu) w/ gcc + CMake
  #
    name: "Ubuntu gcc Binary Test"
    runs-on: ubuntu-latest
    steps:
      - name: Install CMake Dependencies (Linux)
        run: |
          sudo apt-get update
          sudo apt-get install ninja-build doxygen graphviz

      - name: Set up JDK 19
        uses: actions/setup-java@v4
        with:
          java-version: '19'
          distribution: 'temurin'

      - name: Get published binary (Linux)
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
              name: tgz-ubuntu-2404_gcc-${{ inputs.build_mode }}-binary
              path: ${{ github.workspace }}

      - name: Uncompress hdf4 binary (Linux)
        run:  |
            cd "${{ github.workspace }}"
            tar -zxvf ${{ github.workspace }}/HDF-*-Linux.tar.gz --strip-components 1

      - name: set hdf4lib name
        id: set-hdf4lib-name
        run: |
          HDF4DIR=${{ github.workspace }}/HDF_Group/HDF/
          FILE_NAME_HDF4=$(ls ${{ github.workspace }}/HDF_Group/HDF)
          echo "HDF4_ROOT=$HDF4DIR$FILE_NAME_HDF4" >> $GITHUB_OUTPUT

      - name: List files for the binaries (Linux)
        run: |
            ls -l ${{ github.workspace }}/HDF_Group/HDF

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

      - name: Run ctest (Linux)
        env:
          HDF4_ROOT: ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}
        run: |
          cd "${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/share/HDF4Examples"
          cmake --workflow --preset=ci-StdShar-GNUC --fresh
        shell: bash

  test_binary_mac_latest:
  # MacOS w/ Clang + CMake
  #
    name: "MacOS Clang Binary Test"
    runs-on: macos-latest
    steps:
      - name: Install Dependencies (MacOS_latest)
        run: brew install ninja doxygen

      - name: Set up JDK 19
        uses: actions/setup-java@v4
        with:
          java-version: '19'
          distribution: 'temurin'

      - name: Get published binary (MacOS_latest)
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          name: tgz-macos14_clang-${{ inputs.build_mode }}-binary
          path: ${{ github.workspace }}

      - name: Uncompress hdf4 binary (MacOS_latest)
        run:  |
          cd "${{ github.workspace }}"
          tar -zxvf ${{ github.workspace }}/HDF-*-Darwin.tar.gz --strip-components 1

      - name: set hdf4lib name
        id: set-hdf4lib-name
        run: |
          HDF4DIR=${{ github.workspace }}/HDF_Group/HDF/
          FILE_NAME_HDF4=$(ls ${{ github.workspace }}/HDF_Group/HDF)
          echo "HDF4_ROOT=$HDF4DIR$FILE_NAME_HDF4" >> $GITHUB_OUTPUT

      - name: List files for the binaries (MacOS_latest)
        run: |
          ls -l ${{ github.workspace }}/HDF_Group/HDF

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

      # symlinks the compiler executables to a common location 
      - name: Setup GNU Fortran
        uses: fortran-lang/setup-fortran@v1
        id: setup-fortran
        with:
          compiler: gcc
          version: 14

      - name: Run ctest (MacOS_latest)
        id: run-ctest
        env:
          HDF4_ROOT: ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}
        run: |
          cd "${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/share/HDF4Examples"
          cmake --workflow --preset=ci-StdShar-macos-Clang --fresh
        shell: bash

