name: Build

on:
  push:
    tags:
      - v*
  workflow_dispatch:

jobs:
  build_macos:
    name: Build for MacOS
    runs-on: macos-13
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Setup Go
      uses: actions/setup-go@v4
      with:
        go-version: '^1.23'
    - name: Build arm64 gui
      uses: ./.github/actions/prepare
      with:
        id: darwin_arm64_gui
        cmd: make darwin_arm64_gui
        out: out/darwin_arm64_gui/yarr.app
    - name: Build amd64 gui
      uses: ./.github/actions/prepare
      with:
        id: darwin_amd64_gui
        cmd: make darwin_amd64_gui
        out: out/darwin_amd64_gui/yarr.app
    - name: Build arm64 cli
      uses: ./.github/actions/prepare
      with:
        id: darwin_arm64
        cmd: make darwin_arm64
        out: out/darwin_arm64/yarr
    - name: Build amd64 cli
      uses: ./.github/actions/prepare
      with:
        id: darwin_amd64
        cmd: make darwin_amd64
        out: out/darwin_amd64/yarr

  build_windows:
    name: Build for Windows
    runs-on: windows-2022
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Setup Go
      uses: actions/setup-go@v4
      with:
        go-version: '^1.23'
    - name: Build amd64 gui
      uses: ./.github/actions/prepare
      with:
        id: windows_amd64_gui
        cmd: make windows_amd64_gui
        out: out/windows_amd64_gui/yarr.exe
    - name: Build arm64 gui
      if: false
      uses: ./.github/actions/prepare
      with:
        id: windows_arm64_gui
        cmd: make windows_arm64_gui
        out: out/windows_arm64_gui/yarr.exe

  build_multi_cli:
    name: Build for Windows/MacOS/Linux CLI
    runs-on: ubuntu-22.04
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Setup Go
      uses: actions/setup-go@v4
      with:
        go-version: '^1.23'
    - name: Setup Zig
      uses: mlugg/setup-zig@v1
      with:
        version: 0.14.0
    - name: Build linux/amd64
      uses: ./.github/actions/prepare
      with:
        id: linux_amd64
        cmd: make linux_amd64
        out: out/linux_amd64/yarr
    - name: Build linux/arm64
      uses: ./.github/actions/prepare
      with:
        id: linux_arm64
        cmd: make linux_arm64
        out: out/linux_arm64/yarr
    - name: Build linux/armv7
      uses: ./.github/actions/prepare
      with:
        id: linux_armv7
        cmd: make linux_armv7
        out: out/linux_armv7/yarr
    - name: Build windows/amd64
      uses: ./.github/actions/prepare
      with:
        id: windows_amd64
        cmd: make windows_amd64
        out: out/windows_amd64/yarr
    - name: Build windows/arm64
      uses: ./.github/actions/prepare
      with:
        id: windows_arm64
        cmd: make windows_arm64
        out: out/windows_arm64/yarr

  create_release:
    name: Create Release
    runs-on: ubuntu-latest
    needs: [build_macos, build_windows, build_multi_cli]
    steps:
    - name: Download Artifacts
      uses: actions/download-artifact@v4.1.7
      with:
        path: .
    - name: Preparation
      run: |
        set -ex
        ls -R
        for tarfile in `ls **/*.tar`; do
          tar -xvf $tarfile
        done
        for dir in out/*; do
          echo "Compressing: $dir"
          (test -d "$dir" && cd $dir && zip -r ../yarr_`basename $dir`.zip *)
        done
        ls out
    - name: Upload Release
      uses: softprops/action-gh-release@v2
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          draft: true
          prerelease: true
          files: |
            out/*.zip
