#!/usr/local/cpython-3.4/bin/python3

# pylint: disable=superfluous-parens
# superfluous-parens: parentheses are good for clarity and portability

'''
Create a spelling test (with read-aloud prompts) from a series
of spelling words read from stdin.  Output is on stdout.
'''

import sys


def make_intelligible(text):
    '''Add some punctuation to a line, to make it sound better in festival'''
    return '. {} . . . . {} . .'.format(text, text)


def usage(retval):
    '''Output a usage message and exit appropriately'''
    sys.stderr.write('Usage: {} --definitions\n'.format(sys.argv[0]))
    sys.exit(retval)


def main():
    '''Main function'''
    definitions = False
    while sys.argv[1:]:
        if sys.argv[1] in ('-h', '--help'):
            usage(0)
        elif sys.argv[1] == '--definitions':
            definitions = True
        else:
            sys.stderr.write('Unrecognized option: {}\n'.format(sys.argv[1]))
            usage(1)
        del sys.argv[1]
    print('produce_speech')
    lines = [string.strip() for string in sys.stdin.readlines()]

    lines2 = []
    for line in lines:
        if definitions and '-' in line:
            subline = line.split('-')[0].strip()
        else:
            subline = line
        lines2.append('{}\n-\n{}\n'.format(make_intelligible(line), subline))

    print('%%\n'.join(lines2))

main()