---
name: releases

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

jobs:
  build:
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        # build targets
        include:
          - goos: linux
            goarch: amd64
          - goos: linux
            goarch: '386'
          - goos: darwin
            goarch: amd64
          - goos: darwin
            goarch: arm64
    steps:
      - name: Checkout source code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: 1.23

      - name: Check if the version string was updated
        run: |
          VERSION=$(echo ${GITHUB_REF} | awk -F/ '{print substr($3,2,10);}')

          if ! grep "var version string = \"$VERSION\"" main.go; then
            echo "The version string inside main.go should be updated to $VERSION"
            exit 1
          fi

      - name: Build for ${{ matrix.goos }}-${{ matrix.goarch }}
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
        run: |
          go build -x -v -mod=vendor -o "tty-share_${GOOS}-${GOARCH}"

      - name: Release
        uses: elisescu/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          files: tty-share_*

      # - name: Debug with tty-share
      #   if: ${{ matrix.goarch == 'arm64' }}
      #   run: |
      #     curl https://raw.githubusercontent.com/elisescu/tty-share/refs/heads/master/scripts/get.sh | sh
      #     chmod u+x ./tty-share
      #     export TERM=xterm-256color
      #     ./tty-share -A --public --headless --headless-cols 255 --headless-rows 50 --no-wait --listen :8001


