/*
-----BEGIN QCMOD-----
name: buildmode
section: project
arg: release,Build with debugging turned off (default).
arg: debug,Build with debugging turned on.
arg: debug-and-release,Build two versions, with and without debugging turned on (mac only).
arg: no-separate-debug-info,Do not store debug information in a separate file (default for mac).
arg: separate-debug-info,Strip debug information into a separate .debug file (default for non-mac).
-----END QCMOD-----
arg: no-framework,Do not build as a Mac framework (default).
arg: framework,Build as a Mac framework.
*/

#define QC_BUILDMODE
bool qc_buildmode_release = false;
bool qc_buildmode_debug = false;
bool qc_buildmode_framework = false;
bool qc_buildmode_separate_debug_info = false;

class qc_buildmode : public ConfObj
{
public:
	qc_buildmode(Conf *c) : ConfObj(c) {}
	QString name() const { return "buildmode"; }
	QString shortname() const { return "buildmode"; }

	// no output
	QString checkString() const { return QString(); }

	bool exec()
	{
		// first, parse out the options
		bool opt_release = false;
		bool opt_debug = false;
		bool opt_debug_and_release = false;
		//bool opt_no_framework = false;
		//bool opt_framework = false;
		bool opt_no_separate_debug_info = false;
		bool opt_separate_debug_info = false;

		if(conf->getenv("QC_RELEASE") == "Y")
			opt_release = true;
		if(conf->getenv("QC_DEBUG") == "Y")
			opt_debug = true;
		if(conf->getenv("QC_DEBUG_AND_RELEASE") == "Y")
			opt_debug_and_release = true;
		//if(conf->getenv("QC_NO_FRAMEWORK") == "Y")
		//	opt_no_framework = true;
		//if(conf->getenv("QC_FRAMEWORK") == "Y")
		//	opt_framework = true;
		if(conf->getenv("QC_NO_SEPARATE_DEBUG_INFO") == "Y")
			opt_no_separate_debug_info = true;
		if(conf->getenv("QC_SEPARATE_DEBUG_INFO") == "Y")
			opt_separate_debug_info = true;

#ifndef Q_OS_MAC
		if(opt_debug_and_release)
		{
			printf("\nError: The --debug-and-release option is for mac only.\n");
			exit(1);
		}
#endif

		// sanity check exclusive options
		int x;

		// build mode
		x = 0;
		if(opt_release)
			++x;
		if(opt_debug)
			++x;
		if(opt_debug_and_release)
			++x;
		if(x > 1)
		{
			printf("\nError: Use only one of --release, --debug, or --debug-and-release.\n");
			exit(1);
		}

		// framework
		/*x = 0;
		if(opt_no_framework)
			++x;
		if(opt_framework)
			++x;
		if(x > 1)
		{
			printf("\nError: Use only one of --framework or --no-framework.\n");
			exit(1);
		}*/

		// debug info
		x = 0;
		if(opt_no_separate_debug_info)
			++x;
		if(opt_separate_debug_info)
			++x;
		if(x > 1)
		{
			printf("\nError: Use only one of --separate-debug-info or --no-separate-debug-info\n");
			exit(1);
		}

		// now process the options

		if(opt_release)
			qc_buildmode_release = true;
		else if(opt_debug)
			qc_buildmode_debug = true;
		else if(opt_debug_and_release)
		{
			qc_buildmode_release = true;
			qc_buildmode_debug = true;
		}
		else // default
			qc_buildmode_release = true;

		/*if(opt_framework)
			qc_buildmode_framework = true;
		else if(opt_no_framework)
		{
			// nothing to do
		}
		else // default
		{
			// nothing to do
		}*/

		if(opt_separate_debug_info)
			qc_buildmode_separate_debug_info = true;
		else if(opt_no_separate_debug_info)
		{
			// nothing to do
		}
		else // default
		{
#ifndef Q_OS_MAC
			qc_buildmode_separate_debug_info = true;
#endif
		}

		// make the string
		QStringList opts;

		if(qc_buildmode_release && qc_buildmode_debug)
		{
			opts += "debug_and_release";
			opts += "build_all";
		}
		else if(qc_buildmode_release)
			opts += "release";
		else // qc_buildmode_debug
			opts += "debug";

		//if(qc_buildmode_framework)
		//	opts += "lib_bundle";

		if(qc_buildmode_separate_debug_info)
			opts += "separate_debug_info";

		QString str = QString("CONFIG += ") + opts.join(" ") + '\n';
		conf->addExtra(str);
		return true;
	}
};
