/***************************************************************************
 *   Copyright (C) 2005 by Christof Donat   *
 *   cdonat@gmx.de   *
 *                                                                         *
 *   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.             *
 ***************************************************************************/

#include <vector>
#include <string>
#include <iostream>

#ifndef HSADMIN_CONFIGFILE
#define HSADMIN_CONFIGFILE

#include "transaction.h"

using std::vector;
using std::string;
using std::iostream;

//! this class manages configfiles
class ConfigFileFinder: public vector<string*> {
	public:
	//! find the standard config-files
	ConfigFileFinder(Transaction& t);
	//! load a config file
	void loadFile(string filename);
};

//! parser for config files
class ConfigFileParser {
	public:
	//! parses one text
	inline ConfigFileParser(string& text) { this->parse(text); };
	//! parses multiple texts
	inline ConfigFileParser(vector<string*>* texts) { this->parse(texts); };	

	inline string getTicket() { string s = ""; return getTicket(s); };

	/** \brief get a ticket for a specified user. 
	 *
	 * this function finds the config for this user and calls the tocket-command
	 * with the specified shell.
	 */
	string getTicket(string &username);

	//! replaces references (\0, \1, etc.) in content with regex-matches.
	static string replaceStringMatches(string &userpattern, string username, string content);

	//! holds single config entries
	struct config {
		string pattern;
		string ticketcommand;
		string server;
		string shell;
		bool askpass;

		string getTicketcommand(string &username) { return ConfigFileParser::replaceStringMatches(pattern,username,ticketcommand); };
		string getServer(string &username) { return ConfigFileParser::replaceStringMatches(pattern,username,ticketcommand); };
		string getShell(string &username) { return ConfigFileParser::replaceStringMatches(pattern,username,ticketcommand); };
	};

	//! find the config for a specified user
	config& getConfig(string &username);

	private:
	void parse(string& text);
	inline void parse(vector<string*>* texts) {
		for( vector<string*>::iterator i = texts->begin(); i != texts->end(); i++ ) 
			this->parse(**i);
	}

	vector<config> configs;
	config basic;
};

#else
class ConfigFileFinder;
class ConfigFileParser;

#endif /* HSADMIN_CONFIGFILE */
