/****************************************************************************
**
** Implementation of Option class.
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of qmake.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "option.h"
#include <tqdir.h>
#include <tqregexp.h>
#include <stdlib.h>
#include <stdarg.h>

//convenience
TQString Option::prf_ext;
TQString Option::prl_ext;
TQString Option::libtool_ext;
TQString Option::pkgcfg_ext;
TQString Option::ui_ext;
TQStringList Option::h_ext;
TQString Option::cpp_tqmoc_ext;
TQString Option::h_tqmoc_ext;
TQStringList Option::cpp_ext;
TQString Option::obj_ext;
TQString Option::lex_ext;
TQString Option::yacc_ext;
TQString Option::dir_sep;
TQString Option::h_tqmoc_mod;
TQString Option::cpp_tqmoc_mod;
TQString Option::yacc_mod;
TQString Option::lex_mod;

//mode
Option::TQMAKE_MODE Option::qmake_mode = Option::TQMAKE_GENERATE_NOTHING;

//all modes
int Option::warn_level = WarnLogic;
int Option::debug_level = 0;
TQFile Option::output("");
TQString Option::output_dir;
TQStringList Option::before_user_vars;
TQStringList Option::after_user_vars;
TQString Option::user_template;
TQString Option::user_template_prefix;
#if defined(TQ_OS_WIN32)
Option::TARG_MODE Option::target_mode = Option::TARG_WIN_MODE;
#elif defined(TQ_OS_MAC9)
Option::TARG_MODE Option::target_mode = Option::TARG_MAC9_MODE;
#elif defined(TQ_OS_MACX)
Option::TARG_MODE Option::target_mode = Option::TARG_MACX_MODE;
#elif defined(TQ_OS_TQNX6)
Option::TARG_MODE Option::target_mode = Option::TARG_TQNX6_MODE;
#else
Option::TARG_MODE Option::target_mode = Option::TARG_UNIX_MODE;
#endif

//TQMAKE_*_PROPERTY stuff
TQStringList Option::prop::properties;

//TQMAKE_GENERATE_PROJECT stuff
bool Option::projfile::do_pwd = TRUE;
bool Option::projfile::do_recursive = TRUE;
TQStringList Option::projfile::project_dirs;

//TQMAKE_GENERATE_MAKEFILE stuff
TQString Option::mkfile::qmakespec;
int Option::mkfile::cachefile_depth = -1;
bool Option::mkfile::do_deps = TRUE;
bool Option::mkfile::do_tqmocs = TRUE;
bool Option::mkfile::do_dep_heuristics = TRUE;
bool Option::mkfile::do_preprocess = FALSE;
bool Option::mkfile::do_cache = TRUE;
TQString Option::mkfile::cachefile;
TQStringList Option::mkfile::project_files;
TQString Option::mkfile::qmakespec_commandline;

static Option::TQMAKE_MODE default_mode(TQString progname)
{
    int s = progname.findRev(Option::dir_sep);
    if(s != -1)
	progname = progname.right(progname.length() - (s + 1));
    if(progname == "qmakegen")
	return Option::TQMAKE_GENERATE_PROJECT;
    else if(progname == "qt-config")
	return Option::TQMAKE_TQUERY_PROPERTY;
    return Option::TQMAKE_GENERATE_MAKEFILE;
}

TQString project_builtin_regx();
bool usage(const char *a0)
{
    fprintf(stdout, "Usage: %s [mode] [options] [files]\n"
	    "\n"
	    "   TQMake has two modes, one mode for generating project files based on\n"
	    "some heuristics, and the other for generating makefiles. Normally you\n"
	    "shouldn't need to specify a mode, as makefile generation is the default\n"
	    "mode for qmake, but you may use this to test qmake on an existing project\n"
	    "\n"
	    "Mode:\n"
	    "\t-project       Put qmake into project file generation mode%s\n"
	    "\t               In this mode qmake interprets files as files to\n"
	    "\t               be built,\n"
	    "\t               defaults to %s\n"
	    "\t-makefile      Put qmake into makefile generation mode%s\n"
	    "\t               In this mode qmake interprets files as project files to\n"
	    "\t               be processed, if skipped qmake will try to find a project\n"
	    "\t               file in your current working directory\n"
	    "\n"
	    "Warnings Options:\n"
	    "\t-Wnone         Turn off all warnings\n"
	    "\t-Wall          Turn on all warnings\n"
	    "\t-Wparser       Turn on parser warnings\n"
	    "\t-Wlogic        Turn on logic warnings\n"
	    "\n"
	    "Options:\n"
	    "\t * You can place any variable assignment in options and it will be     *\n"
	    "\t * processed as if it was in [files]. These assignments will be parsed *\n"
	    "\t * before [files].                                                     *\n"
	    "\t-o file        Write output to file\n"
	    "\t-unix          Run in unix mode\n"
	    "\t-win32         Run in win32 mode\n"
	    "\t-macx          Run in Mac OS X mode\n"
	    "\t-d             Increase debug level\n"
	    "\t-t templ       Overrides TEMPLATE as templ\n"
	    "\t-tp prefix     Overrides TEMPLATE so that prefix is prefixed into the value\n"
	    "\t-help          This help\n"
	    "\t-v             Version information\n"
	    "\t-after         All variable assignments after this will be\n"
	    "\t               parsed after [files]\n"
	    "\t-cache file    Use file as cache           [makefile mode only]\n"
	    "\t-spec spec     Use spec as TQMAKESPEC       [makefile mode only]\n"
	    "\t-nocache       Don't use a cache file      [makefile mode only]\n"
	    "\t-nodepend      Don't generate dependencies [makefile mode only]\n"
	    "\t-notqmoc         Don't generate tqmoc targets  [makefile mode only]\n"
	    "\t-nopwd         Don't look for files in pwd [ project mode only]\n"
	    "\t-norecursive   Don't do a recursive search [ project mode only]\n"
	    ,a0, 
	    default_mode(a0) == Option::TQMAKE_GENERATE_PROJECT  ? " (default)" : "", project_builtin_regx().latin1(),
	    default_mode(a0) == Option::TQMAKE_GENERATE_MAKEFILE ? " (default)" : "");
    return FALSE;
}

enum {
    TQMAKE_CMDLINE_SUCCESS,
    TQMAKE_CMDLINE_SHOW_USAGE,
    TQMAKE_CMDLINE_BAIL
};
int
Option::internalParseCommandLine(int argc, char **argv, int skip)
{
    bool before = TRUE;
    for(int x = skip; x < argc; x++) {
	if(*argv[x] == '-' && strlen(argv[x]) > 1) { /* options */
	    TQString opt = argv[x] + 1;

	    //first param is a mode, or we default
	    if(x == 1) {
		bool specified = TRUE;
		if(opt == "project") {
		    Option::qmake_mode = Option::TQMAKE_GENERATE_PROJECT;
		} else if(opt == "prl") {
		    Option::mkfile::do_deps = FALSE;
		    Option::mkfile::do_tqmocs = FALSE;
		    Option::qmake_mode = Option::TQMAKE_GENERATE_PRL;
		} else if(opt == "set") {
		    Option::qmake_mode = Option::TQMAKE_SET_PROPERTY;
		} else if(opt == "query") {
		    Option::qmake_mode = Option::TQMAKE_TQUERY_PROPERTY;
		} else if(opt == "makefile") {
		    Option::qmake_mode = Option::TQMAKE_GENERATE_MAKEFILE;
		} else {
		    specified = FALSE;
		}
		if(specified)
		    continue;
	    }
	    //all modes
	    if(opt == "o" || opt == "output") {
		Option::output.setName(argv[++x]);
	    } else if(opt == "after") {
		before = FALSE;
	    } else if(opt == "t" || opt == "template") {
		Option::user_template = argv[++x];
	    } else if(opt == "tp" || opt == "template_prefix") {
		Option::user_template_prefix = argv[++x];
	    } else if(opt == "mac9") {
		Option::target_mode = TARG_MAC9_MODE;
	    } else if(opt == "macx") {
		Option::target_mode = TARG_MACX_MODE;
	    } else if(opt == "unix") {
		Option::target_mode = TARG_UNIX_MODE;
	    } else if(opt == "win32") {
		Option::target_mode = TARG_WIN_MODE;
	    } else if(opt == "d") {
		Option::debug_level++;
	    } else if(opt == "version" || opt == "v" || opt == "-version") {
		fprintf(stderr, "Qmake version: %s (TQt %s)\n", qmake_version(), TQT_VERSION_STR);
		fprintf(stderr, "Qmake is free software from Trolltech ASA.\n");
		return TQMAKE_CMDLINE_BAIL;
	    } else if(opt == "h" || opt == "help") {
		return TQMAKE_CMDLINE_SHOW_USAGE;
	    } else if(opt == "Wall") {
		Option::warn_level |= WarnAll;
	    } else if(opt == "Wparser") {
		Option::warn_level |= WarnParser;
	    } else if(opt == "Wlogic") {
		Option::warn_level |= WarnLogic;
	    } else if(opt == "Wnone") {
		Option::warn_level = WarnNone;
	    } else {
		if(Option::qmake_mode == Option::TQMAKE_GENERATE_MAKEFILE ||
		   Option::qmake_mode == Option::TQMAKE_GENERATE_PRL) {
		    if(opt == "nodepend") {
			Option::mkfile::do_deps = FALSE;
		    } else if(opt == "notqmoc") {
			Option::mkfile::do_tqmocs = FALSE;
		    } else if(opt == "nocache") {
			Option::mkfile::do_cache = FALSE;
		    } else if(opt == "nodependheuristics") {
			Option::mkfile::do_dep_heuristics = FALSE;
		    } else if(opt == "E") {
			Option::mkfile::do_preprocess = TRUE;
		    } else if(opt == "cache") {
			Option::mkfile::cachefile = argv[++x];
		    } else if(opt == "platform" || opt == "spec") {
			Option::mkfile::qmakespec = argv[++x];
			Option::mkfile::qmakespec_commandline = argv[x];
		    } else {
			fprintf(stderr, "***Unknown option -%s\n", opt.latin1());
			return TQMAKE_CMDLINE_SHOW_USAGE;
		    }
		} else if(Option::qmake_mode == Option::TQMAKE_GENERATE_PROJECT) {
		    if(opt == "nopwd") {
			Option::projfile::do_pwd = FALSE;
		    } else if(opt == "r") {
			Option::projfile::do_recursive = TRUE;
		    } else if(opt == "norecursive") {
			Option::projfile::do_recursive = FALSE;
		    } else {
			fprintf(stderr, "***Unknown option -%s\n", opt.latin1());
			return TQMAKE_CMDLINE_SHOW_USAGE;
		    }
		}
	    }
	} else {
	    TQString arg = argv[x];
	    if(arg.find('=') != -1) {
		if(before)
		    Option::before_user_vars.append(arg);
		else
		    Option::after_user_vars.append(arg);
	    } else {
		bool handled = TRUE;
		if(Option::qmake_mode == Option::TQMAKE_TQUERY_PROPERTY ||
		    Option::qmake_mode == Option::TQMAKE_SET_PROPERTY) {
		    Option::prop::properties.append(arg);
		} else {
		    TQFileInfo fi(arg);
		    if(!fi.convertToAbs()) //strange
			arg = fi.filePath();
		    if(Option::qmake_mode == Option::TQMAKE_GENERATE_MAKEFILE ||
		       Option::qmake_mode == Option::TQMAKE_GENERATE_PRL)
			Option::mkfile::project_files.append(arg);
		    else if(Option::qmake_mode == Option::TQMAKE_GENERATE_PROJECT)
			Option::projfile::project_dirs.append(arg);
		    else 
			handled = FALSE;
		}
		if(!handled)
		    return TQMAKE_CMDLINE_SHOW_USAGE;
	    }
	}
    }
    return TQMAKE_CMDLINE_SUCCESS;
}


