/*Deterministically generate a long Lorem-Ipsum based text for tests.
 * Usage: ./gen-lorem-text [repeats] > test_text_lorem.txt
 *
 * What This program does:
 * - Uses a fixed publicly available Lorem paragraph.
 * - Modifies some of the characters into special characters, i.e. "äöüßçáàñ...", based on fixed modification table. 
 * - Modified paragraph is repeated N times separated by blank lines.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

static const char *base_paragraph =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ac "
"ligula id neque pulvinar interdum. Integer nec urna sit amet risus "
"mollis dictum. Vivamus quis quam in est gravida pharetra. Fusce "
"viverra, neque non commodo tempus, risus eros posuere lorem, eget "
"vestibulum purus odio sit amet lacus. Donec vitae nisl at arcu "
"vehicula faucibus. Cras tincidunt, nibh quis aliquet tincidunt, "
"lectus dolor iaculis nunc, id fermentum dui orci at urna.";

/* Deterministic modifications: we replace selected ASCII characters
 * withspecial UTF-8 variants. The transformation depends on
 * the character and the global position index so it's reproducible.
 */
static const char *replace_char(unsigned char c, unsigned long pos) {
    /* Replace only a subset of letters to keep readability. */
    /* Use pos to vary replacements deterministically. */
    switch (c) {
        case 'a':
            if ((pos % 5) == 0) return "ä";
            if ((pos % 8) == 0) return "á";
            return "a";
        case 'o':
            if ((pos % 12) == 0) return "ö";
            if ((pos % 7) == 0) return "ò";
            return "o";
        case 'u':
            if ((pos % 13) == 0) return "ü";
            return "u";
        case 'b':
            if ((pos % 17) == 0) return "ß";
            return "b";
        case 'c':
            if ((pos % 19) == 0) return "ç";
            return "c";
        case 'n':
            if ((pos % 23) == 0) return "ñ";
            return "n";
        case 'e':
            if ((pos % 29) == 0) return "é";
            return "e";
        case 'i':
            if ((pos % 31) == 0) return "í";
            return "i";
        default:
            /* To emit the original byte */
            return NULL;
    }
}

int main(int argc, char *argv[]) {
    unsigned long repeats = 150; /* default: ~150 paragraphs -> multi-page */
    unsigned long i;
    unsigned long global_pos = 1;

    if (argc > 1) {
        char *end;
        unsigned long v = strtoul(argv[1], &end, 10);
        if (end != argv[1] && v > 0) repeats = v;
    }
    printf("/* Generated by gen-lorem-text.c: repeats=%lu */\n\n", repeats);

    for (i = 0; i < repeats; ++i) {
        /* iterate bytes of base_paragraph and print with replacements */
        const char *p = base_paragraph;
        while (*p) {
            unsigned char uc = (unsigned char)*p;
            const char *rep = replace_char(uc, global_pos);
            if (rep) {
                fputs(rep, stdout);  /* rep is UTF-8 literal */
            } else {
                /* print ASCII byte as-is */
                fputc(uc, stdout);
            }
            ++p;
            ++global_pos;
        }
        /* Paragraph separator: two newlines -> makes pages when converted */
        fputs("\n\n", stdout);
    }

    /* trailing newline */
    fputs("\n", stdout);

    return 0;
}