/* ImLib3D
 * Copyright (c) 2001, ULP-IPB Strasbourg.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*! \file UserPlugin.hpp */

#ifndef _UserPlugin_hpp
#define _UserPlugin_hpp

#include<ImLib3D/PluginManagement.hpp>
#include<ImLib3D/ImageProcessorDescription.hpp>
#include<ImLib3D/Base64.hpp>
#include<ImLib3D/PluginManagement.hpp>
#include<ImLib3D/XMLTools.hpp>
#include<ImLib3D/TemplateGroups.hpp>
#include<ImLib3D/ImageProcessorDescription.hpp>
#include<ImLib3D/StringArgumentConversion.hpp>

//  #include<ImageProcessorsByNameDefinitions.cpp>
//  #include<ImageProcessorsByNameCalls.cpp>

void
UserPluginMain(int argc,char **argv)
{
	bool CustomImProcCalls(const string &procName,const vector<string> &args,const string typeName,
						   vector<TypeSafeGenericPointer> alternateArgs);
	void StandardImProcCalls(const string &procName,const vector<string> &args,const string typeName,
							 vector<TypeSafeGenericPointer> alternateArgs);

	vector<string> args;
	for(int i=1;i<argc;i++){args.push_back(argv[i]);}

	if(args.size()==0){ThrowError("bad commandline for plugin");}

	string procName;
	string templateTypeName;

	// *** answer to queries
	// sanity check
	if(args[0]=="--query-imlib3d"){printf("imlib3d-plugin");exit(0);}
	// get xml description 
	if(args[0]=="--query-full-xml-description")
	{
		string xml=Base64::Decode(hardcodedDescription);
		cout << xml << endl;
		exit(0);
	}
	procName=args[0];
	// read description file
	ImageProcessorsDescription description;
	description.ReadFromString(Base64::Decode(hardcodedDescription));
	// parse commandline
	ImageProcessorDescription imageProcessorDescription(procName,description);
	vector<string> resArgs=imageProcessorDescription.ParseCmdLine(args);
	// find template typename
	templateTypeName=imageProcessorDescription.ProcTemplateTypeName(resArgs);
	fprintf(stderr,"PluginMain for:\"%s\" ... over\n",argv[0]);

	// now call 
	vector<TypeSafeGenericPointer> alternateArgs;
#ifdef HAS_CUSTOM_IMPROCCALLS
	if(!CustomImProcCalls(procName,resArgs,templateTypeName,alternateArgs))
#endif //HAS_CUSTOM_IMPROCCALLS
		StandardImProcCalls(procName,resArgs,templateTypeName,alternateArgs);
}

#endif // _UserPlugin_hpp