bool
Option::parseCommandLine(int argc, char **argv)
{
    Option::cpp_tqmoc_mod = "";
    Option::h_tqmoc_mod = "tqmoc_";
    Option::lex_mod = "_lex";
    Option::yacc_mod = "_yacc";
    Option::prl_ext = ".prl";
    Option::libtool_ext = ".la";
    Option::pkgcfg_ext = ".pc";
    Option::prf_ext = ".prf";
    Option::ui_ext = ".ui";
    Option::h_ext << ".h" << ".hpp" << ".hh" << ".H" << ".hxx";
    Option::cpp_tqmoc_ext = ".tqmoc";
    Option::h_tqmoc_ext = ".cpp";
    Option::cpp_ext << ".cpp" << ".cc" << ".cxx" << ".C";
    Option::lex_ext = ".l";
    Option::yacc_ext = ".y";

    if(Option::qmake_mode == Option::TQMAKE_GENERATE_NOTHING)
	Option::qmake_mode = default_mode(argv[0]);
    if(const char *envflags = getenv("TQMAKEFLAGS")) {
	int env_argc = 0, env_size = 0, currlen=0;
	char quote = 0, **env_argv = NULL;
	for(int i = 0; envflags[i]; i++) {
	    if(!quote && (envflags[i] == '\'' || envflags[i] == '"')) {
		quote = envflags[i];
	    } else if(envflags[i] == quote) {
		quote = 0;
	    } else if(!quote && envflags[i] == ' ') {
		if(currlen && env_argv && env_argv[env_argc]) {
		    env_argv[env_argc][currlen] = '\0';
		    currlen = 0;
		    env_argc++;
		}
	    } else {
		if(!env_argv || env_argc > env_size) {
		    env_argv = (char **)realloc(env_argv, sizeof(char *)*(env_size+=10));
		    for(int i2 = env_argc; i2 < env_size; i2++)
			env_argv[i2] = NULL;
		}
		if(!env_argv[env_argc]) {
		    currlen = 0;
		    env_argv[env_argc] = (char*)malloc(255);
		}
		if(currlen < 255) 
		    env_argv[env_argc][currlen++] = envflags[i];
	    }
	}
	if(env_argv[env_argc]) {
	    env_argv[env_argc][currlen] = '\0';
	    currlen = 0;
	    env_argc++;
	}
	internalParseCommandLine(env_argc, env_argv);
	for(int i2 = 0; i2 < env_size; i2++) {
	    if(env_argv[i2])
		free(env_argv[i2]);
	}
	free(env_argv);
    }
    {
	int ret = internalParseCommandLine(argc, argv, 1);
	if(ret != TQMAKE_CMDLINE_SUCCESS) 
	    return ret == TQMAKE_CMDLINE_SHOW_USAGE ? usage(argv[0]) : FALSE;
    }

    //last chance for defaults
    if(Option::qmake_mode == Option::TQMAKE_GENERATE_MAKEFILE ||
	Option::qmake_mode == Option::TQMAKE_GENERATE_PRL) {
	if(Option::mkfile::qmakespec.isNull() || Option::mkfile::qmakespec.isEmpty())
	    Option::mkfile::qmakespec = getenv("TQMAKESPEC");

	//try REALLY hard to do it for them, lazy..
	if(Option::mkfile::project_files.isEmpty()) {
	    TQString pwd = TQDir::currentDirPath(), 
		   proj = pwd + "/" + pwd.right(pwd.length() - (pwd.findRev('/') + 1)) + ".pro";
	    if(TQFile::exists(proj)) {
		Option::mkfile::project_files.append(proj);
	    } else { //last try..
		TQDir d(pwd, "*.pro");
		if(d.count() != 1)
		    return usage(argv[0]);
		Option::mkfile::project_files.append(pwd + "/" + d[0]);
	    }
	}
    }

    //defaults for globals
    if(Option::target_mode == Option::TARG_WIN_MODE) {
	Option::dir_sep = "\\";
	Option::obj_ext =  ".obj";
    } else {
	if(Option::target_mode == Option::TARG_MAC9_MODE)
	    Option::dir_sep = ":";
	else
	    Option::dir_sep = "/";
	Option::obj_ext = ".o";
    }
    return TRUE;
}

