// Package

// Imports
import java.util.*;
import java.io.*;
import org.ini4j.*;
import org.ini4j.Ini.Section;

/**
 * INI file processor
 * <p>
 * For testing
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Keripo
 * @version	1.0 17-Dec-07
 */

public class IniProcessor {

	// Variables
	private static final String[] INILISTSTART = {"test.ini"};
	public static ArrayList<String> iniList;
	public static ArrayList<Pack> baseList, packsList, extrasList;

	public static void main(String[] args) {
		
		// Add initial
		for (int i = 0; i < INILISTSTART.length; i++) {
			iniList.add(INILISTSTART[i]);
		}
		
		// Process INI files
		for (String f: iniList) {
			try {
				processINI(f);
			} catch (Exception e) {
				System.err.println("Error: Exception handling file " + f);
				System.err.println(e.getMessage());
			}
		}

	}
	
	private static void processINI(String f)
	throws IOException, FileNotFoundException, InvalidIniFormatException, NumberFormatException {
		
		// Setup
		Ini ini = new Ini();
		ini.load(new FileInputStream(new File(f)));
		
		// Special section
		Section list = ini.get("list");
		
		// Tokenize the section names
		StringTokenizer base = new StringTokenizer(list.fetch("base"));
		StringTokenizer packs = new StringTokenizer(list.fetch("packs"));
		StringTokenizer extras = new StringTokenizer(list.fetch("extras"));
		StringTokenizer lists = new StringTokenizer(list.fetch("lists"));
		
		// Temporary ArrayLists
		ArrayList<Section> baseListTemp = new ArrayList<Section>();
		ArrayList<Section> packsListTemp = new ArrayList<Section>();
		ArrayList<Section> extrasListTemp = new ArrayList<Section>();
		ArrayList<String> iniListTemp = new ArrayList<String>();

		// Add to ArrayLists
		while (base.hasMoreTokens()) {
			baseListTemp.add(ini.get(base.nextToken()));
		}
		while (packs.hasMoreTokens()) {
			packsListTemp.add(ini.get(packs.nextToken()));
		}
		while (extras.hasMoreTokens()) {
			extrasListTemp.add(ini.get(extras.nextToken()));
		}
		while (lists.hasMoreTokens()) {
			iniListTemp.add(extras.nextToken());
		}
		
		// baseListTemp processing
		for (Section sec: baseListTemp) {
			Pack p = new Pack();
			// About
			p.name = sec.fetch("name");
			p.author = sec.fetch("author");
			p.category = sec.fetch("category");
			// Stats
			p.status = Integer.parseInt((sec.fetch("status")));
			p.name = sec.fetch("name");
			p.name = sec.fetch("name");
			p.name = sec.fetch("name");
			p.name = sec.fetch("name");
		}
		
		
		// iniListTemp processing
		for (String item: iniListTemp) {
			iniList.add(item);
		}
		
	}

}
