<!DOCTYPE html>
<!--
    Copyright (C) 2023 Gustavo Calvo

    groff-web is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    groff-web is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar. If not, see <https://www.gnu.org/licenses/>.
-->
<html>

<?php
    # Generate an uid for each new document
    $uid   = exec("head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 10");
    # groff settings
    $groff = "/usr/bin/groff";
    $comp = "$groff -ketpG -mspdf -Tpdf tmp/$uid.ms > tmp/$uid.pdf";
    # In case you want to use an external
    # script to compile with groff:
    #$comp = "sh groff-compiler.sh";
?>

<head>
    <title>Groff Web</title>
    <link rel="shortcut icon" href="#">
    <style>
        * {
            background-color: #121212;
            font-family: monospace;
            text-align: center;
            margin: 4px auto;
            color: #928374;
        }

        h1,h2,h3,h4,h5 {
            color: #fe8019;
        }

        textarea,object,.columns {
            border-radius: 10px;
            border-width: 1px;
            max-width: 1300px;
            width: 100%;
            columns: 2;
        }

        @media only screen and (max-width: 800px) {
            .columns {
                columns: 1;
                width: 90%;
            }
        }

        /* codebox */
        textarea {
            background-color: #282828;
            text-align: left;
            color: #83a598;
            height: 734px;
        }

        /* pdf */
        object {
            height: 800px;
        }

        .button {
            border-radius: 10px;
            border-width: 1px;
            padding: 5px 30px;
            margin: 10px;
        }
    </style>
</head>

<?php
    exec("rm -rf tmp/*");
    exec("mkdir -p tmp/$uid");
    if($_POST['codebox']){
        $file_open = fopen("tmp/$uid.ms","w+");
        fwrite($file_open, $_POST['codebox']);
        fclose($file_open);
    }
    exec("$comp");
?>

<body>
    <h1>Groff Web</h1>
    <p><?php echo exec("$groff -v | head -n 1")?></p>

    <div class=columns>
        <div style="width: 100%"><form method="POST">
            <div><input type=submit value="Compile PDF" class=button></div>

            <textarea id="codebox" name="codebox"><?php
                $datalines = file ("tmp/$uid.ms");
                foreach ($datalines as $zz) {
                echo $zz; }
            ?></textarea>
        </form></div>

        <object type="application/pdf" data="/tmp/<?php echo $uid?>.pdf?#&navpanes=0">
                <p>PDF cannot be displayed.</p>
        </object>
    </div>

    <footer><p>
        Copyright (C) 2023 Gustavo Calvo |
        <a href="https://gitlab.com/tavo-wasd/groff-web" target="_blank" rel="noopener noreferrer">Source</a> |
        <a href="https://www.gnu.org/licenses/gpl-3.0.txt" target="_blank" rel="noopener noreferrer">GPL-3.0</a>
    </p></footer>
</body>

</html>