name: Release

on:
  release:
    types: [created]
  workflow_dispatch:

permissions:
  contents: write

jobs:
  prepare-source:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version: '^1.24.0'

      - name: Prepare source tarball
        run: |
          git clone -b ${{ github.event.release.name }} --depth 1 https://github.com/schollz/croc croc-${{ github.event.release.name }}
          cd croc-${{ github.event.release.name }} && go mod tidy && go mod vendor
          cd .. && tar -czvf croc_${{ github.event.release.name }}_src.tar.gz croc-${{ github.event.release.name }}

      - name: Upload source artifact
        uses: actions/upload-artifact@v7
        with:
          name: source-tarball
          path: "*.tar.gz"

  build:
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        include:
          # Windows builds
          - goos: windows
            goarch: amd64
            name: Windows-64bit
            ext: .exe
            archive: zip
          - goos: windows
            goarch: "386"
            name: Windows-32bit
            ext: .exe
            archive: zip
          - goos: windows
            goarch: arm
            name: Windows-ARM
            ext: .exe
            archive: zip
          - goos: windows
            goarch: arm64
            name: Windows-ARM64
            ext: .exe
            archive: zip
          
          # Linux builds
          - goos: linux
            goarch: amd64
            name: Linux-64bit
            ext: ""
            archive: tar.gz
          - goos: linux
            goarch: "386"
            name: Linux-32bit
            ext: ""
            archive: tar.gz
          - goos: linux
            goarch: arm
            name: Linux-ARM
            ext: ""
            archive: tar.gz
          - goos: linux
            goarch: arm
            goarm: "5"
            name: Linux-ARMv5
            ext: ""
            archive: tar.gz
          - goos: linux
            goarch: arm64
            name: Linux-ARM64
            ext: ""
            archive: tar.gz
          - goos: linux
            goarch: riscv64
            name: Linux-RISCV64
            ext: ""
            archive: tar.gz
          
          # macOS builds
          - goos: darwin
            goarch: amd64
            name: macOS-64bit
            ext: ""
            archive: tar.gz
          - goos: darwin
            goarch: arm64
            name: macOS-ARM64
            ext: ""
            archive: tar.gz
          
          # BSD builds
          - goos: dragonfly
            goarch: amd64
            name: DragonFlyBSD-64bit
            ext: ""
            archive: tar.gz
          - goos: freebsd
            goarch: amd64
            name: FreeBSD-64bit
            ext: ""
            archive: tar.gz
          - goos: freebsd
            goarch: arm64
            name: FreeBSD-ARM64
            ext: ""
            archive: tar.gz
          - goos: netbsd
            goarch: "386"
            name: NetBSD-32bit
            ext: ""
            archive: tar.gz
          - goos: netbsd
            goarch: amd64
            name: NetBSD-64bit
            ext: ""
            archive: tar.gz
          - goos: netbsd
            goarch: arm64
            name: NetBSD-ARM64
            ext: ""
            archive: tar.gz
          - goos: openbsd
            goarch: amd64
            name: OpenBSD-64bit
            ext: ""
            archive: tar.gz
          - goos: openbsd
            goarch: arm64
            name: OpenBSD-ARM64
            ext: ""
            archive: tar.gz

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Setup Go
        uses: actions/setup-go@v6
        with:
          go-version: '^1.24.0'

      - name: Build binary
        env:
          CGO_ENABLED: 0
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
          GOARM: ${{ matrix.goarm }}
        run: |
          # Set LDFLAGS based on platform
          case "${{ matrix.goos }}" in
            "darwin")
              LDFLAGS='-s -extldflags "-sectcreate __TEXT __info_plist Info.plist"'
              ;;
            "dragonfly"|"freebsd"|"netbsd"|"openbsd")
              LDFLAGS=""
              ;;
            *)
              LDFLAGS='-extldflags "-static"'
              ;;
          esac
          
          echo "Building for ${{ matrix.goos }}/${{ matrix.goarch }} with LDFLAGS: $LDFLAGS"
          go build -ldflags "$LDFLAGS" -o croc${{ matrix.ext }}

      - name: Create archive
        run: |
          if [ "${{ matrix.archive }}" = "zip" ]; then
            zip croc_${{ github.event.release.name }}_${{ matrix.name }}.zip croc${{ matrix.ext }} LICENSE
          else
            tar -czvf croc_${{ github.event.release.name }}_${{ matrix.name }}.tar.gz croc${{ matrix.ext }} LICENSE
          fi

      - name: Upload build artifact
        uses: actions/upload-artifact@v7
        with:
          name: build-${{ matrix.name }}
          path: |
            *.zip
            *.tar.gz

  release:
    needs: [prepare-source, build]
    runs-on: ubuntu-24.04
    if: github.event_name == 'release'
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v8
        with:
          merge-multiple: true

      - name: Generate checksums
        run: |
          # Generate SHA256 checksums for all archives
          sha256sum *.zip *.tar.gz > croc_${{ github.event.release.name }}_checksums.txt
          
          # Display the checksums file for verification
          echo "Generated checksums:"
          cat croc_${{ github.event.release.name }}_checksums.txt

      - name: Upload release assets
        uses: softprops/action-gh-release@v2
        with:
          files: |
            *.zip
            *.tar.gz
            *_checksums.txt
