#!/bin/bash

# SamCloud FileBrowser Build Script
echo "🌩️  Building SamCloud FileBrowser..."

# Set the working directory
cd "$(dirname "$0")/src/frontend"

# Check if node_modules exists
if [ ! -d "node_modules" ]; then
    echo "📦 Installing dependencies..."
    npm install --legacy-peer-deps || {
        echo "❌ npm failed, trying with --force"
        npm install --force
    }
fi

# Build the frontend
echo "🔨 Building frontend..."
npm run build || {
    echo "❌ Build failed, trying alternative approach..."
    npx vite build
}

# Copy built files to our container directory
echo "📋 Copying build files..."
mkdir -p ../../dist
cp -r dist/* ../../dist/

echo "✅ SamCloud FileBrowser build complete!"
echo "🚀 You can now run: docker-compose up -d"
