name: extra-package

on:
  workflow_dispatch:
    inputs:
      set-release-version:
        description: 'Desired release version (x.y.z)'
        required: true
        type: string
      push-to-chocolatey:
        description: 'Push to chocolatey'
        type: boolean
        default: false
      push-to-winget:
        description: 'Push to winget'
        type: boolean
        default: false
      package-generic-linux-aarch64:
        description: 'Create generic linux aarch64 package'
        type: boolean
        default: false

concurrency: extra-package

jobs:
  check-release:
    env:
      GH_TOKEN: ${{ github.token }}
      RELEASE_VERSION: ${{ github.event.inputs.set-release-version }}
    name: Check-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.1.7
        with:
          fetch-depth: 1
          ref: ${{ github.event.inputs.set-release-version }}
      - name: Check if branch is a published release
        run: |
          if gh release list --exclude-drafts --exclude-pre-releases | grep -E "^${RELEASE_VERSION}" ; then
              echo "✅ Desired release ${RELEASE_VERSION} is a published release"
          else
              echo "❌ You have to run this workflow for a published release (excluding draft and pre-release), but the desired one [${RELEASE_VERSION}] is not."
              exit 1
          fi

  push-to-chocolatey:
    if: github.event.inputs.push-to-chocolatey == 'true'
    needs: check-release
    env:
      RELEASE_VERSION: ${{ github.event.inputs.set-release-version }}
      CHOCOLATEY_TOKEN: ${{ secrets.LEPAPAREIL_CHOCOLATEY_TOKEN }}
    name: Push to chocolatey
    runs-on: windows-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.1.7
        with:
          fetch-depth: 1
          ref: ${{ github.event.inputs.set-release-version }}
      - name: Push to chocolatey
        run: .\bin\release\push_package_to_chocolatey.ps1 $env:RELEASE_VERSION $env:CHOCOLATEY_TOKEN

  push-to-winget:
    if: github.event.inputs.push-to-winget == 'true'
    needs: check-release
    env:
      RELEASE_VERSION: ${{ github.event.inputs.set-release-version }}
      WINGET_TOKEN: ${{ secrets.LEPAPAREIL_WINGET_TOKEN }}
    name: Push to winget
    runs-on: windows-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.1.7
        with:
          fetch-depth: 1
          ref: ${{ github.event.inputs.set-release-version }}
      - name: Push to winget
        run: |
          Invoke-WebRequest https://aka.ms/wingetcreate/latest -OutFile .\wingetcreate.exe
          .\wingetcreate.exe version
          .\bin\release\push_package_to_winget.ps1 "$env:RELEASE_VERSION" "$env:LEPAPAREIL_WINGET_TOKEN"

  package-generic-linux-aarch64:
    if: github.event.inputs.package-generic-linux-aarch64 == 'true'
    needs: check-release
    name: Create generic linux aarch64 package
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4.1.7
        with:
          fetch-depth: 1
          ref: ${{ github.event.inputs.set-release-version }}
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3.2.0
      - name: Create package
        uses: addnab/docker-run-action@v3
        with:
          image: ubuntu:22.04
          options: --platform linux/arm64/v8 --volume ${{ github.workspace }}:/work --workdir /work --privileged --env CARGO_TERM_COLOR=always --env CARGO_BUILD_TARGET=aarch64-unknown-linux-gnu
          run: |
            set -e
            echo "::group::Install Prerequisites"
              export DEBIAN_FRONTEND=noninteractive
              bin/install_prerequisites_ubuntu.sh
              sudo apt-get install -y g++-aarch64-linux-gnu libc6-dev-arm64-cross
              ./bin/export_cross_compile_env.sh
            echo "::endgroup::"
            echo "::group::Install Rust"
              bin/install_rust.sh
              . "$HOME/.cargo/env"
            echo "::endgroup::"
            echo "::group::Environment"
              bin/environment.sh
            echo "::endgroup::"
            echo "::group::Build"
              ./bin/release/release.sh
              cp -frp target/$CARGO_BUILD_TARGET/release/hurl ./target/release/
              cp -frp target/$CARGO_BUILD_TARGET/release/hurlfmt ./target/release/
            echo "::endgroup::"
            echo "::group::Get version"
              export VERSION=$(bin/release/get_version.sh)
            echo "::endgroup::"
            echo "::group::Create man"
              bin/release/man.sh
            echo "::endgroup::"
            echo "::group::Create tarball"
              bin/release/create_tarball.sh
              bin/release/sha256sum.sh --write target/upload/hurl-$VERSION-aarch64-unknown-linux-gnu.tar.gz
            echo "::endgroup::"
      - name: Archive production artifacts
        uses: actions/upload-artifact@v4.3.6
        with:
          name: release-generic-linux-aarch64-artifacts
          path: target/upload/*
