/*
 Eteria IRC Client, an RFC 1459 compliant client program written in Java.
 Copyright (C) 2000  Javier Kohen <jkohen at tough.com>
 
 This program 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.
 
 This program 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 General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 package ar.com.jkohen.util;
 
 import java.io.*;
 import java.util.Enumeration;
 import java.util.Properties;
 
 public class LocalConfiguration extends ConfigurationProperties
 {
 	public LocalConfiguration(String s)
 	{
 		read(s);
 	}
 	
 	public LocalConfiguration(String s, Properties defaults)
 	{
 		super(defaults);
 		read(s);
 	}
 	
 	private void read(String name)
 	{
 		InputStream stream = null;
 		
 		if (name != null && name.length() > 0)
 		{
 			try
 			{
 				stream =  new FileInputStream(name);
 				properties.load(stream);
 				stream.close();
 			}
 			catch (FileNotFoundException ex)
 			{
 				create(name);
 			}
 			catch (IOException ex)
 			{
 				ex.printStackTrace();
 			}
 		}
 	}
 	
 	private void create(String name)
 	{
 		try
 		{
 			/*
 			** Try to create the specified directory if it doesn't exist.
 			** i.e. $HOME/.dir/user.properties or %APPDATA%\\Dir\\user.properties
 			*/
 			String s = new File(name).getParent();
 			if (s != null)
 			{
 				File parent = new File(s);
 				if (parent != null && !parent.exists())
 					parent.mkdir();
 			}
 
 			/* Save default configuration */
 			write(name);
 		}
 		catch (Exception ex)
 		{
 			System.err.println("Error creating user configuration : " + ex);
 		}
 	}
 
 	public void write(String s) throws IOException
 	{
 		OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(s), "ISO-8859-1");
 		String crlf = System.getProperty("line.separator");
 		
 		for (Enumeration e = keys(); e.hasMoreElements();)
 		{
 			String field = (String) e.nextElement();
 			String value = (String) get(field);
 
 			if (field != null)
 			{
 				out.write(field + "=");
 
 				if (value != null)
 				{
 					char chars[] = value.toCharArray();
 					char c = 0;
 					for (int i = 0; i < chars.length; i++)
 					{
 						c = chars[i];
 					
 						if (c == 0x5c)
 						{
 							/* Escape a backslash */
 							out.write("\\\\");
 						}
 						else if (c >= 0x20 && c <= 0xFF)
 						{
 							out.write(c);
 						}
 						else
 						{
 							/* Escape a Unicode char */
 							out.write("\\u");
 							String hex = Integer.toHexString((int) c);
 							for (int j = 0; j < 4 - hex.length(); j++)
 								out.write('0');
 						
 							out.write(hex);
 						}
 					}
 				}
 				
 				out.write(crlf);
 			}
 		}
 		out.close();
 	}
 }