#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

import sys
import time

# Este ejemplo muestra como destripar texto.
if len(sys.argv) < 2:
	print "usage: %s <file>" % sys.argv[0]
	sys.exit(1)

in_file = sys.argv[1]

try:
	file = open(in_file, "r").read()
except IOError, e:
	print e

else:
	print "\n1. Show file "
	print "2. Number of lines and words"
	print "3. Replace words"
	print "4. Exit"
	while True:
		print "\nType a number: "
		try:
			option = int( sys.stdin.readline() )	
	
			if ( option > 4 ):
				print "Invalid option\n"
	
			if ( option == 1 ):
				print file
	
			elif ( option == 2 ):
				total_lines = len( file.splitlines() )
				total_words = len( file.split() )
				print "The file have %d lines and %d words" % (total_lines, total_words)
	
			elif ( option == 3 ):
				rep_file = file.replace("u", "U").replace("h", "H")
				print rep_file
				date = time.strftime("%D - %H:%M:%S")
				last_modified = "Last modified %s" % date
				file_w = open(in_file, "w")
				file_w.write(rep_file)
				text = "Last modified"
				text_position = file.find(text)
				if not text in file:
					file_w.write("\n")
					file_w.write(last_modified)				
				else:
					file_w.seek(text_position)
					file_w.write(last_modified)				

			elif ( option == 4 ):
				break
	
		except ValueError:
			print "Invalid caracter \n"
