#!/usr/bin/python

#
# Copyright (C) 2005 by Yasushi Saito (yasushi.saito@gmail.com)
# 
# Cxxchecker is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Cxxchecker is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

import optparse
import sys
import re
import os

from cxxcheckerutils.elemtypes import *
from cxxcheckerutils import *

def cxxcheckerDir():
    """Return the cxxchecker installation directory"""
    for path in sys.path:
        d = path + "/cxxcheckerutils/styles"  
        if os.path.exists(d):
            return d
    raise IOError, "cxxcheckerutils directory doesn't exist"    
def parseOptions():
    optParser = optparse.OptionParser(usage="""%prog [options] FILES""")
    optParser.add_option("-I", "--include", action="append",
                         help = """Add a directory to the list of directory to be searched""")
    optParser.add_option("--ignore", action="append",
                         help = """Add a directory to the list of directory to be ignored""")
    optParser.add_option("-D", "--define", action="append",
                         help = """Define a preprocessor symbol""")
    optParser.add_option("-c", "--config", action="append",
                         help = """Read and execute the python file""")
    optParser.add_option("-W", "--warn", action="append",
                  help = """warn blah""")

    opt, argv = optParser.parse_args(sys.argv[1:])

    if opt.warn:
        if "all" in opt.warn:
            options.warningOptions = options.allWarningOptions
        else:
            for w in opt.warn:
                if w.startswith("-"):
                    w = w[1:]
                    if w not in options.allWarningOptions:
                        error("Option %s not supported" % w)
                    options.warningOptions.remove(w)
                else:
                    if w not in options.allWarningOptions:
                        error("Option %s not supported" % w)
                    options.warningOptions.append(w)

    if opt.config:
        for c in opt.config:
            try:
                execfile(c)
            except IOError:
                execfile(cxxcheckerDir() + "/" + c + ".py") 

    if opt.ignore:
        options.ignoredDirs += opt.ignore

    includes = ""
    defines = ""
    if opt.include:
        includes = " ".join(["-I" + x for x in opt.include])
    if opt.define:
        defines = " ".join(["-D" + x for x in opt.define])
    return includes, defines, argv

includes, defines, argv = parseOptions()

for path in argv:
    objs = {}
    if re.search('\.(C|c|cc|cpp|H|h|hh|hpp)$', path):
        tmppath = "%s.cxx.xml" % path
        cmd = "gccxml -fxml=%s %s %s %s" % (tmppath, includes, defines, path)
        #print "Running: ", cmd
        os.system(cmd)
        
        parser.parseFile(tmppath, objs)
        os.unlink(tmppath)
    else:
        #parser.parseFile(path, objs)
        pass
    standardchecker.run(objs)
    

