<?php

header('Access-Control-Allow-Origin: *');

if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'HEAD') {
    header('Status: 405');
    header('Content-Length: 0');
    header('Allow: GET, HEAD');
    exit;
}

function jenc($data): string {
    return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}

$info = $_SERVER['PATH_INFO'];
if ($info !== '') {
    header('Status: 404');
    header('Content-Length: 0');
    exit;
}

header('Content-Type: application/json; charset=UTF-8');

echo "{\"data\":[\n";
passthru(<<<EOF
curl -s 'https://lkv.at/at/zertifizierung/themen/BIO/zertifizierte-BIO-Betriebe.php' | grep '<tr' -A 3 \
| sed 's@<tr>@  {@;s@</tr>@  },@;s@<td><a .*href="\([^"]*\)".*>\(.*\)</a></td>@    "certUrl":"\\1","certNr":"\\2",@;s@<td>\(.*\)<br/>\(.*\)<br/>\([0-9]*\) \(.*\)</td>@    "name":"\\1","address":"\\2","postalCode":"\\3","city":"\\4"@;s@<td>\(.*\)<br/>\(.*\)</td>@    "name":"\\1","type":"\\2"@;\$s/.$//' \
| sed 's@"certUrl":"\(/[^"]*\)"@"certUrl":"https://lkv.at\\1"@' \
| sed 's@\s*",@",@g;s@:"\s*@:"@g'
EOF);

echo "]}\n";
