name: CI
on:
  push:
  workflow_dispatch:

jobs:

  test:
    strategy:
      fail-fast: false
      matrix:
        os: [ ubuntu-latest-m, macos-latest-xl, windows-latest-l ]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"
          check-latest: true
      - name: Get go version
        id: go-version
        run: echo "name=version::$(go env GOVERSION)" >> $GITHUB_OUTPUT
      - name: go mod download
        run: go mod download
      - name: go mod verify
        run: go mod verify
      - name: generate command strings
        run: go generate ./... && git diff --exit-code
      - name: "Place wintun.dll"
        run: cp deps/wintun/bin/amd64/wintun.dll ./
      - name: Run tests
        run: make test

  release:
    needs: test
    if: startsWith(github.ref, 'refs/tags/v0.') && !contains(github.ref, 'dev-')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"
          check-latest: true
      - name: Get go version
        id: go-version
        run: echo "name=version::$(go env GOVERSION)" >> $GITHUB_OUTPUT
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Place wintun.dll
        run: cp -r deps/wintun/bin/amd64/wintun.dll ./
      - name: generate release notes
        run: |
          mkdir -p ./tmp
          ./scripts/changelog.sh > ./tmp/changelog.txt
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v5
        with:
          version: latest
          args: release --clean --release-notes=./tmp/changelog.txt
        env:
          BUILD_ENV: "production"
          GITHUB_TOKEN: ${{ secrets.GORELEASER_GITHUB_TOKEN }}
      - name: Upload checksums as artifact
        uses: actions/upload-artifact@v4
        with:
          name: checksums
          path: dist/checksums.txt
          overwrite: true

  sync_docs:
    needs: release
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v0.') && !contains(github.ref, 'pre') && !contains(github.ref, 'dev-')
    steps:
      - name: Checkout flyctl
        uses: actions/checkout@v4
      - uses: jnwng/github-app-installation-token-action@778da2ab509f3ef5e4b902bc15daf25ef3e34939
        id: installationToken
        with:
          appId: 339363
          installationId: 37947271
          privateKey: ${{ secrets.DOCS_SYNCER_GITHUB_APP_PRIVATE_KEY }}
      - name: Checkout docs
        uses: actions/checkout@v4
        with:
          repository: superfly/docs
          token: ${{ steps.installationToken.outputs.token }}
          path: docs
      - uses: actions/setup-go@v5
        with:
          go-version-file: "go.mod"
          check-latest: true
      - name: Publish CLI docs
        id: publish-cli-docs
        env:
          GITHUB_TOKEN: ${{ steps.installationToken.outputs.token }}
        run: scripts/publish_docs.sh ${{ github.ref_name }}

  aur-publish:
    name: Build & publish to AUR
    needs: release
    if: startsWith(github.ref, 'refs/tags/v0.') && !contains(github.ref, 'pre') && !contains(github.ref, 'dev-')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Download checksums
        uses: actions/download-artifact@v4
        with:
          name: checksums
      - name: Generate PKGBUILD
        run: |
          mkdir -p aur/flyctl-bin
          export tag=${GITHUB_REF#refs/*/}
          export version=${tag:1}
          export sha256sum=$(grep "Linux_x86_64.tar.gz" checksums.txt | cut -d ' ' -f 1)
          envsubst '${version},${sha256sum}' < aur/PKGBUILD > aur/flyctl-bin/PKGBUILD
          cat aur/flyctl-bin/PKGBUILD
      - name: Publish flyctl-bin to the AUR
        uses: superfly/aur-releaser@ba29a0a809d7221713a104f9c9a23c34ee5b0742
        with:
          pkgname: flyctl-bin
          pkgbuild: ./aur/flyctl-bin/PKGBUILD
          commit_username: jeromegn
          commit_email: jeromegn@gmail.com
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: ${{ github.event.ref }}
          force_push: "true"
