name: Build & Publish Binaries

on:
  workflow_dispatch:
  release:
    types: [created]
  push:
    tags:
      - v[0-9]+.[0-9]+.[0-9]+*

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: windows-latest
            target: win-x64
    continue-on-error: true
    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"
        run: |
          touch .env
          echo TUNARR_VERSION="${{github.event.release.tag_name || github.ref_name}}" >> .env
          echo TUNARR_BUILD="$(git rev-parse --short ${{ github.sha }})" >> .env
          echo TUNARR_EDGE_BUILD=${{github.ref == format('refs/heads/{0}', 'dev') && (github.event_name == 'workflow_dispatch' || github.event_name == 'schedule')}} >> .env
          echo NODE_ENV=production >> .env
          echo TUNARR_BIND_ADDR=0.0.0.0 >> .env
          grep -v '^#.*' .env >> $GITHUB_ENV
          cat .env
          cp .env server/.env
          cp .env web/.env

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

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

      - name: Upload Edge Binary
        uses: softprops/action-gh-release@v2.4.2
        with:
          name: Tunarr Edge
          tag_name: edge
          prerelease: true
          files: |
            ./server/bin/tunarr-*.*(tar.gz|zip)
        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.4.2
        with:
          files: |
            ./server/bin/tunarr-*.*(tar.gz|zip)
        if: startsWith(github.ref, 'refs/tags/')
  build-mac:
    needs: typecheck
    strategy:
      matrix:
        os:
          - name: macos-x64
            target: macos-x64
            arch: x86_64
            runner: macos-15
          - name: macos-arm
            target: macos-arm64
            arch: arm64
            runner: macos-15
    runs-on: ${{matrix.os.runner}}
    continue-on-error: true
    steps:
      - name: Checkout
        uses: actions/checkout@v4 # Checkout code from repository

      - 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
        shell: bash
        run: |
          pnpm install
          brew install jq

      - name: Import Code-Signing Certificates
        uses: Apple-Actions/import-codesign-certs@v2
        with:
          p12-file-base64: ${{ secrets.APPLE_CERTIFICATE_P12_BASE64 }}
          p12-password: ${{ secrets.APPLE_CERTIFICATE_P12_PASSWORD }}

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

          tunarr_version="${{github.event.release.tag_name || github.ref_name}}"
          tunarr_full_version="${tunarr_version}"
          if [[ "$TUNARR_EDGE_BUILD" == "true" || "$TUNARR_EDGE_BUILD" == "1" ]]; then
            is_edge_build=true
          else
            is_edge_build=false
          fi

          if [[ -n "$TUNARR_BUILD" && "$TUNARR_EDGE_BUILD" == true ]]; then
            tunarr_full_version+="-${build}"
          fi
          echo "TUNARR_VERSION=${tunarr_version}" >> .env
          echo "TUNARR_FULL_VERSION=${tunarr_full_version}" >> .env

          release_name="Tunarr-${tunarr_full_version}-${{ matrix.os.target }}"
          echo "RELEASE_NAME=${release_name}" >> .env

          grep -v '^#.*' .env >> $GITHUB_ENV
          cat .env
          cp .env server/.env
          cp .env web/.env

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

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

      - name: Bundle
        shell: bash
        run: |
          brew install coreutils
          plutil -replace CFBundleShortVersionString -string "$TUNARR_FULL_VERSION" macos/Tunarr/Tunarr/Info.plist
          plutil -replace CFBundleVersion -string "$TUNARR_FULL_VERSION" macos/Tunarr/Tunarr/Info.plist

          if [[ "$TUNARR_EDGE_BUILD" == "true" || "$TUNARR_EDGE_BUILD" == "1" ]]; then
            scripts/bundle-macos.sh "tunarr-${{ matrix.os.target }}" "${{ matrix.os.arch }}" "${{matrix.os.target}}"
          else
            scripts/bundle-macos.sh "tunarr-${{ env.TUNARR_VERSION }}-${{ matrix.os.target }}" "${{ matrix.os.arch }}" "${{matrix.os.target}}"
          fi

      - name: Sign
        shell: bash
        run: scripts/sign-macos.sh

      - name: Create DMG
        shell: bash
        run: |
          brew install create-dmg
          create-dmg \
            --volname "Tunarr" \
            --volicon "design/Tunarr.icns" \
            --window-pos 200 120 \
            --window-size 800 400 \
            --icon-size 100 \
            --icon "Tunarr.app" 200 190 \
            --hide-extension "Tunarr.app" \
            --app-drop-link 600 185 \
            --skip-jenkins \
            --no-internet-enable \
            "Tunarr.dmg" \
            "Tunarr.app/"

      - name: Notarize
        shell: bash
        run: |
          xcrun notarytool submit Tunarr.dmg --apple-id "${{ secrets.AC_USERNAME }}" --password "${{ secrets.AC_PASSWORD }}" --team-id XX35C4MZN8 --wait
          xcrun stapler staple Tunarr.dmg

      - name: Finalize
        shell: bash
        run: |
          mv Tunarr.dmg "${{env.RELEASE_NAME}}.dmg"

      - name: Upload Edge Binary
        uses: softprops/action-gh-release@v2
        with:
          name: Tunarr Edge
          tag_name: edge
          prerelease: true
          files: |
            ${{ env.RELEASE_NAME }}.dmg
        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: |
            ${{ env.RELEASE_NAME }}.dmg
        if: startsWith(github.ref, 'refs/tags/')
