name: Build

on:
  push:
    branches:
      - main
      - dev
    tags:
      - 'v*'

env:
  TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
  CARGO_INCREMENTAL: 1
  CARGO_NET_RETRY: 10
  RUSTUP_MAX_RETRIES: 10

jobs:
  increment-version:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main' && !contains(github.event.head_commit.message, '[skip ci]')
    outputs:
      version: ${{ steps.bump.outputs.VERSION }}
      should_build: ${{ steps.bump.outputs.SHOULD_BUILD }}
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Bump patch version
        id: bump
        run: |
          # Get current version
          CURRENT=$(grep -Po '"version":\s*"\K[^"]+' src-tauri/tauri.conf.json | head -1)
          echo "Current version: $CURRENT"
          
          # Split and increment patch
          MAJOR=$(echo $CURRENT | cut -d. -f1)
          MINOR=$(echo $CURRENT | cut -d. -f2)
          PATCH=$(echo $CURRENT | cut -d. -f3)
          NEW_PATCH=$((PATCH + 1))
          NEW_VERSION="${MAJOR}.${MINOR}.${NEW_PATCH}"
          echo "New version: $NEW_VERSION"
          
          # Update tauri.conf.json
          sed -i "s/\"version\": \"${CURRENT}\"/\"version\": \"${NEW_VERSION}\"/" src-tauri/tauri.conf.json
          
          # Verify the change
          grep '"version"' src-tauri/tauri.conf.json | head -1
          
          echo "VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "SHOULD_BUILD=true" >> $GITHUB_OUTPUT
      
      - name: Commit and push version bump
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          git add src-tauri/tauri.conf.json
          git commit -m "chore: bump version to ${{ steps.bump.outputs.VERSION }} [skip ci]"
          git push

  build-windows:
    runs-on: windows
    needs: increment-version
    if: always() && (needs.increment-version.outputs.should_build == 'true' || needs.increment-version.result == 'skipped')
    env:
      CARGO_TARGET_DIR: C:\cargo-cache\cinny-desktop
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false
          ref: ${{ github.ref_name }}
          clean: false
      
      - name: Pull latest (after version bump)
        if: github.ref == 'refs/heads/main'
        shell: powershell
        run: git pull origin main
      
      - name: Checkout submodules (Windows workaround)
        shell: powershell
        run: |
          git submodule update --init --recursive
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true
      
      - name: Install dependencies (root)
        run: npm install --prefer-offline
      
      - name: Install dependencies (cinny)
        working-directory: ./cinny
        run: npm install --prefer-offline
      
      - name: Build Tauri app
        run: npm run tauri build
      
      - name: Copy and rename MSI files
        shell: powershell
        run: |
          # Clean and recreate output dir
          $outDir = "src-tauri/target/release/bundle/msi"
          if (Test-Path $outDir) { Remove-Item -Recurse -Force $outDir }
          New-Item -ItemType Directory -Force -Path $outDir | Out-Null
          
          # Find and copy MSI from cache location (get NEWEST file by LastWriteTime)
          $srcDir = "$env:CARGO_TARGET_DIR/release/bundle/msi"
          Write-Host "Looking for MSI in: $srcDir"
          Get-ChildItem $srcDir -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending
          
          $msi = Get-ChildItem "$srcDir/*.msi" -ErrorAction Stop | Sort-Object LastWriteTime -Descending | Select-Object -First 1
          Write-Host "Found MSI: $($msi.FullName) (Size: $($msi.Length) bytes, Modified: $($msi.LastWriteTime))"
          Copy-Item $msi.FullName "$outDir/Cinny-Windows-x64.msi" -Force
          
          $msiZip = Get-ChildItem "$srcDir/*.msi.zip" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
          if ($msiZip) { 
            Write-Host "Found MSI.zip: $($msiZip.FullName)"
            Copy-Item $msiZip.FullName "$outDir/Cinny-Windows-x64.msi.zip" -Force 
          }
          
          $msiZipSig = Get-ChildItem "$srcDir/*.msi.zip.sig" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
          if ($msiZipSig) { 
            Write-Host "Found MSI.zip.sig: $($msiZipSig.FullName)"
            Copy-Item $msiZipSig.FullName "$outDir/Cinny-Windows-x64.msi.zip.sig" -Force 
          }
          
          $msiSig = Get-ChildItem "$srcDir/*.msi.sig" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1
          if ($msiSig) { 
            Write-Host "Found MSI.sig: $($msiSig.FullName)"
            Copy-Item $msiSig.FullName "$outDir/Cinny-Windows-x64.msi.sig" -Force 
          }
          
          Write-Host "Final output:"
          Get-ChildItem $outDir
      
      - name: Upload MSI installer
        uses: actions/upload-artifact@v3
        with:
          name: Cinny-Windows-x64.msi
          path: src-tauri/target/release/bundle/msi/Cinny-Windows-x64.msi
      
      - name: Upload MSI updater zip
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: Cinny-Windows-x64.msi.zip
          path: src-tauri/target/release/bundle/msi/Cinny-Windows-x64.msi.zip
      
      - name: Upload MSI updater signature (zip)
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: Cinny-Windows-x64.msi.zip.sig
          path: src-tauri/target/release/bundle/msi/Cinny-Windows-x64.msi.zip.sig
      
      - name: Upload MSI signature
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: Cinny-Windows-x64.msi.sig
          path: src-tauri/target/release/bundle/msi/Cinny-Windows-x64.msi.sig

  build-linux:
    runs-on: ubuntu-latest
    needs: increment-version
    if: always() && (needs.increment-version.outputs.should_build == 'true' || needs.increment-version.result == 'skipped')
    env:
      CARGO_TARGET_DIR: /home/runner/cargo-cache/cinny-desktop
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: recursive
          ref: ${{ github.ref_name }}
      
      - name: Pull latest (after version bump)
        if: github.ref == 'refs/heads/main'
        run: git pull origin main
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true
      
      - name: Install system dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libssl-dev libgtk-3-dev libayatana-appindicator3-dev librsvg2-dev rpm patchelf xdg-utils
      
      - name: Install dependencies (root)
        run: npm ci --prefer-offline
      
      - name: Install dependencies (cinny)
        working-directory: ./cinny
        run: npm ci --prefer-offline
      
      - name: Build Tauri app
        run: npm run tauri build
      
      - name: Copy and rename Linux packages
        run: |
          # Clean and recreate workspace output directories
          rm -rf src-tauri/target/release/bundle/appimage
          rm -rf src-tauri/target/release/bundle/deb
          rm -rf src-tauri/target/release/bundle/rpm
          mkdir -p src-tauri/target/release/bundle/appimage
          mkdir -p src-tauri/target/release/bundle/deb
          mkdir -p src-tauri/target/release/bundle/rpm
          
          # Show what's available in cache (sorted by time, newest first)
          echo "Available AppImage files:"
          ls -lt $CARGO_TARGET_DIR/release/bundle/appimage/ 2>/dev/null || true
          echo "Available DEB files:"
          ls -lt $CARGO_TARGET_DIR/release/bundle/deb/ 2>/dev/null || true
          echo "Available RPM files:"
          ls -lt $CARGO_TARGET_DIR/release/bundle/rpm/ 2>/dev/null || true
          
          # Copy from persistent cache location (get NEWEST file by modification time)
          APPIMAGE=$(ls -t $CARGO_TARGET_DIR/release/bundle/appimage/*.AppImage 2>/dev/null | head -1)
          echo "Copying AppImage: $APPIMAGE"
          cp "$APPIMAGE" src-tauri/target/release/bundle/appimage/Cinny-Linux-x64.AppImage
          
          DEB=$(ls -t $CARGO_TARGET_DIR/release/bundle/deb/*.deb 2>/dev/null | head -1)
          echo "Copying DEB: $DEB"
          cp "$DEB" src-tauri/target/release/bundle/deb/Cinny-Linux-x64.deb
          
          RPM=$(ls -t $CARGO_TARGET_DIR/release/bundle/rpm/*.rpm 2>/dev/null | head -1)
          echo "Copying RPM: $RPM"
          cp "$RPM" src-tauri/target/release/bundle/rpm/Cinny-Linux-x64.rpm
          
          # Copy signature file for updater if it exists (get NEWEST)
          if ls $CARGO_TARGET_DIR/release/bundle/appimage/*.AppImage.sig 1> /dev/null 2>&1; then
            APPSIG=$(ls -t $CARGO_TARGET_DIR/release/bundle/appimage/*.AppImage.sig 2>/dev/null | head -1)
            echo "Copying AppImage sig: $APPSIG"
            cp "$APPSIG" src-tauri/target/release/bundle/appimage/Cinny-Linux-x64.AppImage.sig
          fi
          
          # List final output
          echo "Final output:"
          ls -la src-tauri/target/release/bundle/appimage/
          ls -la src-tauri/target/release/bundle/deb/
          ls -la src-tauri/target/release/bundle/rpm/
      
      - name: Upload AppImage
        uses: actions/upload-artifact@v3
        with:
          name: Cinny-Linux-x64.AppImage
          path: src-tauri/target/release/bundle/appimage/Cinny-Linux-x64.AppImage
      
      - name: Upload AppImage Signature
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: Cinny-Linux-x64.AppImage.sig
          path: src-tauri/target/release/bundle/appimage/Cinny-Linux-x64.AppImage.sig
      
      - name: Upload DEB package
        uses: actions/upload-artifact@v3
        with:
          name: Cinny-Linux-x64.deb
          path: src-tauri/target/release/bundle/deb/Cinny-Linux-x64.deb
      
      - name: Upload RPM package
        uses: actions/upload-artifact@v3
        with:
          name: Cinny-Linux-x64.rpm
          path: src-tauri/target/release/bundle/rpm/Cinny-Linux-x64.rpm

  build-android:
    runs-on: windows
    needs: increment-version
    if: always() && (needs.increment-version.outputs.should_build == 'true' || needs.increment-version.result == 'skipped')
    env:
      CARGO_TARGET_DIR: C:\cargo-cache\cinny-desktop-android
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false
          ref: ${{ github.ref_name }}
          clean: false
      
      - name: Pull latest (after version bump)
        if: github.ref == 'refs/heads/main'
        shell: powershell
        run: git pull origin main
      
      - name: Checkout submodules (Windows workaround)
        shell: powershell
        run: |
          git submodule update --init --recursive
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'
      
      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          profile: minimal
          override: true
          target: aarch64-linux-android
      
      - name: Add Android targets
        run: |
          rustup target add aarch64-linux-android --toolchain stable
      
      - name: Install dependencies (root)
        run: npm ci --prefer-offline
      
      - name: Install dependencies (cinny)
        working-directory: ./cinny
        run: npm ci --prefer-offline
      
      - name: Build Android APK
        shell: powershell
        timeout-minutes: 30
        run: |
          $env:PATH = "$env:USERPROFILE\.cargo\bin;$env:PATH"
          npm run tauri android build -- --ci --target aarch64
          # Kill any lingering processes that might hang
          Stop-Process -Name "java" -Force -ErrorAction SilentlyContinue
          Stop-Process -Name "gradle" -Force -ErrorAction SilentlyContinue
        env:
          ANDROID_HOME: ${{ env.ANDROID_HOME }}
          NDK_HOME: ${{ env.ANDROID_HOME }}/ndk/25.2.9519653
          RUSTUP_TOOLCHAIN: stable
          CI: true
      
      - name: Sign APK
        shell: powershell
        run: |
          $unsignedApk = "src-tauri/gen/android/app/build/outputs/apk/universal/release/app-universal-release-unsigned.apk"
          $signedApk = "src-tauri/gen/android/app/build/outputs/apk/universal/release/cinny-release.apk"
          $keystoreDir = "$env:USERPROFILE\.android"
          $keystore = "$keystoreDir\debug.keystore"
          
          # Find Android SDK
          $androidHome = if ($env:ANDROID_HOME) { $env:ANDROID_HOME } 
                         elseif ($env:ANDROID_SDK_ROOT) { $env:ANDROID_SDK_ROOT }
                         elseif (Test-Path "$env:LOCALAPPDATA\Android\Sdk") { "$env:LOCALAPPDATA\Android\Sdk" }
                         else { "$env:USERPROFILE\AppData\Local\Android\Sdk" }
          
          Write-Host "Using Android SDK at: $androidHome"
          
          # Ensure keystore directory exists
          if (-not (Test-Path $keystoreDir)) {
            New-Item -ItemType Directory -Path $keystoreDir -Force | Out-Null
          }
          
          # Use debug keystore for signing (create if missing)
          if (-not (Test-Path $keystore)) {
            Write-Host "Creating debug keystore..."
            # Use -noprompt to avoid any interactive prompts
            & keytool -genkeypair -noprompt -keystore $keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Android Debug,O=Android,C=US"
            if ($LASTEXITCODE -ne 0) {
              Write-Error "Failed to create keystore"
              exit 1
            }
          }
          
          Write-Host "Using keystore: $keystore"
          
          # Find build tools
          $buildTools = Get-ChildItem "$androidHome\build-tools" | Sort-Object Name -Descending | Select-Object -First 1
          $apksigner = "$($buildTools.FullName)\apksigner.bat"
          $zipalign = "$($buildTools.FullName)\zipalign.exe"
          
          Write-Host "Using build tools: $($buildTools.FullName)"
          
          # Align the APK
          Write-Host "Aligning APK..."
          & $zipalign -v -p 4 $unsignedApk "$unsignedApk.aligned"
          if ($LASTEXITCODE -ne 0) {
            Write-Error "Failed to align APK"
            exit 1
          }
          
          # Sign the APK
          Write-Host "Signing APK..."
          & $apksigner sign --ks $keystore --ks-pass pass:android --key-pass pass:android --out $signedApk "$unsignedApk.aligned"
          if ($LASTEXITCODE -ne 0) {
            Write-Error "Failed to sign APK"
            exit 1
          }
          
          # Verify
          Write-Host "Verifying signature..."
          & $apksigner verify $signedApk
          
          Write-Host "Signed APK created at: $signedApk"
          
          # Rename to final name
          Copy-Item $signedApk "src-tauri/gen/android/app/build/outputs/apk/universal/release/Cinny-Android.apk"
          Write-Host "Done! APK ready at: Cinny-Android.apk"
      
      - name: Upload APK
        uses: actions/upload-artifact@v3
        with:
          name: Cinny-Android.apk
          path: src-tauri/gen/android/app/build/outputs/apk/universal/release/Cinny-Android.apk

  create-release:
    runs-on: ubuntu-latest
    needs: [increment-version, build-windows, build-linux, build-android]
    if: always() && (needs.build-windows.result == 'success' || needs.build-linux.result == 'success')
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.ref_name }}
      
      - name: Pull latest (after version bump)
        if: github.ref == 'refs/heads/main'
        run: git pull origin main
      
      - name: Get version
        id: version
        run: |
          # Use version from increment-version job if available, otherwise read from file
          if [ -n "${{ needs.increment-version.outputs.version }}" ]; then
            VERSION="${{ needs.increment-version.outputs.version }}"
          else
            VERSION=$(grep -Po '"version":\s*"\K[^"]+' src-tauri/tauri.conf.json | head -1)
          fi
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"
      
      - name: Check if tag exists
        id: tag_check
        run: |
          VERSION="${{ steps.version.outputs.VERSION }}"
          if git rev-parse "v$VERSION" >/dev/null 2>&1; then
            echo "Tag v$VERSION already exists"
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "Tag v$VERSION does not exist"
            echo "exists=false" >> $GITHUB_OUTPUT
          fi
      
      - name: Create and push tag
        if: steps.tag_check.outputs.exists == 'false'
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          git tag -a "v${{ steps.version.outputs.VERSION }}" -m "Release v${{ steps.version.outputs.VERSION }}"
          git push origin "v${{ steps.version.outputs.VERSION }}"
      
      - name: Download all artifacts
        uses: actions/download-artifact@v3
        with:
          path: artifacts
      
      - name: Create update.json
        run: |
          VERSION="${{ steps.version.outputs.VERSION }}"
          APPIMAGE_SIG=""
          MSI_SIG=""
          if [ -f "artifacts/Cinny-Linux-x64.AppImage.sig/Cinny-Linux-x64.AppImage.sig" ]; then
            APPIMAGE_SIG=$(cat "artifacts/Cinny-Linux-x64.AppImage.sig/Cinny-Linux-x64.AppImage.sig")
          fi
          if [ -f "artifacts/Cinny-Windows-x64.msi.sig/Cinny-Windows-x64.msi.sig" ]; then
            MSI_SIG=$(cat "artifacts/Cinny-Windows-x64.msi.sig/Cinny-Windows-x64.msi.sig")
          fi
          
          cat > update.json << EOF
          {
            "version": "${VERSION}",
            "notes": "Update to version ${VERSION}",
            "pub_date": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
            "platforms": {
              "linux-x86_64": {
                "signature": "${APPIMAGE_SIG}",
                "url": "http://synbox.ruv.wtf:8418/litruv/cinny-desktop/releases/download/v${VERSION}/Cinny-Linux-x64.AppImage"
              },
              "windows-x86_64": {
                "signature": "${MSI_SIG}",
                "url": "http://synbox.ruv.wtf:8418/litruv/cinny-desktop/releases/download/v${VERSION}/Cinny-Windows-x64.msi"
              }
            }
          }
          EOF
          cat update.json
      
      - name: Prepare release files
        run: |
          mkdir -p release-files
          find artifacts -type f \( -name "*.msi" -o -name "*.msi.zip" -o -name "*.msi.zip.sig" -o -name "*.AppImage" -o -name "*.deb" -o -name "*.rpm" -o -name "*.apk" -o -name "*.sig" \) -exec cp {} release-files/ \;
          cp update.json release-files/
          ls -la release-files/
      
      - name: Create or Update Release
        env:
          GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          VERSION="${{ steps.version.outputs.VERSION }}"
          TAG_NAME="v${VERSION}"
          API_BASE="http://synbox.ruv.wtf:8418/api/v1"
          REPO="litruv/cinny-desktop"
          
          # Check if release exists
          RELEASE_ID=$(curl -s -H "Authorization: token ${GITEA_TOKEN}" \
            "${API_BASE}/repos/${REPO}/releases/tags/${TAG_NAME}" | jq -r '.id // empty')
          
          if [ -z "$RELEASE_ID" ]; then
            echo "Creating new release for ${TAG_NAME}..."
            RELEASE_ID=$(curl -s -X POST \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -H "Content-Type: application/json" \
              -d "{\"tag_name\": \"${TAG_NAME}\", \"name\": \"Release ${TAG_NAME}\", \"body\": \"Release ${VERSION}\", \"draft\": false, \"prerelease\": false}" \
              "${API_BASE}/repos/${REPO}/releases" | jq -r '.id')
            echo "Created release with ID: ${RELEASE_ID}"
          else
            echo "Release exists with ID: ${RELEASE_ID}"
            # Delete existing assets
            echo "Deleting existing assets..."
            ASSETS=$(curl -s -H "Authorization: token ${GITEA_TOKEN}" \
              "${API_BASE}/repos/${REPO}/releases/${RELEASE_ID}/assets" | jq -r '.[].id')
            for ASSET_ID in $ASSETS; do
              echo "Deleting asset ${ASSET_ID}..."
              curl -s -X DELETE -H "Authorization: token ${GITEA_TOKEN}" \
                "${API_BASE}/repos/${REPO}/releases/${RELEASE_ID}/assets/${ASSET_ID}"
            done
          fi
          
          # Upload new assets
          echo "Uploading assets..."
          for FILE in release-files/*; do
            FILENAME=$(basename "$FILE")
            echo "Uploading ${FILENAME}..."
            curl -s -X POST \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -H "Content-Type: application/octet-stream" \
              --data-binary "@${FILE}" \
              "${API_BASE}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=${FILENAME}"
            echo ""
          done
          
          echo "Release complete!"
          
          # Also update 'latest' release with update.json for auto-updater
          echo "Updating 'latest' release..."
          LATEST_TAG="latest"
          LATEST_ID=$(curl -s -H "Authorization: token ${GITEA_TOKEN}" \
            "${API_BASE}/repos/${REPO}/releases/tags/${LATEST_TAG}" | jq -r '.id // empty')
          
          if [ -z "$LATEST_ID" ]; then
            echo "Creating 'latest' release..."
            LATEST_ID=$(curl -s -X POST \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -H "Content-Type: application/json" \
              -d "{\"tag_name\": \"${LATEST_TAG}\", \"name\": \"Latest Release\", \"body\": \"Always points to the most recent version (${VERSION})\", \"draft\": false, \"prerelease\": false}" \
              "${API_BASE}/repos/${REPO}/releases" | jq -r '.id')
          else
            # Update release body
            curl -s -X PATCH \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -H "Content-Type: application/json" \
              -d "{\"body\": \"Always points to the most recent version (${VERSION})\"}" \
              "${API_BASE}/repos/${REPO}/releases/${LATEST_ID}"
            # Delete existing update.json asset
            LATEST_ASSETS=$(curl -s -H "Authorization: token ${GITEA_TOKEN}" \
              "${API_BASE}/repos/${REPO}/releases/${LATEST_ID}/assets" | jq -r '.[] | select(.name=="update.json") | .id')
            for ASSET_ID in $LATEST_ASSETS; do
              curl -s -X DELETE -H "Authorization: token ${GITEA_TOKEN}" \
                "${API_BASE}/repos/${REPO}/releases/${LATEST_ID}/assets/${ASSET_ID}"
            done
          fi
          
          # Upload update.json to latest
          curl -s -X POST \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -H "Content-Type: application/octet-stream" \
            --data-binary "@release-files/update.json" \
            "${API_BASE}/repos/${REPO}/releases/${LATEST_ID}/assets?name=update.json"
          
          echo "Latest release updated!"
