name: Agent Release

on:
    push:
        tags:
            - "agent/v*"

jobs:
    build:
        runs-on: ubuntu-docker
        container: node:26
        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Set up Go
              uses: actions/setup-go@v5
              with:
                  go-version: "1.26"
                  cache: true
                  cache-dependency-path: agent/go.sum

            - name: Extract version
              id: version
              run: echo "VERSION=${GITHUB_REF_NAME#agent/}" >> $GITHUB_OUTPUT

            - name: Build
              working-directory: agent
              env:
                  VERSION: ${{ steps.version.outputs.VERSION }}
              run: |
                  mkdir -p dist
                  GOOS=linux GOARCH=amd64 go build \
                    -ldflags="-s -w -X main.Version=${VERSION}" \
                    -o dist/vantage-agent-linux-amd64 ./cmd
                  GOOS=linux GOARCH=arm64 go build \
                    -ldflags="-s -w -X main.Version=${VERSION}" \
                    -o dist/vantage-agent-linux-arm64 ./cmd
                  GOOS=windows GOARCH=amd64 go build \
                    -ldflags="-s -w -X main.Version=${VERSION}" \
                    -o dist/vantage-agent-windows-amd64.exe ./cmd

            - name: Checksums
              working-directory: agent/dist
              run: sha256sum vantage-agent-linux-amd64 vantage-agent-linux-arm64 vantage-agent-windows-amd64.exe > checksums.txt

            - name: Create release
              uses: https://gitea.com/actions/gitea-release-action@v1
              with:
                  token: ${{ secrets.RELEASE_TOKEN }}
                  files: |
                      agent/dist/vantage-agent-linux-amd64
                      agent/dist/vantage-agent-linux-arm64
                      agent/dist/vantage-agent-windows-amd64.exe
                      agent/dist/checksums.txt

    msi:
        needs: build
        runs-on: windows-2022
        steps:
            - name: Checkout
              uses: actions/checkout@v4

            - name: Set up Go
              uses: actions/setup-go@v5
              with:
                  go-version: "1.26"
                  cache: true
                  cache-dependency-path: agent/go.sum

            - name: Extract version
              id: version
              shell: pwsh
              run: |
                  $v = "${{ github.ref_name }}" -replace '^agent/v', ''
                  "VERSION=$v" | Out-File -Append $env:GITHUB_OUTPUT
                  # MSI ProductVersion must be numeric x.x.x.x
                  "MSIVERSION=$v.0" | Out-File -Append $env:GITHUB_OUTPUT

            - name: Build agent exe
              working-directory: agent
              shell: pwsh
              env:
                  VERSION: ${{ steps.version.outputs.VERSION }}
              run: |
                  $env:GOOS = "windows"; $env:GOARCH = "amd64"
                  go build -ldflags="-s -w -X main.Version=$env:VERSION" -o ../installer/vantage-agent-windows-amd64.exe ./cmd

            - name: Install WiX
              shell: pwsh
              run: dotnet tool install --global wix --version 5.*

            - name: Build MSI
              working-directory: installer
              shell: pwsh
              run: |
                  $env:PATH = "$env:PATH;$env:USERPROFILE\.dotnet\tools"
                  wix build vantage-agent.wxs -d Version=${{ steps.version.outputs.MSIVERSION }} -o vantage-agent.msi
                  (Get-FileHash vantage-agent.msi -Algorithm SHA256).Hash.ToLower() + "  vantage-agent.msi" | Out-File -Encoding ascii checksums-msi.txt

            - name: Attach MSI to release
              working-directory: installer
              shell: pwsh
              env:
                  TOKEN: ${{ secrets.RELEASE_TOKEN }}
              run: |
                  $api = "${{ github.server_url }}/api/v1/repos/${{ github.repository }}"
                  $tag = [uri]::EscapeDataString("${{ github.ref_name }}")
                  $headers = @{ Authorization = "token $env:TOKEN" }
                  # gitea-release-action can't find a slashed tag, so append via the API directly
                  $rel = Invoke-RestMethod -Headers $headers -Uri "$api/releases/tags/$tag"
                  foreach ($f in "vantage-agent.msi", "checksums-msi.txt") {
                      $name = [uri]::EscapeDataString($f)
                      Invoke-RestMethod -Headers $headers -Method Post -InFile $f `
                        -ContentType "application/octet-stream" `
                        -Uri "$api/releases/$($rel.id)/assets?name=$name"
                  }
