#!/bin/bash

# EMERGENCY SYMLINK FIX FOR ALGOPK.COM
# Run this script via SSH in your public directory

echo "🚨 EMERGENCY SYMLINK FIX STARTING..."

# Navigate to public directory
cd public

# 1. Remove symlink if it exists
if [ -L "storage" ]; then
    echo "🔗 Removing symlink: storage"
    unlink storage
    echo "✅ Symlink removed"
elif [ -d "storage" ]; then
    echo "✅ storage is already a real directory"
else
    echo "📁 Creating storage directory"
fi

# 2. Create real directory if it doesn't exist
if [ ! -d "storage" ]; then
    mkdir -p storage
    echo "✅ Real directory created"
fi

# 3. Set proper permissions
chmod 755 storage
echo "✅ Permissions set to 755"

# 4. Create .gitignore
if [ ! -f "storage/.gitignore" ]; then
    echo "*" > storage/.gitignore
    echo "!.gitignore" >> storage/.gitignore
    echo "✅ .gitignore created"
fi

# 5. Copy files from storage/app/public if they exist
if [ -d "../storage/app/public" ]; then
    echo "📋 Copying files from storage/app/public..."
    cp -r ../storage/app/public/* storage/ 2>/dev/null || true
    echo "✅ Files copied (if any existed)"
fi

# 6. Clear Laravel cache
cd ..
echo "🧹 Clearing Laravel cache..."
php artisan cache:clear --quiet
php artisan config:clear --quiet
php artisan route:clear --quiet
echo "✅ Cache cleared"

echo "🎉 SYMLINK FIX COMPLETE!"
echo "Your site should now work without symlink errors."
echo "Visit: https://algopk.com"
