#include <QCoreApplication>
#include <QString>
#include <QSettings>
#include <QDir>
#include <QProcess>
#include <QStringList>
#include <QDebug>
#include <stdlib.h>
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	QString exeName = a.applicationName();
	//0. the exe name and ini filename should be  stored together.
	QString iniFile = a.applicationFilePath() + ".ini";
	QSettings settings(iniFile,QSettings::IniFormat);
	//1.get the current directory
	QString strCurrentDir = QDir::current().absolutePath();
	//2.get the visual studio vcvars32 path and raw EXE path
	QString vcvarsPath = settings.value("SETTINGS/VCVARS","D:\\Microsoft Visual Studio 10.0\\VC\\bin\\vcvars32.bat")
			.toString();
	QString exeDir = settings.value("SETTINGS/EXEDIR","C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x86")
			.toString();
	//3.Make the cmd target
	QString cmd ;
	//3.1. call vc vars to
	//	cmd += " && ";
	cmd += "CALL ";
	cmd += "\"" + vcvarsPath + "\"";
	//3.2. chdir back to current
	cmd += " && ";
	cmd += "CD /D \"" + strCurrentDir.replace("/","\\") + "\"";
	//3.3. call exeName with args
	cmd += " && ";
	cmd += "\"" + exeDir + "\\" + exeName + "\" ";

	for(int i=1;i<argc;++i)
		cmd += argv[i] + QString(" ");

	settings.setValue("SETTINGS/VCVARS",vcvarsPath);
	settings.setValue("SETTINGS/EXEDIR",exeDir);

	//4. display and exe the cmd
	puts(cmd.toStdString().c_str());
	int ret = system(cmd.toStdString().c_str());
	a.exit(0);
	return ret;
}
