<?php
/**
 * cPanel Auto-Deployment & Path Setup Utility for Custom PHP App
 * 
 * Instructions:
 * 1. Upload this file directly to your public_html (web root) directory.
 * 2. Upload your backend folder to /home/yourusername/wts-back.
 * 3. Visit https://yourdomain.com/cpanel-deploy.php in your web browser.
 * 4. Once done, DELETE this file immediately for security!
 */

// Possible search paths for the backend folder relative to public_html
$search_paths = [
    __DIR__ . '/../wts-back',
    __DIR__ . '/../../wts-back',
    __DIR__ . '/wts-back',
    __DIR__ . '/../laravel',
    __DIR__ . '/..',
    __DIR__
];

$backend_path = null;
foreach ($search_paths as $path) {
    if (file_exists($path . '/app/bootstrap.php')) {
        $backend_path = realpath($path);
        break;
    }
}

header('Content-Type: text/html; charset=utf-8');
echo "<!DOCTYPE html><html><head><title>cPanel PHP Deployer</title><style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 700px; margin: 40px auto; padding: 20px; line-height: 1.6; background-color: #f7fafc; color: #2d3748; }
.card { background: white; padding: 30px; border-radius: 12px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); border-top: 4px solid #ed8936; }
h2 { color: #2c5282; margin-top: 0; }
.status-ok { color: #38a169; font-weight: bold; }
.status-err { color: #e53e3e; font-weight: bold; }
pre { background: #edf2f7; padding: 15px; border-radius: 8px; font-family: monospace; overflow-x: auto; font-size: 14px; }
</style></head><body><div class='card'>";

if (!$backend_path) {
    echo "<h2>❌ Backend Folder Not Found</h2>";
    echo "<p>Could not locate <code>app/bootstrap.php</code> in any of the search paths:</p><ul>";
    foreach ($search_paths as $path) {
        echo "<li><code>" . htmlspecialchars($path) . "</code></li>";
    }
    echo "</ul>";
    echo "<p><strong>How to fix:</strong> Ensure you have uploaded all backend files (app, routes, resources, vendor, etc.) to <code>/home/yourusername/wts-back</code>.</p>";
    echo "</div></body></html>";
    exit;
}

// Normalize separator to slash for PHP paths
$backend_path_normalized = str_replace('\\', '/', $backend_path);

// Auto-configure public_html/index.php paths
$index_path = __DIR__ . '/index.php';
if (file_exists($index_path)) {
    $index_content = file_get_contents($index_path);
    
    // Exact targets to replace
    $target_autoload = "\$base_dir = __DIR__ . '/../app/';";
    $target_bootstrap = "require_once __DIR__ . '/../app/bootstrap.php';";
    $target_web = "require_once __DIR__ . '/../routes/web.php';";
    
    $replace_autoload = "\$base_dir = '" . $backend_path_normalized . "/app/';";
    $replace_bootstrap = "require_once '" . $backend_path_normalized . "/app/bootstrap.php';";
    $replace_web = "require_once '" . $backend_path_normalized . "/routes/web.php';";
    
    // New unified backend path target for dynamic index.php
    $target_backend = "\$backend_dir = null; // CPANEL_BACKEND_PATH";
    $replace_backend = "\$backend_dir = '" . $backend_path_normalized . "'; // CPANEL_BACKEND_PATH";
    
    $modified = false;
    if (strpos($index_content, $target_autoload) !== false) {
        $index_content = str_replace($target_autoload, $replace_autoload, $index_content);
        $modified = true;
    }
    if (strpos($index_content, $target_bootstrap) !== false) {
        $index_content = str_replace($target_bootstrap, $replace_bootstrap, $index_content);
        $modified = true;
    }
    if (strpos($index_content, $target_web) !== false) {
        $index_content = str_replace($target_web, $replace_web, $index_content);
        $modified = true;
    }
    if (strpos($index_content, $target_backend) !== false) {
        $index_content = str_replace($target_backend, $replace_backend, $index_content);
        $modified = true;
    }
    
    if ($modified) {
        if (is_writable($index_path)) {
            file_put_contents($index_path, $index_content);
            echo "<p class='status-ok'>✅ Successfully updated paths in public_html/index.php to point to:</p>";
            echo "<pre>Autoloader: " . htmlspecialchars($replace_autoload) . "\nBootstrap: " . htmlspecialchars($replace_bootstrap) . "\nWeb Routes: " . htmlspecialchars($replace_web) . "</pre>";
        } else {
            echo "<p class='status-err'>⚠️ Cannot write to public_html/index.php. Please make index.php writable (e.g. 644) and run this script again.</p>";
        }
    } else {
        echo "<p>ℹ️ Paths in <code>index.php</code> are already configured or custom.</p>";
    }
} else {
    echo "<p class='status-err'>❌ Error: <code>index.php</code> not found in current directory.</p>";
}

echo "<h2>🚀 Verifying Site Health</h2>";

// Test bootstrap file loading
$bootstrap_file = $backend_path_normalized . '/app/bootstrap.php';
if (file_exists($bootstrap_file)) {
    echo "<p class='status-ok'>✅ app/bootstrap.php exists.</p>";
} else {
    echo "<p class='status-err'>❌ app/bootstrap.php is missing in backend directory.</p>";
}

// Test web.php routes file loading
$web_file = $backend_path_normalized . '/routes/web.php';
if (file_exists($web_file)) {
    echo "<p class='status-ok'>✅ routes/web.php exists.</p>";
} else {
    echo "<p class='status-err'>❌ routes/web.php is missing in backend directory.</p>";
}

echo "<hr/>";
echo "<h3 class='status-ok'>🎉 Project Successfully Configured!</h3>";
echo "<p>Your custom PHP app is now linked and optimized for cPanel.</p>";
echo "<p style='color: #718096; font-size: 13px;'>Backend Path: <code>" . htmlspecialchars($backend_path_normalized) . "</code><br/>Web Root Path: <code>" . htmlspecialchars(__DIR__) . "</code></p>";

echo "<p style='background-color: #fffaf0; border-left: 4px solid #dd6b20; padding: 12px;'>";
echo "<strong>🚨 IMPORTANT SECURITY NOTICE:</strong> Delete this file (<code>cpanel-deploy.php</code>) immediately from your cPanel File Manager to prevent others from inspecting your files.";
echo "</p>";

echo "</div></body></html>";
?>
