/*
-----BEGIN QCMOD-----
name: buildmodeapp
section: project
arg: release,Build with debugging turned off (default).
arg: debug,Build with debugging turned on.
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: debug-and-release,Build two versions, with and without debugging turned on (mac only).
*/

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

#define PSI_DIR_NAME "psi-plus"

static QString sourceDir;

// this is a utility function required by few other modules
static bool psiGenerateFile(const QString &inFile, const QString &outFile, const QHash<QString,QString> &vars)
{
    QFile fin(inFile);
    QFile fout(outFile);
    if(!fin.open(QIODevice::ReadOnly | QIODevice::Text) || !fout.open(QIODevice::WriteOnly | QIODevice::Truncate))
    	return false;

    QTextStream tin(&fin);
    QTextStream tout(&fout);
    while(!tin.atEnd())
    {
    	QString line = tin.readLine();

    	QHashIterator<QString,QString> it(vars);
    	while(it.hasNext())
    	{
    		it.next();
    		line.replace("@@" + it.key() + "@@", it.value());
    	}

    	tout << line << endl;
    }

    return true;
}

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

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

    bool exec()
    {
    	QFileInfo fi(qc_getenv("QC_COMMAND"));
    	sourceDir = fi.absolutePath();

    	// 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;

    	bool staticmode = false;
    	if(conf->getenv("QC_STATIC") == "Y")
    		staticmode = 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);
    	}

    	// 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
    	{
#ifndef 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 += "*-g++*:QMAKE_CFLAGS += -g\n";
    		other += "*-g++*:QMAKE_CXXFLAGS += -g\n";
    	}

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

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

    	return true;
    }
};
