#!/bin/bash

# TimeSafari Electron Build Script
# Usage: ./build-packages.sh [pack|appimage|deb|rpm|all]

set -e

echo "🚀 TimeSafari Electron Build Script"
echo "=================================="

# Build TypeScript and rebuild native modules
echo "📦 Building TypeScript and native modules..."
npm run build

BUILD_TYPE="${1:-all}"

case "$BUILD_TYPE" in
  "pack")
    echo "📦 Creating unpacked build..."
    npx electron-builder build --dir -c ./electron-builder.config.json
    ;;
  "appimage")
    echo "📦 Creating AppImage..."
    npx electron-builder build --linux AppImage -c ./electron-builder.config.json
    ;;
  "deb")
    echo "📦 Creating Debian package..."
    npx electron-builder build --linux deb -c ./electron-builder.config.json
    ;;
  "rpm")
    echo "📦 Creating RPM package..."
    if ! command -v rpmbuild &> /dev/null; then
      echo "⚠️  rpmbuild not found. Install with: sudo pacman -S rpm-tools"
      exit 1
    fi
    npx electron-builder build --linux rpm -c ./electron-builder.config.json
    ;;
  "all")
    echo "📦 Creating all Linux packages..."
    npx electron-builder build --linux -c ./electron-builder.config.json
    ;;
  *)
    echo "❌ Unknown build type: $BUILD_TYPE"
    echo "Usage: $0 [pack|appimage|deb|rpm|all]"
    exit 1
    ;;
esac

echo ""
echo "✅ Build completed successfully!"
echo "📁 Output files in: ./dist/"
echo ""
echo "📦 Available packages:"
ls -la dist/ | grep -E '\.(AppImage|deb|rpm)$' || echo "   No packages found"
echo ""
echo "🎉 Ready to distribute!" 