#!/bin/bash
# Environment Setup Script for Remote Development

set -e

echo "🔧 Setting up development environment..."

# Create .env file from example
if [ ! -f .env ]; then
    cp .env.example .env
    echo "📝 Created .env file from template"
    echo "⚠️  Please edit .env with your specific configuration"
fi

# Create necessary directories
mkdir -p certs/
mkdir -p mosquitto/{config,data,log}
mkdir -p server/media/converted
mkdir -p server/received_screenshots
mkdir -p server/screenshots

# Set permissions for mosquitto
sudo chown -R 1883:1883 mosquitto/data mosquitto/log 2>/dev/null || true
sudo chmod 755 mosquitto/config mosquitto/data mosquitto/log

# Generate development SSL certificates if they don't exist
if [ ! -f certs/dev.crt ] || [ ! -f certs/dev.key ]; then
    echo "🔒 Generating development SSL certificates..."
    openssl req -x509 -newkey rsa:4096 -keyout certs/dev.key -out certs/dev.crt -days 365 -nodes \
        -subj "/C=AT/ST=Vienna/L=Vienna/O=Development/CN=localhost"
    echo "✅ SSL certificates generated"
fi

echo "✅ Environment setup complete!"
echo ""
echo "📋 Next steps:"
echo "1. Edit .env file with your configuration"
echo "2. Run: docker compose up -d --build"
echo "3. Access dashboard at http://localhost:5173"
