/*
-----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 (windows/mac only).
arg: no-separate-debug-info,Do not store debug information in a separate file (unix only, default for mac).
arg: separate-debug-info,Strip debug information into a separate .debug file (unix only, default for non-mac).
-----END QCMOD-----
*/

#define QC_BUILDMODE
bool qc_buildmode_release = false;
bool qc_buildmode_debug = 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_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_SEPARATE_DEBUG_INFO") == "Y")
			opt_no_separate_debug_info = true;
		if(conf->getenv("QC_SEPARATE_DEBUG_INFO") == "Y")
			opt_separate_debug_info = true;

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

#ifdef Q_OS_WIN
		if(opt_no_separate_debug_info)
		{
			printf("\nError: The --no-separate-debug-info option is for unix only.\n");
			exit(1);
		}

		if(opt_separate_debug_info)
		{
			printf("\nError: The --separate-debug-info option is for unix 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);
		}

		// 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_separate_debug_info)
			qc_buildmode_separate_debug_info = true;
		else if(opt_no_separate_debug_info)
		{
			// nothing to do
		}
		else // default
		{
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
			qc_buildmode_separate_debug_info = true;
#endif
		}

		// make the string
		QStringList opts;
		QString other;

		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_separate_debug_info)
		{
			opts += "separate_debug_info";
			other += "QMAKE_CFLAGS += -g\n";
			other += "QMAKE_CXXFLAGS += -g\n";
		}

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

		if(!other.isEmpty())
			conf->addExtra(other);

		return true;
	}
};
