<?php
$file = explode(PHP_EOL, file_get_contents("radon-page-4.md"));
// remove EOF_LINE
array_pop($file);

$sqlReadings = array();
$sqlNotes = array();

$yr; $mo; $dy;
foreach($file as $line) {
    if("2021" === substr($line, 0, 4)) {
        $yr = strtok($line, " ");
        $mo = strtok(" ");
        $dy = strtok(" ");
        //echo "{$yr} {$mo} {$dy}" . PHP_EOL;
        continue;
    }
    //echo "line: {$line}" . PHP_EOL; continue;
    $a = 100 * strtok($line, " ");
    $b = 100 * strtok(" ");
    $c = 100 * strtok(" ");
    $hour = strtok(" ");
    $notes = strtok("\n");
    //echo "{$a},{$b},{$c},{$hour}" . PHP_EOL . "    >{$notes}" . PHP_EOL;
    $seconds = strtotime("{$dy} {$mo} {$yr} {$hour}");
    $sqlReadings[] = "('A', {$seconds}, {$a})";
    $sqlReadings[] = "('C', {$seconds}, {$b})";
    $sqlReadings[] = "('B', {$seconds}, {$c})";
    if(0 == $c) array_pop($sqlReadings);
    if(false === $notes) continue;
    $sqlNotes[] = "('note', {$seconds}, '{$notes}')";
}

$sqlReadings = "INSERT INTO radonLog (id, time, reading) VALUES " . PHP_EOL . implode("," . PHP_EOL, $sqlReadings) . ";";
$sqlNotes = "INSERT INTO locationLog (id, time, description) VALUES " . PHP_EOL . implode("," . PHP_EOL, $sqlNotes) . ";";

echo $sqlReadings;
//echo $sqlNotes;
