/*
 * Main.java
 *
 * Copyright (c) 2007  Vreixo Formoso
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See COPYING file for details.
 */

package org.pykix.libburnia.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.EnumSet;

import org.pykix.libburnia.libburn.BurnSource;
import org.pykix.libburnia.libburn.Source;
import org.pykix.libburnia.libisofs.Ecma119ExtensionFlag;
import org.pykix.libburnia.libisofs.IsoTreeNode;
import org.pykix.libburnia.libisofs.IsoVolSet;
import org.pykix.libburnia.libisofs.IsoVolume;

public class IsoFsMain {

	private static void usage() {
		System.out.println("Main [OPTIONS] DIRECTORY OUTPUT");
	}
	
	private static void help()
	{
		System.out.println(
			"Options:\n" +
			"  -J       Add Joliet support\n" +
			"  -R       Add Rock Ridge support\n" +
			"  -L <num> Set the ISO level (1 or 2)\n" +
			"  -h       Print this message"
		);
	}

	private static void invalidOption(String opt) {
		System.out.println("Invalid option: " + opt +
				". Try Main -h for help");
		usage();	
	}
	
	public static void main(String[] args) throws IOException  {

		EnumSet<Ecma119ExtensionFlag> flags = 
			EnumSet.noneOf(Ecma119ExtensionFlag.class);
		int level = 1;
		
		/* parse cmd line. */
		int i = 0;
		while ( i < args.length ) {
			String arg = args[i];
			
			if ( arg.startsWith("-") ) {
				
				if (arg.length() > 2) {
					invalidOption(arg);
					System.exit(1);
				}
				
				char opt = arg.charAt(1);
				
				switch (opt) {
				case 'h':
					usage();
					help();
					System.exit(0);
					break;
				case 'J':
					flags.add(Ecma119ExtensionFlag.JOLIET);
					break;
				case 'R':
					flags.add(Ecma119ExtensionFlag.ROCKRIDGE);
					break;
				case 'L':
					/* get level value */
					if ( ++i < args.length ) {
						level = Integer.parseInt(args[i]);
					} else {
						System.out.println("Level number needed");
						usage();
						help();
						System.exit(1);
						break;
					}
					break;
				default:
					invalidOption(arg);
					System.exit(1);
					break;
				}
				i++;
			} else {
				break; /* exit loop, no more options */
			}
		}
		
		
		if (args.length < i + 1) {
			System.out.println("Please pass directory from which to build ISO");
			usage();
			System.exit(1);
		}
		
		if (args.length < i + 2) {
			System.out.println("Please supply output file");
			usage();
			System.exit(1);
		}
			
		FileOutputStream f = null;
		try {
			f = new FileOutputStream(args[i+1]);
		} catch (FileNotFoundException e) {
			System.out.println("error opening output file");
			System.exit(1);
		}
		
		File dir = new File(args[i]);
		IsoTreeNode root = null;
		try {
			root = IsoTreeNode.raddDir(null, dir);
		} catch (FileNotFoundException e) {
			System.out.println("Input directory not found");
			System.exit(1);
		}
		
		if ( root == null ) {
			System.out.println("Error opening input directory.");
			System.exit(1);
		}
		
		IsoVolume volume = new IsoVolume("VOLID", "PUBID", "PREPID", root);
		IsoVolSet volset = new IsoVolSet( volume, "VOLSETID" );
		BurnSource src = new Source(volset, 0, level, flags);

		byte [] buf = new byte[2048];
		while ( src.read(buf, 2048) == 2048 ) {
			f.write(buf);
		}
		f.close();
	}

}
