<?php
// CLI upgrade runner that simulates OpenCart's install/upgrade flow

define('VERSION', '2.3.0.2');
define('DIR_OPENCART', '/var/www/html/');
define('DIR_APPLICATION', DIR_OPENCART . 'install/');
define('DIR_SYSTEM', DIR_OPENCART . 'system/');
define('DIR_STORAGE', DIR_SYSTEM . 'storage/');
define('DIR_DATABASE', DIR_SYSTEM . 'database/');
define('DIR_LANGUAGE', DIR_APPLICATION . 'language/');
define('DIR_TEMPLATE', DIR_APPLICATION . 'view/template/');
define('DIR_CONFIG', DIR_SYSTEM . 'config/');
define('DIR_CACHE', DIR_STORAGE . 'cache/');
define('DIR_DOWNLOAD', DIR_STORAGE . 'download/');
define('DIR_LOGS', DIR_STORAGE . 'logs/');
define('DIR_MODIFICATION', DIR_STORAGE . 'modification/');
define('DIR_UPLOAD', DIR_STORAGE . 'upload/');

define('DB_DRIVER', 'mysqli');
define('DB_HOSTNAME', 'db');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'rootpw');
define('DB_DATABASE', 'genarom_db_23');
define('DB_PORT', '3306');
define('DB_PREFIX', 'hk_');

require_once(DIR_SYSTEM . 'startup.php');

// Registry
$registry = new Registry();

// Config
$config = new Config();
$registry->set('config', $config);

// DB
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT);
$registry->set('db', $db);

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Request
$registry->set('request', new Request());

// Response
$response = new Response();
$response->addHeader('Content-Type: text/plain');
$registry->set('response', $response);

for ($i = 1000; $i <= 1008; $i++) {
    $file = DIR_APPLICATION . 'model/upgrade/' . $i . '.php';
    if (!file_exists($file)) {
        echo "SKIP $i (no file)\n";
        continue;
    }
    echo "==== $i ====\n";
    try {
        $loader->model('upgrade/' . $i);
        $prop = 'model_upgrade_' . $i;
        $registry->get('load')->model('upgrade/' . $i);
        $model = $registry->get('model_upgrade_' . $i);
        if (!$model) {
            echo "FAIL: cannot load model_upgrade_$i\n";
            continue;
        }
        $model->upgrade();
        echo "OK $i\n";
    } catch (Throwable $e) {
        echo "EXCEPTION $i: " . $e->getMessage() . "\n";
    }
}
