name: Publish Draft Release

on:
  push:

    # only publish draft for x.x.x tags
    tags:
#      - '**' # for testing
      - "[0-9]+.[0-9]+.[0-9]+"

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:

  build:
    name: Package ${{ github.ref_name }} release
    defaults:
      run:
        shell: bash -l {0}
    runs-on: 'ubuntu-latest'
    strategy:
      fail-fast: false

    steps:
    - uses: actions/checkout@v4
    - uses: mamba-org/setup-micromamba@v2
      with:
        init-shell: bash
        environment-file: "scripts/ci/current-environment.yml"
        environment-name: "pdal-build"
        cache-environment: true
        cache-downloads: true

    - name: Setup
      run: |
          source ./scripts/ci/linux/setup.sh

    - name: CMake
      run: |
          source ../scripts/ci/linux/cmake.sh
      working-directory: ./build

    - name: Generate source distribution
      shell: bash -l {0}
      run: |
         ninja dist
         extensions=".tar.gz .tar.bz2"
         for ext in $extensions
         do
             for filename in $(ls *$ext)
             do

                 `md5sum $filename > $filename.md5`
                 `sha256sum $filename > $filename.sha256sum`
                 `sha512sum $filename > $filename.sha512sum`
             done
         done
      working-directory: ./build

    - uses: actions/upload-artifact@v4
      name: Gather source distribution artifact
      with:
        name: source-package-ubuntu-latest
        if-no-files-found: error
        path: |
         ./build/PDAL*

  test:
    name: Test ${{ github.ref_name }} release
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: write
    steps:
     - uses: actions/download-artifact@v4
       name: Download release artifact
       with:
         name: source-package-ubuntu-latest
         path: release

     - name: Verify checksums
       shell: bash -l {0}
       working-directory: ./release
       run: |
          set -e
          sha256sum -c *.sha256sum


     - name: Unzip
       shell: bash -l {0}
       working-directory: ./release
       run: |
          set -e
          echo "Tagged version is $GITHUB_REF_NAME, checking that PDAL version is the same"
          ls *
          tar xvf PDAL-${{ github.ref_name }}-src.tar.bz2
          cd PDAL-$GITHUB_REF_NAME-src

     - uses: mamba-org/setup-micromamba@v2
       name: Install mamba
       with:
         init-shell: bash
         environment-file: "./release/PDAL-${{ github.ref_name }}-src/scripts/ci/current-environment.yml"
         environment-name: "pdal-build"
         cache-environment: true
         cache-downloads: true

     - name: Confirm release
       shell: bash -l {0}
       working-directory: ./release/PDAL-${{ github.ref_name }}-src
       run: |
          set -e
          source ./scripts/ci/linux/setup.sh
          cd build
          source ../scripts/ci/linux/cmake.sh
          cmake .. \
            -G Ninja \
            -DCMAKE_LIBRARY_PATH:FILEPATH="$CONDA_PREFIX/lib" \
            -DCMAKE_INCLUDE_PATH:FILEPATH="$CONDA_PREFIX/include" \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX=${CONDA_PREFIX} \
            -DWITH_TESTS=OFF
          source ../scripts/ci/linux/compile.sh
          export PDAL_VERSION=$(pdal-config --version)
          if [ $GITHUB_REF_NAME == $PDAL_VERSION ]; then
            echo "Tagged version equals tree version. All good."
          else
            echo "Tagged version is $GITHUB_REF_NAME and PDAL version is $PDAL_VERSION. Failing"
            exit 1;
          fi

  release:
    name: Gather ${{ github.ref_name }} release
    runs-on: ubuntu-latest
    needs: test
    permissions:
      contents: write
    steps:
     - uses: actions/download-artifact@v4
       name: Download release artifact
       with:
         name: source-package-ubuntu-latest
         path: release

     - uses: softprops/action-gh-release@v2
       if: startsWith(github.ref, 'refs/tags/')
       name: Publish release as draft
       with:
         make_latest: false
         fail_on_unmatched_files: true
         prerelease: true
         generate_release_notes: true
         draft: true
         files: |
          release/PDAL-*

