<?php
// Intended to run hourly. The backup service itself decides whether the configured slot is due.
require_once dirname(__DIR__) . '/db_bootstrap.php';
require_once dirname(__DIR__) . '/backup_lib.php';

$conn = ensure_db(true);
ua_backup_ensure_schema($conn);
$settings = ua_backup_settings($conn);
$force = in_array('--force', $argv ?? [], true);
if (!$force && !ua_backup_is_due($settings)) {
  echo json_encode(['ok'=>true,'due'=>false,'next'=>($n=ua_backup_next_run($settings))?$n->format('Y-m-d H:i:s'):null],JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) . PHP_EOL;
  exit(0);
}
$result = ua_backup_run($conn,$force?'manual':'scheduled');
echo json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT) . PHP_EOL;
exit(!empty($result['ok'])?0:1);

