name: Build KAMOS installer ISO

on:
  workflow_dispatch:
    inputs:
      variant:
        description: "Which variant to build an ISO for"
        required: true
        default: "kamos"
        type: choice
        options:
          - kamos          # AMD / Intel graphics
          - kamos-nvidia   # NVIDIA graphics

env:
  REGISTRY_HOST: fcs.tgsad.ae

jobs:
  build-iso:
    name: ISO for ${{ inputs.variant }}
    runs-on: ubuntu-latest

    steps:
      - name: Set image ref
        run: |
          echo "OWNER=${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_ENV"
          echo "DATE_TAG=$(date +%Y%m%d)" >> "$GITHUB_ENV"

      - name: Build ISO with bootc-image-builder
        # Runs as a sibling container on the host Docker daemon (needs the
        # kamos package set to PUBLIC in Forgejo so it can pull the image).
        run: |
          docker volume rm -f kamos-bib-output 2>/dev/null || true
          docker volume create kamos-bib-output
          docker run --rm --privileged \
            -v kamos-bib-output:/output \
            quay.io/centos-bootc/bootc-image-builder:latest \
            --type anaconda-iso \
            "${REGISTRY_HOST}/${OWNER}/${{ inputs.variant }}:stable"

      - name: Extract ISO from volume
        run: |
          docker create --name kamos-bib-extract -v kamos-bib-output:/output alpine
          docker cp "kamos-bib-extract:/output/bootiso/install.iso" \
            "./${{ inputs.variant }}-${DATE_TAG}.iso"
          docker rm kamos-bib-extract
          docker volume rm kamos-bib-output

      - name: Upload ISO to Forgejo package registry
        run: |
          ISO="${{ inputs.variant }}-${DATE_TAG}.iso"
          curl --fail -u "${{ secrets.REGISTRY_USER }}:${{ secrets.REGISTRY_TOKEN }}" \
            --upload-file "$ISO" \
            "https://${REGISTRY_HOST}/api/packages/${OWNER}/generic/kamos-iso/${DATE_TAG}/${ISO}"
          echo "ISO available at: https://${REGISTRY_HOST}/${OWNER}/-/packages/generic/kamos-iso/${DATE_TAG}"
