<?php
/**
 * Sembrado post-copia: deja el respaldo listo para /install/ (sin usuarios ni instancia).
 * Uso: php backup_installer_mode_seed.php "RUTA_DESTINO_RESPALDO"
 */
declare(strict_types=1);

if ($argc < 2) {
    fwrite(STDERR, "Uso: php backup_installer_mode_seed.php DEST\n");
    exit(1);
}

$dest = realpath($argv[1]);
if ($dest === false || !is_dir($dest)) {
    fwrite(STDERR, "Destino invalido.\n");
    exit(1);
}

define('GRID_ROOT', $dest);
define('ANTARA_ROOT', $dest);
chdir($dest);

require $dest . '/includes/install_public.php';
require $dest . '/includes/install_checks.php';

$ds = DIRECTORY_SEPARATOR;

$removeRecursive = static function (string $path) use (&$removeRecursive, $ds): void {
    if (is_file($path) || is_link($path)) {
        @unlink($path);
        return;
    }
    if (!is_dir($path)) {
        return;
    }
    $items = @scandir($path);
    if (!is_array($items)) {
        return;
    }
    foreach ($items as $it) {
        if ($it === '.' || $it === '..') {
            continue;
        }
        $removeRecursive($path . $ds . $it);
    }
    @rmdir($path);
};

$purgeNames = [
    'install.lock',
    'install_wizard_draft.json',
    'auth_config.enc',
    'users.enc',
    'webauthn_credentials.enc',
    'rate.enc',
    'flexia_instance.json',
    'flexia_format_count.json',
    'flexia_traffic_stats.json',
    'external_secrets_basename.txt',
    'recovery.allow',
    '.master.key',
    'master.key',
    '.user.ini',
    '.share_secret.key',
    'settings.json.bak',
    'explorer_customization.json',
    'security.log',
    'activity.log',
];

foreach ($purgeNames as $name) {
    foreach ([
        $dest,
        $dest . $ds . 'storage' . $ds . 'config',
        $dest . $ds . 'storage' . $ds . 'secure',
        $dest . $ds . 'storage' . $ds . 'logs',
        $dest . $ds . 'config',
        $dest . $ds . 'logs',
        $dest . $ds . 'temp',
    ] as $dir) {
        $f = $dir . $ds . $name;
        if (is_file($f)) {
            @unlink($f);
        }
    }
}

foreach (glob($dest . $ds . 'data' . $ds . 'users.json.bak*') ?: [] as $bak) {
    @unlink($bak);
}

$userDirs = $dest . $ds . 'storage' . $ds . 'users';
if (is_dir($userDirs)) {
    $items = @scandir($userDirs);
    if (is_array($items)) {
        foreach ($items as $it) {
            if ($it === '.' || $it === '..' || $it === 'index.php' || $it === '.htaccess') {
                continue;
            }
            $removeRecursive($userDirs . $ds . $it);
        }
    }
}

$flexiaDisk = $dest . $ds . 'flexia disk';
if (is_dir($flexiaDisk)) {
    $removeRecursive($flexiaDisk);
}

$quarantine = $dest . $ds . 'storage' . $ds . 'secure' . $ds . 'quarantine';
if (is_dir($quarantine)) {
    $removeRecursive($quarantine);
}

$media = $dest . $ds . 'media';
if (is_dir($media)) {
    $removeRecursive($media);
    @mkdir($media, 0755, true);
}

$logos = $dest . $ds . 'logos_sinapsys';
if (is_dir($logos)) {
    $removeRecursive($logos);
    @mkdir($logos, 0755, true);
}

$reserved = $dest . $ds . 'arquitect-app' . $ds . 'reserved_projects';
if (is_dir($reserved)) {
    $removeRecursive($reserved);
    @mkdir($reserved, 0755, true);
}

antara_install_reset_workspace_json_for_new_install();
antara_install_remove_lock();
antara_install_prepare_hosting_layout();

$lock = AntaraPaths::installLockFile();
if (is_file($lock)) {
    fwrite(STDERR, "Quedo install.lock en destino.\n");
    exit(1);
}

echo "OK\n";
exit(0);
