#!/bin/bash
#################################################################
# EMERGENCY SYMLINK FIX FOR CPANEL
# Run this IMMEDIATELY on your server if symlink error persists
#################################################################

echo ""
echo "=========================================="
echo "  EMERGENCY SYMLINK FIX"
echo "=========================================="
echo ""

# Go to script directory
cd "$(dirname "$0")"

echo "[1/6] Checking current state..."
if [ -L "public/storage" ]; then
    echo "      ✗ public/storage is a SYMLINK (this is the problem!)"
elif [ -d "public/storage" ]; then
    echo "      ✓ public/storage is a directory"
elif [ -e "public/storage" ]; then
    echo "      ? public/storage exists but is neither symlink nor directory"
else
    echo "      - public/storage does not exist"
fi

echo ""
echo "[2/6] FORCE removing public/storage (all methods)..."

# Method 1: unlink (for symlinks)
if [ -L "public/storage" ]; then
    echo "      → Removing symlink with unlink..."
    unlink public/storage 2>/dev/null && echo "      ✓ Symlink removed" || echo "      ! unlink failed"
fi

# Method 2: rm -rf (force remove everything)
if [ -e "public/storage" ]; then
    echo "      → Force removing with rm -rf..."
    rm -rf public/storage 2>/dev/null && echo "      ✓ Removed with rm -rf" || echo "      ! rm failed"
fi

# Method 3: Remove via find (catches hidden symlinks)
if [ -e "public/storage" ]; then
    echo "      → Using find command..."
    find public -maxdepth 1 -name "storage" -exec rm -rf {} \; 2>/dev/null
fi

# Verify removal
if [ -e "public/storage" ]; then
    echo "      ✗ FAILED - Still exists!"
    echo ""
    echo "MANUAL ACTION REQUIRED:"
    echo "1. Login to cPanel File Manager"
    echo "2. Go to public/ folder"
    echo "3. Delete 'storage' (might show with arrow/chain icon)"
    echo "4. Run this script again"
    echo ""
    exit 1
else
    echo "      ✓ Successfully removed"
fi

echo ""
echo "[3/6] Creating fresh directories..."

# Create public/storage
mkdir -p public/storage
chmod 755 public/storage
echo "      ✓ Created public/storage"

# Create storage/app/public
mkdir -p storage/app/public
chmod 755 storage/app/public
echo "      ✓ Created storage/app/public"

echo ""
echo "[4/6] Setting permissions..."

# Set all necessary permissions
chmod -R 755 storage 2>/dev/null
chmod -R 755 bootstrap/cache 2>/dev/null
chmod 755 public/storage 2>/dev/null
find storage -type d -exec chmod 755 {} \; 2>/dev/null
find storage -type f -exec chmod 644 {} \; 2>/dev/null

echo "      ✓ Permissions set"

echo ""
echo "[5/6] Clearing Laravel caches..."

php artisan config:clear 2>/dev/null
php artisan cache:clear 2>/dev/null
php artisan view:clear 2>/dev/null
php artisan route:clear 2>/dev/null

echo "      ✓ Caches cleared"

echo ""
echo "[6/6] Final verification..."

if [ -L "public/storage" ]; then
    echo "      ✗ ERROR: Still a symlink!"
    exit 1
elif [ -d "public/storage" ]; then
    echo "      ✓ public/storage is a REAL directory"
else
    echo "      ✗ ERROR: public/storage missing!"
    exit 1
fi

if [ -d "storage/app/public" ]; then
    echo "      ✓ storage/app/public exists"
else
    echo "      ! Warning: storage/app/public missing"
fi

echo ""
echo "=========================================="
echo "  ✓ FIX COMPLETE!"
echo "=========================================="
echo ""
echo "Verification:"
ls -la public/ | grep storage
echo ""
echo "Should show: drwxr-xr-x (directory)"
echo "NOT:         lrwxrwxrwx (symlink)"
echo ""
echo "Test your application now."
echo ""
