#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-o","--output", dest="outputfile",
                  help="Write python file FILE",metavar="FILE")
parser.add_option("-s", "--dontrun",
                  action="store_false", dest="run", default=True,
                  help="don't run the script")

(options, args) = parser.parse_args()
def getAmount(string):
	i = 0
	count = 0
	while(i < len(string)):
		if(string[i] == " "):
			count+=1
		else:
			return count
		i+=1
result = list()
intend_str = "\t"
intend = 0
no_intend = list()
file = args[0]
file_dir = os.path.split(file)[0]
if file_dir:
    os.chdir(file_dir)
file_object = open(file)
file_lines = file_object.readlines()
for line in file_lines:
    no_intend.append(line[getAmount(line):])
line_nr = 1
line_int = []
for line in no_intend:
    intend_brf = intend
    line_int.append(intend_brf)
    plu_int = [1 for elem in line if elem == "{"]
    min_int = [1 for elem in line if elem == "}"]
    #print plu_int
    #print min_int
    for elem in plu_int:
        intend+=elem
    for elem in min_int:
        if intend == 0:
            print "ERROR on line %d: No intend to close" % line_nr
            sys.exit(1)
        intend-=elem
    line = line.replace("{",":")
    line = line.replace("}","")
    result.append("%s%s" % (intend_brf*intend_str,line))
    line_nr+=1
result_str = ''
for line in result:
    c_line = line.replace("\t","")
    c_line = c_line.replace("\n","")
    if c_line:
        result_str = ("%s%s" % (result_str, line))
if options.outputfile:
    outp = open(options.outputfile, "w")
    outp.write(result_str)
    outp.close()
if options.run:
    namespace = {"__name__":"__main__"}
    exec result_str in namespace