name: Build & Publish Binaries

on:
  workflow_dispatch:
  push:
    tags:
      - v[0-9]+.[0-9]+.[0-9]+
  schedule:
    - cron: '0 13-21/2 * * *' # every 2 hours during US Eastern daytime

jobs:
  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Use Latest Corepack
        run: |
          npm install -g corepack@latest
          corepack enable && corepack enable pnpm
          pnpm --version

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22.13.0
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install

      - name: Build server
        run: pnpm turbo typecheck --filter=@tunarr/server

      - name: Build web
        run: pnpm turbo typecheck --filter=@tunarr/web
  build:
    needs: typecheck
    strategy:
      matrix:
        os:
          - name: ubuntu-latest
            target: linux-x64
          - name: ubuntu-22.04-arm
            target: linux-arm64
          - name: macos-13
            target: macos-x64
          - name: macos-14
            target: macos-arm64
          - name: windows-latest
            target: win-x64
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4 # Checkout code from repository

      - uses: ilammy/setup-nasm@v1

      - name: Use Latest Corepack
        run: |
          npm install -g corepack@latest
          corepack enable && corepack enable pnpm
          pnpm --version

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22.13.0
          cache: 'pnpm'

      - name: Install dependencies
        run: pnpm install
      
      - name: 'Create env file (release)'
        run: |
          touch .env
          echo TUNARR_EDGE_BUILD=false >> .env
          echo NODE_ENV=production >> .env
          echo TUNARR_BIND_ADDR=0.0.0.0 >> .env
          cat .env
          cp .env server/.env
          cp .env web/.env
        if: startsWith(github.ref, 'refs/tags/')

      - name: 'Create env file (edge)'
        run: |
          touch .env
          echo TUNARR_BUILD="$(git rev-parse --short ${{ github.sha }})" >> .env
          echo TUNARR_EDGE_BUILD=true >> .env
          echo NODE_ENV=production >> .env
          echo TUNARR_BIND_ADDR=0.0.0.0 >> .env
          cat .env
          cp .env server/.env
          cp .env web/.env
        if: github.ref == format('refs/heads/{0}', 'dev') && (github.event_name == 'workflow_dispatch' || github.event_name == 'schedule')

      - name: Bundle web
        run: pnpm turbo bundle --filter=@tunarr/web

      # Install ldid for adhoc signing of macos ARM binaries. PKG should take
      # care of this.
      - name: Install ldid
        uses: MOZGIII/install-ldid-action@v1
        with:
          tag: v2.1.5-procursus2
        if: matrix.os.target == 'macos-arm64'

      - name: Build Executable
        run: pnpm turbo make-bin -- --target ${{matrix.os.target}}

      - name: Upload Edge Binary
        uses: softprops/action-gh-release@v2
        with:
          name: Tunarr Edge
          tag_name: edge
          prerelease: true
          files: |
            ./server/bin/tunarr-*
        if: github.ref == format('refs/heads/{0}', 'dev') && (github.event_name == 'workflow_dispatch' || github.event_name == 'schedule')

      - name: Upload Binaries
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ./server/bin/tunarr-*
        if: startsWith(github.ref, 'refs/tags/')
