#!/usr/bin/python
import os
import sys
import optparse
import re
from formatting import seng265_formatter

def main():
    s = """?pgwdth 50
?mrgn 15
Call me Ishmael. Some years ago--never mind how long precisely--having
little or no money in my purse, and nothing particular to interest me on
?mrgn +5
shore, I thought I would sail about a little and see the watery part of
the world. It is a way I have of driving off the spleen and regulating
the circulation. Whenever I find myself growing grim about the mouth;
?mrgn +5
whenever it is a damp, drizzly November in my soul; whenever I find
myself involuntarily pausing before coffin warehouses, and bringing up
?mrgn +5
the rear of every funeral I meet; and especially whenever my hypos get
such an upper hand of me, that it requires a strong moral principle to
?mrgn +5
prevent me from deliberately stepping into the street, and methodically
knocking people's hats off--then, I account it high time to get to sea
as soon as I can. This is my substitute for pistol and ball. With a
?mrgn +5
philosophical flourish Cato throws himself upon his sword; I quietly
?mrgn +5
take to the ship. There is nothing surprising in this. If they but knew
it, almost all men in their degree, some time or other, cherish very
nearly the same feelings towards the ocean with me.

There now is your insular city of the Manhattoes, belted round by
?mrgn +5
wharves as Indian isles by coral reefs--commerce surrounds it with
her surf. Right and left, the streets take you waterward. Its extreme
downtown is the battery, where that noble mole is washed by waves, and
?mrgn +5
cooled by breezes, which a few hours previous were out of sight of land.
Look at the crowds of water-gazers there."""
    fp_exist = 0 #variable for checking if tempwrite.txt exists
    fp = ""
    if(len(sys.argv)>1):
        fp = sys.argv[1]
        if(re.match(r"\.txt$",fp)):
            pass
    else:
        process_stdin()     
        fp_exist = 1
        fp = "tempwrite.txt"
        if(check_if_empty(fp)):
            fp = s.splitlines()
            fp_exist = 0
    f = seng265_formatter(fp)
    f = f.get_lines()
    
    for x in f:
        print x,
    if(fp_exist!=0):
        os.remove("tempwrite.txt")
        fp_exist=0
    
def process_stdin():
    """No file name was input, so have to accept text from console"""
    
    fpout = open("tempwrite.txt",'w')
    x = raw_input()
    while(x != "-1"):
        fpout.write(x+"\n")
        x = raw_input()
    fpout.close()
    return None

def check_if_empty(fp):
    filename = open(fp,'r')
    filename.seek(0)
    first_char = filename.read(1)
    if not first_char:
        return True
    else:
        return False

if __name__ == "__main__":
    main()
    
    
