#! /usr/local/bin/python

# force files into Unix EOL format (\n)
# please, no mac format files!

import sys
import os

def usage ():
	print 'tounix.py file [file...]'
	sys.exit()

if len(sys.argv) < 2:
	usage()
	
files = sys.argv[1:]

for x in files:
	fin = open(x, 'r')
	outname = 'tmp.'+str(os.getpid())
	fout = open(outname, 'wb')
	print x+' -> unix'
	while 1:
		line = fin.readline()
		if line == '':
			break
		# single blank unix-format line
		if len(line) == 1:
			fout.write ('\n')
		# line in dos format.
		elif line[-2:] == '\r\n':
			fout.write(line[:-2]+'\n')
		# already unix-format
		else:
			fout.write(line)
	fin.close()
	fout.close()
	os.unlink (x)
	os.rename (outname,x)
	
