name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

concurrency:
  group: release-${{ github.ref_name }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        arch: [amd64, arm64]

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version-file: ./go.mod

      - name: Run tests
        run: go test -v ./...

      - name: Build dankinstall (${{ matrix.arch }})
        env:
          GOOS: linux
          CGO_ENABLED: 0
          GOARCH: ${{ matrix.arch }}
        run: |
          set -eux
          cd cmd/dankinstall
          go build -trimpath -ldflags "-s -w -X main.Version=${GITHUB_REF#refs/tags/}" \
            -o ../../dankinstall-${{ matrix.arch }}
          cd ../..
          gzip -9 -k dankinstall-${{ matrix.arch }}
          sha256sum dankinstall-${{ matrix.arch }}.gz > dankinstall-${{ matrix.arch }}.gz.sha256

      - name: Build dms (${{ matrix.arch }})
        env:
          GOOS: linux
          CGO_ENABLED: 0
          GOARCH: ${{ matrix.arch }}
        run: |
          set -eux
          cd cmd/dms
          go build -trimpath -ldflags "-s -w -X main.Version=${GITHUB_REF#refs/tags/}" \
            -o ../../dms-${{ matrix.arch }}
          cd ../..
          gzip -9 -k dms-${{ matrix.arch }}
          sha256sum dms-${{ matrix.arch }}.gz > dms-${{ matrix.arch }}.gz.sha256

      - name: Build dms-distropkg (${{ matrix.arch }})
        env:
          GOOS: linux
          CGO_ENABLED: 0
          GOARCH: ${{ matrix.arch }}
        run: |
          set -eux
          cd cmd/dms
          go build -trimpath -tags distro_binary -ldflags "-s -w -X main.Version=${GITHUB_REF#refs/tags/}" \
            -o ../../dms-distropkg-${{ matrix.arch }}
          cd ../..
          gzip -9 -k dms-distropkg-${{ matrix.arch }}
          sha256sum dms-distropkg-${{ matrix.arch }}.gz > dms-distropkg-${{ matrix.arch }}.gz.sha256

      - name: Upload artifacts (${{ matrix.arch }})
        uses: actions/upload-artifact@v4
        with:
          name: release-assets-${{ matrix.arch }}
          path: |
            dankinstall-${{ matrix.arch }}.gz
            dankinstall-${{ matrix.arch }}.gz.sha256
            dms-${{ matrix.arch }}.gz
            dms-${{ matrix.arch }}.gz.sha256
            dms-distropkg-${{ matrix.arch }}.gz
            dms-distropkg-${{ matrix.arch }}.gz.sha256
          if-no-files-found: error

  update-flake-version:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0

      - name: Update flake.nix version
        run: |
          set -euo pipefail

          version="${GITHUB_REF#refs/tags/}"
          version="${version#v}"
          echo "Updating flake.nix to version: $version"

          # Update version in flake.nix
          sed -i "s/version = \"[^\"]*\"/version = \"$version\"/" flake.nix

          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

          if ! git diff --quiet flake.nix; then
            git add flake.nix
            git commit -m "flake: bump version to $version"

            # Push to master (or main, depending on your default branch)
            git push origin HEAD:master || git push origin HEAD:main
            echo "Pushed flake.nix version update to master"
          else
            echo "No version changes needed"
          fi

  release:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: release-assets-*
          merge-multiple: true
          path: ./_dist

      - name: Create/Update GitHub Release (single run)
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: Release ${{ github.ref_name }}
          body: |
            ## Danklinux Release

            This release includes binaries for:
            - Linux AMD64
            - Linux ARM64

            ### Installation

            ```bash
            curl -fsSL https://raw.githubusercontent.com/AvengeMedia/danklinux/master/install.sh | sh
            ```
          files: _dist/**
          draft: false
          prerelease: ${{ contains(github.ref_name, '-') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Notify DankMaterialShell to create release and mirror assets (single run)
        uses: peter-evans/repository-dispatch@v3
        with:
          token: ${{ secrets.SHELL_REPO_PAT }}
          repository: AvengeMedia/DankMaterialShell
          event-type: dms_release
          client-payload: >-
            {
              "tag": "${{ github.ref_name }}",
              "dms_repo": "${{ github.repository }}"
            }
