#!/bin/sh
PYFIGLET='./pyfiglet'
OUTPUT='/tmp/pyfiglet_example.log'
TEXT='pyfiglet'
LOG=0

cd "$(dirname "$0")"
if [ ! -f "$PYFIGLET" ]; then
    echo "Error ! can't find $PYFIGLET!"
fi

print_samples() {
    if [ "$LOG" -eq 1 ]; then
        output="$OUTPUT"
        rm "$output" 2>/dev/null
    else
        output='/dev/null'
    fi

    #. strip \' \, \[ \] from font lists of pyfiglet.
    fonts="$("$PYFIGLET" -l | sed -e "s/[',]//g" -e 's/^\[//g' -e 's/\]$//g')"

    for font in $fonts; do
        echo "$font" | tee -a "$output"
        "$PYFIGLET" "$TEXT" -f "$font" | tee -a "$output"
    done
}

useage() {
    echo "Useage: $0 [options] [text ...]\n"
    echo "Options:"
    echo "-l\t\t\tcreate log file."
    echo "-o [path_to_file]\tspecify the path of log file,\n\t\t\tdefault is $OUTPUT."
    echo "-h\t\t\tshow help messages."
    exit 0
}

#. parse apts & args
while getopts ":lo:h" opt; do
    case "$opt" in
        l)
            LOG=1
            ;;
        o)
            if [ -n $OPTARG ]; then
                OUTPUT="$OPTARG"
            fi
            ;;
        h|\?)
            useage
            ;;
    esac
done
shift `expr $OPTIND - 1`
if [ ! -z "$1" ]; then
    TEXT="$@"
fi

#. main 
print_samples

