import java.io.*;

/**
 * LineEndConvert.
 * Converts the line endings of a text file to the line endings required for the OS platform.
 * For example, in DOS lines are ended with the "\r\n" characters, for the Macintosh
 * it is the '\r' character, while for Unix it is the '\n' character.
 * This utility can also convert all line endings to the HTML '<br>' tag.
 *
 *	Copyright (C) 2003  Shazron Abdullah
 *	  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *  
 * @author Shazron Abdullah
 * @date 2003/01/27
 *
 **/
public class LineEndConvert
{
	private static String EOL_MAC = "\r";
	private static String EOL_DOS = "\r\n";
	private static String EOL_UNIX = "\n";
	private static String EOL_BR = "<br>";
	
	private static String FLAG_MAC = "-mac";
	private static String FLAG_DOS = "-dos";
	private static String FLAG_UNIX = "-unix";
	private static String FLAG_BR = "-br";
	
	private static String CONVERT_SUFFIX = ".conv";
	
	/** ***********************************************************************
	 *
	 * Converts the line endings of the file specified by the filepath, to 
	 * the new line endings specified
	 *
	 *************************************************************************/
	private static void convertFile(String filepath, String newEOL)
	{
		BufferedReader in;
		PrintWriter out;
		String tmp;

		try 
		{
			in = new BufferedReader(new FileReader(filepath));
			out = new PrintWriter(new BufferedWriter(new FileWriter(filepath + CONVERT_SUFFIX )));
			
			while((tmp=in.readLine()) != null) 
			{
				out.print(tmp);
				out.print(newEOL);
			}
			
			in.close();
			out.flush();
			out.close();
			
		} catch (Throwable t) {
			throw new RuntimeException(t.getMessage());
		}
	} 

	/** ***********************************************************************
	 *
	 * Main function for the program
	 *
	 *************************************************************************/
	public static void main(String args[])
	{
		if (args.length < 1) {
			System.out.println("usage: LineEndConvert <filename> [flags]");
			System.out.println("\tflags:");
			System.out.println("\t-mac\t convert the eol chars in the file to Mac eol");
			System.out.println("\t-dos\t convert the eol chars in the file to DOS/Win32 eol");
			System.out.println("\t-unix\t convert the eol chars in the file to Unix eol");
			System.exit(1);
		}
		
		// look for flag -mac, -dos or -unix
		// if none, use current system EOL line.separator env variable
		// if more than one, the FIRST one specified is used
		
		String eol = System.getProperty("line.separator");
		if (args.length > 1) // there are flags
		{ 
			for (int i=1; i < args.length; i++) {
				if (args[i].equalsIgnoreCase(FLAG_MAC)) {
					eol = EOL_MAC;
					break;
				} else if (args[i].equalsIgnoreCase(FLAG_DOS)) {
					eol = EOL_DOS;
					break;
				} else if (args[i].equalsIgnoreCase(FLAG_UNIX)) {
					eol = EOL_UNIX;
					break;
				} else if (args[i].equalsIgnoreCase(FLAG_BR)) {
					eol = EOL_BR;
					break;
				}
			}
		}
		convertFile(args[0], eol);
	}
}