name: Packaging Tests
on:
  workflow_dispatch:  # allow manual triggering of this workflow
    inputs:
      outgoing_ref:
        description: "The ref to be built. Can be a tag, commit hash, or branch name"
        required: true
        default: "main"
      upload_to_pypi:
        description: "Try to upload wheels and sdist to PyPI after building"
        required: false
        default: "false"
      upload_to_anaconda:
        description: "Try to upload package to Anaconda after building"
        required: false
        default: "false"
  push:
    # Run on tags that look like releases
    tags:
      - v*
    # Run when main is pushed to
    branches:
      - main

jobs:
  build-packages:
    name: Trigger building packages from external repos
    runs-on: ubuntu-20.04
    steps:
      - name: Set up the job
        run: |
          if [ -n "${{ github.event.inputs.outgoing_ref }}" ]; then
            echo "REF=${{ github.event.inputs.outgoing_ref }}" >> $GITHUB_ENV
          else
            echo "REF=${{ github.ref }}" >> $GITHUB_ENV
          fi
          if [ -n "${{ github.event.inputs.upload_to_pypi }}" ]; then
            echo "UPLOAD_TO_PYPI=${{ github.event.inputs.upload_to_pypi }}" >> $GITHUB_ENV
          elif [[ "${{ github.ref }}" == refs/tags* ]]; then
            echo "UPLOAD_TO_PYPI=true" >> $GITHUB_ENV
          else
            echo "UPLOAD_TO_PYPI=false" >> $GITHUB_ENV
          fi
          if [ -n "${{ github.event.inputs.upload_to_anaconda }}" ]; then
            echo "UPLOAD_TO_ANACONDA=${{ github.event.inputs.upload_to_anaconda }}" >> $GITHUB_ENV
          elif [[ "${{ github.ref }}" == refs/tags* ]]; then
            echo "UPLOAD_TO_ANACONDA=true" >> $GITHUB_ENV
          else
            echo "UPLOAD_TO_ANACONDA=false" >> $GITHUB_ENV
          fi
      - name: Trigger PyPI/Wheel builds
        run: >
          gh workflow run -R cantera/pypi-packages
          python-package.yml
          -f incoming_ref=${{ env.REF }}
          -f upload=${{ env.UPLOAD_TO_PYPI }}
        env:
          GITHUB_TOKEN: ${{ secrets.PYPI_PACKAGE_PAT }}
      - name: Trigger Conda builds
        run: >
          gh workflow run -R cantera/conda-recipes
          main.yml
          -f incoming_ref=${{ env.REF }}
          -f upload=${{ env.UPLOAD_TO_ANACONDA }}
        env:
          GITHUB_TOKEN: ${{ secrets.CONDA_PACKAGE_PAT }}
      - name: Trigger Windows MSI Builds
        run: >
          gh workflow run -R cantera/cantera-windows-binaries
          main.yaml
          -f incoming_ref=${{ env.REF }}
        env:
          GITHUB_TOKEN: ${{ secrets.WINDOWS_MSI_PAT }}
      - name: Trigger macOS Package Builds
        run: >
          gh workflow run -R cantera/cantera-macos-pkg
          main.yml
          -f incoming_ref=${{ env.REF }}
        env:
          GITHUB_TOKEN: ${{ secrets.MACOS_PKG_PAT }}
