#!/bin/bash
# Setup Independent Aumentum System
# Removes dependency on Windows server 10.10.10.3

set -e

echo "=========================================="
echo "Setting Up Independent Aumentum System"
echo "=========================================="

# Default contentstore directory
CONTENTSTORE_DIR="${1:-/opt/aumentum/contentstore}"

echo ""
echo "📁 Step 1: Creating local contentstore directory..."
sudo mkdir -p "$CONTENTSTORE_DIR"
sudo chown $USER:$USER "$CONTENTSTORE_DIR"
echo "✅ Created: $CONTENTSTORE_DIR"

echo ""
echo "🔧 Step 2: Setting environment variable..."
# Add to .bashrc
if ! grep -q "CONTENTSTORE_BASE" ~/.bashrc; then
    echo "export CONTENTSTORE_BASE=$CONTENTSTORE_DIR" >> ~/.bashrc
    echo "✅ Added to ~/.bashrc"
else
    echo "⚠️  CONTENTSTORE_BASE already in ~/.bashrc"
fi

# Set for current session
export CONTENTSTORE_BASE="$CONTENTSTORE_DIR"
echo "✅ Set for current session: CONTENTSTORE_BASE=$CONTENTSTORE_DIR"

echo ""
echo "📊 Step 3: Verification..."
echo "   Directory: $CONTENTSTORE_DIR"
echo "   Exists: $(test -d "$CONTENTSTORE_DIR" && echo 'Yes' || echo 'No')"
echo "   Writable: $(test -w "$CONTENTSTORE_DIR" && echo 'Yes' || echo 'No')"

echo ""
echo "=========================================="
echo "✅ Setup Complete!"
echo "=========================================="
echo ""
echo "Next steps:"
echo "1. Copy files from Windows server (if needed):"
echo "   rsync -avz //10.10.10.3/LRS_STORAGE/contentstore/ $CONTENTSTORE_DIR/"
echo ""
echo "2. Or start fresh (empty directory for new files)"
echo ""
echo "3. Restart your application to use new path"
echo ""
echo "4. Verify:"
echo "   source venv/bin/activate"
echo "   python3 -c \"from aumentum_browser_service import DEFAULT_CONTENTSTORE_BASE; print('Contentstore:', DEFAULT_CONTENTSTORE_BASE)\""
