/* 
ZLib license:
Copyright (c) 2011 Dirk de la Hunt

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

/*
This parses command-line options, asking for missing required parameters via a very, very simple input interface.
These options are settings that carry throughout the program.
*/
#ifndef __MONKEYOPTIONS_H_
#define __MONKEYOPTIONS_H_

#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
#include "crappystring.h"

enum
{
	InputBufferSize = 256
};

class MonkeyOptions
{
public:
	CrappyString ExecutionFolder;
	CrappyString Username;
	CrappyString Password;
	CrappyString Server;
	CrappyString DownloadFolder;
	CrappyString Database;
	CrappyString God;
	CrappyString Handshake;
	CrappyString HandshakeConfirm;

public:
	MonkeyOptions()
	{
	}

	~MonkeyOptions()
	{
	}

	bool Get(int argc, char *argv[])
	{
		int index;

		if (!argc || !argv)
			return false;

		ExecutionFolder.Duplicate(Utils::GetExecutionPath(argv[0]));
		DownloadFolder.MakeString(ExecutionFolder, "downloads/");
		Server.Duplicate("talk.google.com");
		Database.MakeString(ExecutionFolder, "monkey.db");
		HandshakeConfirm.Duplicate("Listening...");

		for (index = 1; index < argc; index++) /* argv[0] is exe name */
		{
			if ((strlen(argv[index]) < 2) || (argv[index][0] != '-'))
				continue;

			switch (argv[index][1])
			{
				case 'u':
					Username.Duplicate(argv[index+1]);
					break;
				case 'p':
					Password.Duplicate(argv[index+1]);
					break;
				case 'f':
					DownloadFolder.Duplicate(argv[index+1]);
					break;
				case 's':
					Server.Duplicate(argv[index+1]);
					break;
				case 'd':
					Database.Duplicate(argv[index+1]);
					break;
				case 'g':
					God.Duplicate(argv[index+1]);
					break;
				case 'h':
					Handshake.Duplicate(argv[index+1]);
					break;
				case 'c':
					HandshakeConfirm.Duplicate(argv[index+1]);
					break;
			}
		}
#ifndef __DEBUG
		char buffer[InputBufferSize];

		while (!Username.Length())
		{
			printf("Enter the MonkeyBot account username: ");
			Username.Duplicate(gets(buffer));
		}
		while (!Password.Length())
		{
			printf("Enter the MonkeyBot account password: ");
			Password.Duplicate(gets(buffer));
		}
		while (!God.Length())
		{
			printf("Enter your command account username (MonkeyBot will only listen to this): ");
			God.Duplicate(gets(buffer));
		}
		while (!Handshake.Length())
		{
			printf("Enter your secret command phrase (phrase to initiate instructions): ");
			Handshake.Duplicate(gets(buffer));
		}
#endif
		return true;
	}
};

#endif

