<?php

if (isset($_GET["count"])) { // get number of taglines
    header("Content-Type: text/plain");
    $tags = new SplFileObject("../taglines.txt", "r");
    $tags->seek(PHP_INT_MAX);
    echo $tags->key();
}

elseif (isset($_GET["key"])) { // get specified tagline (with bounds checking)
    header("Content-Type: text/plain");
    $tags = file("../taglines.txt");
    $i = intval($_GET["key"]);
    if (array_key_exists($i, $tags))
        echo $tags[$i];
    else
        die("$i not found");
}

elseif (isset($_GET["fonts"])) { // get list of fonts
    header("Content-Type: application/json");
    echo json_encode(array_map(function ($f) {
        return basename($f, ".flf");
    }, glob("fonts/*.flf")));
}

elseif (isset($_GET["text"])) { // run figlet
    header("Content-Type: text/plain");
    echo shell_exec(
        "/usr/bin/figlet -d fonts -w 76 -f " .
        escapeshellarg($_GET["font"] ?? "standard") . " -- " .
        escapeshellarg($_GET["text"])
    );
}

else { // get a random tagline
    header("Content-Type: text/plain");
    $tags = file("../taglines.txt");
    echo $tags[array_rand($tags)];
}

