#!/bin/bash

# مُصادَق - Shorebird Build Script with Obfuscation
# This script builds the production releases for Android and iOS using Shorebird.

echo "🚀 Starting Shorebird Build Process..."

# 1. Increment Version and Build Number in pubspec.yaml
echo "🔢 Incrementing version (patch) and build number..."
# Increments X.Y.Z+N to X.Y.(Z+1)+(N+1)
perl -i -pe 's/^(version:\s+\d+\.\d+\.)(\d+)\+(\d+)$/$1 . ($2 + 1) . "+" . ($3 + 1)/e' pubspec.yaml

NEW_VERSION=$(grep 'version:' pubspec.yaml | sed 's/version: //')
echo "✅ New Version: $NEW_VERSION"

# 2. Clean and Get Dependencies
echo "🧹 Cleaning project..."
flutter clean
echo "📦 Getting dependencies..."
flutter pub get

# 3. Build Android Release
echo "🤖 Building Android Release via Shorebird..."
shorebird release android -- --obfuscate --split-debug-info=build/app/outputs/symbols

# 4. Build iOS Release
echo "🍎 Building iOS Release via Shorebird..."
shorebird release ios -- --obfuscate --split-debug-info=build/ios/outputs/symbols

echo "✅ Build Process Completed!"
echo "--------------------------------------------------"
echo "Android APK/AppBundle: build/app/outputs/flutter-apk/release/"
echo "iOS Archive: build/ios/archive/"
echo "--------------------------------------------------"
