/***********************************************************************
 * 
 *  Copyright (C) 2005-2006 Novell, Inc. All Rights Reserved.
 *
 *  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; version 2.1
 *  of the License.
 *
 *  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
 *  Library 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, Novell, Inc.
 * 
 *  To contact Novell about this file by physical or electronic mail, 
 *  you may find current contact information at www.novell.com.
 * 
 ***********************************************************************/

using System;
using System.IO;
using System.Collections;

namespace VersionVDProj
{
	/// <summary>
	/// Summary description for ArgumentParser.
	/// </summary>
	public class ArgumentParser
	{
		public readonly Hashtable Options;
		public readonly string Command;
		public readonly FileInfo ProjectFile;

		public ArgumentParser( string[] args )
		{
			Options = new Hashtable();

			if ( args.Length < 2 )
				throw new Exception( "Wrong number of arguments" );

			Command = ParseCommand( args[0] );
			ProjectFile = ParseFileName( args[1] );

			for ( int i = 2; i < args.Length; i++ )
				ParseArgument( args[i] );
		}		

		private string ParseCommand( string command )
		{
			if ( command.Substring( 0, 1 ) != "-" )
				throw new Exception( string.Format( "Unrecognized command {0}", command ) );

			return command.Substring( 1 ).ToLower();
		}

		private FileInfo ParseFileName( string path )
		{
			FileInfo file = new FileInfo( path );
			if ( !file.Exists )
				throw new Exception( string.Format( "The file {0} could not be found", path ) );

			return file;
		}

		private void ParseArgument( string arg )
		{
			int eqPos = arg.IndexOf( '=', 0 );
			
			if ( eqPos == -1 )
				throw new Exception( string.Format( "The argument {0} is not in the format NAME=VALUE", arg ) );

			string name = arg.Substring( 0, eqPos );
			string val = arg.Substring( eqPos + 1 );

			Options.Add( name.ToLower(), val );
		}
	}
}