<?php
$__req = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH);
if (!is_string($__req) || $__req === '') {
    $__req = '/';
}
if (preg_match('#/index\.html$#i', $__req)) {
    $dest = preg_replace('#/index\.html$#i', '/', $__req);
    if (!is_string($dest) || $dest === '') {
        $dest = '/';
    }
    header('Location: ' . $dest, true, 301);
    exit;
}

function serve_static_seo() {
    $uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
    $types = array(
        '/robots.txt' => 'text/plain; charset=UTF-8',
        '/sitemap.xml' => 'application/xml; charset=UTF-8',
    );
    if (isset($types[$uri])) {
        $path = __DIR__ . $uri;
        if (is_file($path) && filesize($path) > 0) {
            header('Content-Type: ' . $types[$uri]);
            readfile($path);
            return true;
        }
        http_response_code(404);
        header('Content-Type: text/plain; charset=UTF-8');
        echo $uri === '/sitemap.xml' ? 'Sitemap not found.' : 'Robots not found.';
        return true;
    }
    if (preg_match('#^/google[a-f0-9]+\.html$#i', $uri)) {
        $path = __DIR__ . $uri;
        if (is_file($path) && filesize($path) > 0) {
            header('Content-Type: text/html; charset=UTF-8');
            header('X-Robots-Tag: noindex, nofollow');
            readfile($path);
            return true;
        }
    }
    return false;
}

if (serve_static_seo()) {
    exit;
}

$__norm = rtrim($__req, '/');
if ($__norm === '') {
    $__norm = '/';
}
if ($__norm !== '/' && $__norm !== '/index.php') {
    http_response_code(404);
    header('Content-Type: text/plain; charset=UTF-8');
    echo 'Not found.';
    exit;
}

$lp = __DIR__ . '/index.html';
if (is_file($lp) && filesize($lp) > 256) {
    header('Content-Type: text/html; charset=UTF-8');
    readfile($lp);
    exit;
}
http_response_code(503);
header('Content-Type: text/plain; charset=UTF-8');
echo 'Landing page missing (index.html).';
