#!/usr/bin/python

# linda - Replacement for lintian
# (c) 2001-2005 Steve Kowalik <stevenk@debian.org>

# This program 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 of the License, or
# (at your option) any later version.
#
# This program 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

###########################################################

#
# > How *DARE* you interrupt this flame war about useless spam with your 
# > useless spam.
# Hey, doogie, give my brain back.  I need it.
# I think.
#                 -- Branden Robinson
#

###########################################################

import sys, traceback, gettext
gettext.bindtextdomain('linda')
gettext.textdomain('linda')
_ = gettext.gettext
try:
    import os, pwd, gettext, locale, linda
    from linda import clparser, root
    from linda.debug import dprint, vprint
    from linda.checker import Checker, CheckerException
    import linda.bootstrap
except ImportError, e:
    print _("Failed to import modules: %s") % e
    print _("Please try setting the PYTHONPATH environment variable.")
    traceback.print_exc(file=sys.stdout)
    sys.exit(6)

class Linda:
    def __init__(self):
        self.check_sanity()
        self.set_up_locale()
        self.check_user()
        
    def run(self):
        if not clparser['files']:
            print _("Linda: No valid files specified.")
            sys.exit(3)
        dprint(_("Files: %s") % clparser['files'])
        checker = Checker()
        for file in clparser['files']:
            try:
                checker.check(file)
            except CheckerException, e:
                print _("File %s failed to process: %s") % (file, e)
        sys.exit(linda.outputobj.exit_status)
    
    def check_sanity(self):
        for dir in ('checks', 'data', 'output'):
            if not os.path.isdir(os.path.join(root, dir)):
                print _("Linda: %s is not a directory.") % \
                    os.path.join(root, dir)
                sys.exit(5)

    def set_up_locale(self):
        try:
            locale.setlocale(locale.LC_ALL, "")
        except locale.Error, e:
            print _("Failed to set the locale: %s") % e

    def check_user(self):
        if not os.geteuid():
            if not clparser['quiet'] and not clparser['seteuid']:
                print _("Linda: Running as root, dropping to nobody.")
            if not clparser['seteuid']:
                os.seteuid(pwd.getpwnam('nobody')[3])
        
if __name__ == '__main__':
    try:
        main = Linda()
        if clparser['profiling']:
            try:
                import profile, pstats
                profile.run('main.run()', '/tmp/linda.prof')
                p = pstats.Stats('/tmp/linda.prof')
                p.sort_stats('cumulative').print_stats(20)
            except ImportError:
                print _("Linda: Can't locate profiling modules.")
                main.run()
        else:
            main.run()
    except KeyboardInterrupt:
        print _("Exiting due to user interrupt.")

