<?php
header('Content-Type: text/plain');
$ph   = '/home/nhimexco/public_html/';
$dest = '/home/nhimexco/old-nhimex-site-backup/';
$keep = ['.', '..', '.well-known', 'nhx-diag.php'];
$go = isset($_GET['go']) && $_GET['go'] === '1';
echo $go ? "MODE: APPLY\n\n" : "MODE: DRY-RUN (nothing moved)\n\n";

$items = [];
foreach (scandir($ph) as $it) if (!in_array($it, $keep, true)) $items[] = $it;
echo "Will move " . count($items) . " items to $dest\n";
foreach ($items as $it) echo '  ' . (is_dir($ph . $it) ? '[dir]  ' : '[file] ') . $it . "\n";
echo "\nStays in public_html: .well-known, nhx-diag.php\n";

if ($go) {
    if (!is_dir($dest)) mkdir($dest, 0755, true);
    $ok = 0; $fail = 0;
    foreach ($items as $it) {
        if (file_exists($dest . $it)) { echo "SKIP (exists): $it\n"; $fail++; continue; }
        if (@rename($ph . $it, $dest . $it)) $ok++; else { echo "FAILED: $it\n"; $fail++; }
    }
    // fallback copy of this script, so it stays reachable after the switch
    @copy(__FILE__, '/home/nhimexco/test.nhimex.com/nhx-diag.php');
    echo "\nMoved: $ok, failed/skipped: $fail\n";
    echo "NOW do step C in cPanel (Node.js app -> Application URL = nhimex.com).\n";
    echo "To roll back: move everything from old-nhimex-site-backup back into public_html.\n";
}