#!/bin/bash

# Flutter Tank Web App - Update Script
# Erstellt ein neues Web Build und kopiert es auf den Server

set -e

# Configuration
SERVER_USER="root"
SERVER_IP="192.168.1.19"
SERVER_PATH="/root/webserver"

echo "🚀 Starting Flutter Web App Update..."
echo "================================================"

# Step 1: Build Flutter Web
echo "📦 Building Flutter web release..."
flutter build web --release

if [ $? -ne 0 ]; then
    echo "❌ Build failed!"
    exit 1
fi

echo "✅ Build successful!"

# Step 2: Clean and copy to local webserver folder (preserve installDocker)
echo "🧹 Cleaning webserver folder (keeping installDocker)..."
rm -rf webserver/assets webserver/canvaskit webserver/icons
rm -f webserver/*.js webserver/*.json webserver/*.html webserver/*.png

echo "📋 Copying new build to webserver folder..."
cp -r build/web/* webserver/

echo "✅ Local webserver folder updated!"

# Step 3: Copy to server
echo "📤 Copying to server $SERVER_IP..."
sudo scp -r webserver $SERVER_USER@$SERVER_IP:$SERVER_PATH

if [ $? -ne 0 ]; then
    echo "❌ Server copy failed!"
    exit 1
fi

echo ""
echo "✅ Update complete!"
echo "================================================"
echo "Next steps on the server:"
echo "  ssh $SERVER_USER@$SERVER_IP"
echo "  cd $SERVER_PATH/installDocker"
echo "  ./deploy.sh"
echo "================================================"