bool Option::postProcessProject(TQMakeProject *project)
{
    Option::cpp_ext = project->variables()["TQMAKE_EXT_CPP"];
    if(cpp_ext.isEmpty())
	cpp_ext << ".cpp"; //something must be there
    Option::h_ext = project->variables()["TQMAKE_EXT_H"];
    if(h_ext.isEmpty())
	h_ext << ".h";

    if(!project->isEmpty("TQMAKE_EXT_PKGCONFIG"))
	Option::pkgcfg_ext = project->first("TQMAKE_EXT_PKGCONFIG");
    if(!project->isEmpty("TQMAKE_EXT_LIBTOOL"))
	Option::libtool_ext = project->first("TQMAKE_EXT_LIBTOOL");
    if(!project->isEmpty("TQMAKE_EXT_PRL"))
	Option::prl_ext = project->first("TQMAKE_EXT_PRL");
    if(!project->isEmpty("TQMAKE_EXT_PRF"))
	Option::prf_ext = project->first("TQMAKE_EXT_PRF");
    if(!project->isEmpty("TQMAKE_EXT_UI"))
	Option::ui_ext = project->first("TQMAKE_EXT_UI");
    if(!project->isEmpty("TQMAKE_EXT_CPP_TQMOC"))
	Option::cpp_tqmoc_ext = project->first("TQMAKE_EXT_CPP_TQMOC");
    if(!project->isEmpty("TQMAKE_EXT_H_TQMOC"))
	Option::h_tqmoc_ext = project->first("TQMAKE_EXT_H_TQMOC");
    if(!project->isEmpty("TQMAKE_EXT_LEX"))
	Option::lex_ext = project->first("TQMAKE_EXT_LEX");
    if(!project->isEmpty("TQMAKE_EXT_YACC"))
	Option::yacc_ext = project->first("TQMAKE_EXT_YACC");
    if(!project->isEmpty("TQMAKE_EXT_OBJ"))
	Option::obj_ext = project->first("TQMAKE_EXT_OBJ");
    if(!project->isEmpty("TQMAKE_H_MOD_TQMOC"))
	Option::h_tqmoc_mod = project->first("TQMAKE_H_MOD_TQMOC");
    if(!project->isEmpty("TQMAKE_CPP_MOD_TQMOC"))
	Option::cpp_tqmoc_mod = project->first("TQMAKE_CPP_MOD_TQMOC");
    if(!project->isEmpty("TQMAKE_MOD_LEX"))
	Option::lex_mod = project->first("TQMAKE_MOD_LEX");
    if(!project->isEmpty("TQMAKE_MOD_YACC"))
	Option::yacc_mod = project->first("TQMAKE_MOD_YACC");
    if(!project->isEmpty("TQMAKE_DIR_SEP"))
	Option::dir_sep = project->first("TQMAKE_DIR_SEP");
    return TRUE;
}

