#!/usr/bin/env sh
# Output paragraphs of lorem ipsum placeholder text
#
# Usage:
#
#   lorem
#   lorem 5
#   lorem 5 | fmt
#   lorem 5 0

while getopts h opt; do
    case $opt in
    h) awk 'NR == 1 { next } /^$/ { exit } { print substr($0, 3) }' "$0"
       exit ;;
    esac
done
shift $(( OPTIND - 1 ))

paragraphs="${1:-1}"
preamble_show="${2:-1}"

_lorem_base() {
    printf '%s\n' \
        consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore\
        et dolore magna aliqua ut enim ad minim veniam quis nostrud\
        exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat\
        duis aute irure dolor in reprehenderit in voluptate velit esse cillum\
        dolore eu fugiat nulla pariatur excepteur sint occaecat cupidatat non\
        proident sunt in culpa qui officia deserunt mollit anim id est laborum
}

while test "$paragraphs" -gt 0; do
    _sentences=$(shuf -i 4-6 -n 1)

    while test "$_sentences" -gt 0; do
        _lorem_base \
            | {
                if [ "$preamble_show" -eq 1 ]; then
                    printf '%s\n' lorem ipsum dolor sit amet
                    _words=$(shuf -i 4-6 -n 1)
                else
                    _words=$(shuf -i 4-10 -n 1)
                fi
                shuf -n "$_words";
            } \
            | xe -N0 echos \
            | awk '{ print toupper(substr($0, 1, 1)) substr($0, 2) "." }'

        _sentences=$(( _sentences - 1 ))
        preamble_show=0
    done | xe -N0 echos

    paragraphs=$(( paragraphs - 1 ))
    preamble_show=0
    if test "$paragraphs" -gt 0; then printf '\n'; fi
done
