<?php
$lang = 'de';  // prefer german
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    foreach (explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $pref) {
        $l = substr($pref, 0, 2);
        if ($l === 'de') {
            $lang = 'de';
            break;  // force german
        } else if ($l === 'en') {
            $lang = 'en';  // use english only, if user specifically asks for it
        }
    }
}
header("Content-Length: 0");
switch ($_SERVER['PATH_INFO']) {
    case '':
    case '/':
        header("Status: 303");
        header("Location: /$lang/");
        break;
    case '/preview':
    case '/vorschau':
        header("Status: 303");
        header("Location: /$lang/" . ($lang === 'de' ? 'vorschau' : 'preview'));
        break;
    case '/changelog':
        header("Status: 303");
        header("Location: https://git.necronda.net/winzer/elwig/src/branch/main/CHANGELOG.md#changelog");
        break;
    case '/vcs':
    case '/git':
    case '/src':
    case '/source':
    case '/code':
        header("Status: 303");
        header("Location: https://git.necronda.net/winzer/");
        break;
    case '/access':
        header("Status: 303");
        header("Location: https://access.elwig.at/");
        break;
    case '/clients':
        header("Status: 303");
        header("Location: https://sync.elwig.at/");
        break;
    default:
        if (str_starts_with($_SERVER['PATH_INFO'], '/access/')) {
            header("Status: 308");
            header("Location: https://access.elwig.at/" . substr($_SERVER['PATH_INFO'], 8));
        } else if (str_starts_with($_SERVER['PATH_INFO'], '/clients/')) {
            header("Status: 308");
            header("Location: https://sync.elwig.at/" . substr($_SERVER['PATH_INFO'], 9));
        } else {
            header("Status: 404");
        }
}
