<?php

if (php_sapi_name() !== 'cli') {
    exit(1);
}

$sessionPath = dirname(__DIR__) . '/tmp_sessions';
if (!is_dir($sessionPath)) {
    @mkdir($sessionPath, 0777, true);
}
session_save_path($sessionPath);

require_once dirname(__DIR__) . '/config.php';
require_once dirname(__DIR__) . '/lib/exam_generation_service.php';

$jobId = null;
foreach ($argv as $argument) {
    if (str_starts_with($argument, '--job=')) {
        $jobId = (int) substr($argument, 6);
        break;
    }
}

if (!$jobId) {
    fwrite(STDERR, "Missing --job argument\n");
    exit(1);
}

$result = processExamGenerationJob($jobId);
exit(!empty($result['success']) ? 0 : 1);

