name: "Release RPM"

on:
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag to release'
        required: true
      arch:
        type: choice
        description: 'Architecture to build'
        required: true
        default: all
        options:
          - all
          - x86_64
          - arm64
      gh-release:
        type: boolean
        description: 'Update GitHub release'
        required: true
        default: true

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
    - name: Check tag exists
      uses: mukunku/tag-exists-action@v1.6.0
      id: check-tag
      with:
        tag: ${{ github.event.inputs.tag }}
    - name: Exit if tag does not exist
      run: |
        if [[ "${{ steps.check-tag.outputs.exists }}" == "false" ]]; then
          echo "Tag ${{ github.event.inputs.tag }} does not exist"
          exit 1
        fi

  setup-matrix:
    needs:
    - check
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: Set up matrix
        id: set-matrix
        run: |
          if [[ "${{ github.event.inputs.arch }}" == "all" ]]; then
            echo "matrix=[\"ubuntu-latest\", \"arm64\"]" >> $GITHUB_OUTPUT
          elif [[ "${{ github.event.inputs.arch }}" == "x86_64" ]]; then
            echo "matrix=[\"ubuntu-latest\"]" >> $GITHUB_OUTPUT
          else
            echo "matrix=[\"arm64\"]" >> $GITHUB_OUTPUT
          fi

  build:
    needs:
    - setup-matrix
    strategy:
      matrix:
        os: ${{fromJson(needs.setup-matrix.outputs.matrix)}}
    runs-on: ${{ matrix.os }}
    steps:
    - name: Prepare workspace
      run: rm -rf build-rpm && mkdir build-rpm
    - name: Download ${{ github.event.inputs.tag }} source code
      uses: robinraju/release-downloader@v1.9
      with:
        token: ${{ secrets.GH_PAT }}
        tag: ${{ github.event.inputs.tag }}
        fileName: globalprotect-openconnect-*.tar.gz
        tarBall: false
        zipBall: false
        out-file-path: build-rpm
    - name: Docker Login
      run: echo ${{ secrets.DOCKER_HUB_TOKEN }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin
    - name: Build RPM package in Docker
      run: |
        docker run --rm -v $(pwd)/build-rpm:/rpm -e INCLUDE_GUI=1 yuezk/gpdev:rpm-builder
    - name: Install RPM package in Docker
      run: |
        docker run --rm -v $(pwd)/build-rpm:/rpm yuezk/gpdev:rpm-builder \
          bash -c "sudo rpm -i /rpm/*.$(uname -m).rpm; gpclient -V; gpservice -V; gpauth -V; gpgui-helper -V; gpgui -V;"
    - name: Upload RPM package
      uses: actions/upload-artifact@v3
      with:
        name: artifact-rpm-${{ matrix.os }}
        if-no-files-found: error
        path: |
          build-rpm/*.rpm

  gh-release:
    needs:
    - build
    runs-on: ubuntu-latest
    if: ${{ github.event.inputs.gh-release == 'true' }}
    steps:
    - name: Prepare workspace
      run: rm -rf gh-release && mkdir gh-release
    - name: Download artifact
      uses: actions/download-artifact@v3
      with:
        path: gh-release
    - name: Update release
      uses: softprops/action-gh-release@v1
      with:
        token: ${{ secrets.GH_PAT }}
        prerelease: ${{ contains(github.ref, 'latest') }}
        fail_on_unmatched_files: true
        tag_name: ${{ github.event.inputs.tag }}
        files: |
          gh-release/artifact-*/*