void fixEnvVariables(TQString &x)
{
    int rep;
    TQRegExp reg_var("\\$\\(.*\\)");
    reg_var.setMinimal( TRUE );
    while((rep = reg_var.search(x)) != -1)
	x.replace(rep, reg_var.matchedLength(), TQString(getenv(TQT_TQSTRING(x.mid(rep + 2, reg_var.matchedLength() - 3)).latin1())));
}
static TQString fixPath(TQString x)
{
#if 0
    TQFileInfo fi(x);
    if(fi.isDir()) {
	TQDir dir(x);
	x = dir.canonicalPath();
    } else {
	TQString dir = fi.dir().canonicalPath();
	if(!dir.isEmpty() && dir.right(1) != Option::dir_sep)
	    dir += Option::dir_sep;
	x = dir + fi.fileName();
    }
#endif
    return TQDir::cleanDirPath(x);
}


TQString
Option::fixPathToTargetOS(const TQString& in, bool fix_env, bool canonical)
{
    TQString tmp(in);
    if(fix_env)
	fixEnvVariables(tmp);
    if(canonical)
	tmp = fixPath(tmp);
    TQString rep;
    if(Option::target_mode == TARG_MAC9_MODE) 
	tmp = TQT_TQSTRING(tmp.replace('/', Option::dir_sep)).replace('\\', Option::dir_sep);
    else if(Option::target_mode == TARG_WIN_MODE) 
	tmp = tmp.replace('/', Option::dir_sep);
    else 
	tmp = tmp.replace('\\', Option::dir_sep);
    return tmp;
}

TQString
Option::fixPathToLocalOS(const TQString& in, bool fix_env, bool canonical)
{
    TQString tmp(in);
    if(fix_env)
	fixEnvVariables(tmp);
    if(canonical)
	tmp = fixPath(tmp);
#if defined(TQ_OS_WIN32)
    return tmp.replace('/', '\\');
#else
    return tmp.replace('\\', '/');
#endif
}

const char *qmake_version()
{
    static char *ret = NULL;
    if(ret)
	return ret;
    ret = (char *)malloc(15);
    sprintf(ret, "%d.%02d%c", TQMAKE_VERSION_MAJOR, TQMAKE_VERSION_MINOR, 'a' + TQMAKE_VERSION_PATCH);
    return ret;
}

void debug_msg(int level, const char *fmt, ...)
{
    if(Option::debug_level < level)
	return;
    fprintf(stderr, "DEBUG %d: ", level);
    {
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
    }
    fprintf(stderr, "\n");
}

void warn_msg(TQMakeWarn type, const char *fmt, ...)
{
    if(!(Option::warn_level & type))
	return;
    fprintf(stderr, "WARNING: ");
    {
	va_list ap;
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
    }
    fprintf(stderr, "\n");
}
