#!/usr/bin/php
<?php

require_once __DIR__ . '/config/init.php';


$jsonFile = __DIR__ . '/web_check.json';


if (!file_exists($jsonFile)) {
    echo "CRITICAL - JSON file not found\n";
    exit(2);
}

$data = json_decode(file_get_contents($jsonFile), true);
if (!$data) {
    echo "CRITICAL - Invalid JSON\n";
    exit(2);
}

$critical = [];
$warning = [];

foreach ($data as $site) {

    $days = ssl_days_left($site['ssl-expiry']);

    if ($days === null) continue;

    if ($days <= SSL_CRITICAL_DAYS) {
        $critical[] = $site['domain'] . " ($days days)";
    }
    elseif ($days <= SSL_WARNING_DAYS) {
        $warning[] = $site['domain'] . " ($days days)";
    }
}

if (!empty($critical)) {
    echo "CRITICAL - SSL expiring soon: " . implode(', ', $critical) . "\n";
    exit(2);
}

if (!empty($warning)) {
    echo "WARNING - SSL expiring: " . implode(', ', $warning) . "\n";
    exit(1);
}

echo "OK - No SSL expiring soon\n";
exit(0);
