package edu.vt.marian.Document;

import java.io.*;
import java.net.*;
import java.util.*;

import edu.vt.marian.common.*;


/**
    Unit tester for MarcDocument (and thus MarcRecord) class.
    @author	Robert France
*/

public class KscToUnicode
{
	static private void showUsage()
	{
		System.err.println("usage:  java KscToUnicode -i <inputFile> -o <outputFile>");
	}
	

	public static void main(String argv[]) throws Exception
    {
        int Err;
        // Debug debug = new Debug("debug.conf", "trace.log", false);
		String inFile = "input";
		String outFile = "output";
        
		for (int i=0; i<argv.length; i++)
		{
			if (argv[i].charAt(0) == '-')	// Flag.
			{
				switch(argv[i].charAt(1))
				{
				case 'o':	// Output file name.
					outFile = argv[++i];
					break;
				case 'i':
					inFile = argv[++i];
					break;
				default:
					showUsage();
					return;
				}
			}
			else
			{
					showUsage();
					return;
			}
		}
		
		FileInputStream   fis;
		InputStreamReader isr;
		BufferedReader    input = null;
		FileOutputStream   fos;
		OutputStreamWriter osw;
		BufferedWriter     output = null;

		// Open the files
		try {
			fis   = new FileInputStream( inFile );
			isr   = new InputStreamReader( fis, new String("EUC_KR") );  // EUC_KR: KSC-5601 code
			input = new BufferedReader( isr );
		} catch ( Exception e ) {
			System.err.println( "File '" + inFile + "' not opened properly: " + e.toString() );
			System.exit( 1 );
		}      
		try {
			fos   = new FileOutputStream( outFile );
			osw   = new OutputStreamWriter( fos, new String("UTF8") );
			output = new BufferedWriter( osw );
		} catch ( Exception e ) {
			System.err.println( "File '" + outFile + "' not opened properly: " + e.toString() );
			System.exit( 1 );
		}      

		// Read a char
		int retVal = 1;
		char ch[] = new char[1];
	
		try { while (retVal > 0)
		{
			retVal = input.read(ch, 0, 1);
			output.write(ch);
		} } catch ( IOException e ) {
			System.err.println( "Error during read or write file:\n   " + e.toString() );
			System.exit( 1 );
		}

		output.flush();
		System.err.println("Normal EOF.");
		return;
	}
}

