#include <windows.h>

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

#ifndef XXX
#define XXX 2
#endif

using namespace std;

void replace_all ( std::string & str, std::string const & pattern, std::string const & replacement ) {

	std::string::size_type start = str.find( pattern, 0 );

	while ( start != str.npos ) {
		str.replace( start, pattern.size(), replacement );
		start = str.find( pattern, start+replacement.size() );
	}

}

void show_help() {

	cout << "Wirk" << XXX << " - Windows Release Kit for Greenstone" << XXX << endl;
	cout << "Helps you to create releases from the Repository" << endl << endl;

	cout << "usage: wirk" << XXX << " [args]" << endl;
	cout << "	-sim" << endl;
	cout << "		simulation only, don't actually do anything" << endl << endl;

	cout << "	-help" << endl;
	cout << "		show this help screen" << endl << endl;

	cout << "	-from <target>" << endl;
	cout << "		start execution from the target with the given target address" << endl << endl;

	cout << "	-to <target>" << endl;
	cout << "		stop execution just before the target with the given target address" << endl << endl;

	cout << "	-descend <target>" << endl;
	cout << "		execute only the target with the given address, including subtargets" << endl << endl;

}


int main(int argc, char** argv ) {
	
	//some checks
	bool ok = true;
	if ( getenv( "JAVA_HOME" ) == NULL ) {
		cerr << "Please set JAVA_HOME before running wirk" << XXX << endl;
		ok = false;
	}

	stringstream out;
	out << "WIRK" << XXX << "_HOME";
	string wirk_home_var;
	wirk_home_var = out.str();

	if ( getenv( wirk_home_var.c_str() ) == NULL ) {
		cerr << "Please set " << wirk_home_var << " before running wirk" << XXX << endl;
		ok = false;
	}
	if ( !ok ) {
		return -1;
	}

	//load environment variables
	string JAVA_HOME = getenv( "JAVA_HOME" );
	string WIRK_HOME = getenv( wirk_home_var.c_str() );
	putenv( ("ANT_HOME=" + WIRK_HOME + "\\core\\ant").c_str() );
	
	//get the pwd
	string pwd;
	system( "CD > cd.dat" );
	ifstream file( "cd.dat" ) ;
	getline(file, pwd);
	file.close();
	system( "del cd.dat" );
		
	//create the command
	stringstream command;
	command << "cmd.exe /c " << WIRK_HOME << "\\core\\ant\\bin\\ant.bat -f \"" << WIRK_HOME << "\\ant-scripts\\build.xml\" -addressing \"-Dwirk" << XXX << ".home=" << WIRK_HOME << "\"";

	//pass on the arguments
	string a;
	bool simMode = false;
	for ( int i=1; i < argc; i++ ) {
		a = argv[i];
		
		if ( a.compare("-help") == 0 || a.compare("--help") == 0 ) {
			show_help();
			return 0;
		} else if ( a.compare("-sim") == 0) {
			command << " " << a;
			simMode = true;
		} else {
			command << " " << a;
		}

	}

	//set the basedir in the command
	command << " \"-Dbasedir=" << pwd << "\"";

	//pipe it all to tee
	command << " | " << WIRK_HOME << "\\windows\\utils\\tee.exe " << pwd << "\\wirk" << XXX << ".out";
	
	cout
		<< "O-----------------------------------------O" << endl
		<< "|                                         |" << endl
		<< "|   WiRK" << XXX << "                                 |" << endl
		<< "|   Windows Release Kit for Greenstone" << XXX << "   |" << endl
		<< "|                                         |" << endl
		<< "O-----------------------------------------O" << endl
		;
	
	string final_command = command.str();
	cout << "pwd: " << pwd << endl;
	cout << "command: " << final_command << endl;

	system( final_command.c_str() );
	
	return 0;

}
