#! /usr/bin/env python

### Copyright (C) 2002-2006 Stephen Kennedy <stevek@gnome.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

import os
import sys

# Disable buffering of stdout
class Unbuffered(object):
    def __init__(self, file):
        self.file = file
    def write(self, arg):
        self.file.write(arg)
        self.file.flush()
    def __getattr__(self, attr):
        return getattr(self.file, attr)
sys.stdout = Unbuffered(sys.stdout)

# Use pychecker if requested
if "--pychecker" in sys.argv:
    sys.argv.remove("--pychecker")
    os.environ['PYCHECKER'] = "--no-argsused --no-classattr --stdlib"
        #'--blacklist=gettext,locale,pygtk,gtk,gtk.keysyms,popen2,random,difflib,filecmp,tempfile'
    import pychecker.checker

# Ignore session management
for ignore in "--sm-config-prefix", "--sm-client-id":
    try:
        smprefix = sys.argv.index(ignore)
        del sys.argv[smprefix:smprefix + 2]
    except (ValueError, IndexError):
        pass

# Extract the profiling flag
try:
    sys.argv.remove("--profile")
    profiling = True
except ValueError:
    profiling = False

# Support running from an uninstalled version
melddir = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), ".."))
if os.path.exists(os.path.join(melddir, "meld.doap")):
    sys.path[0:0] = [melddir]
else:
    sys.path[0:0] = [ #LIBDIR#
    ]

# i18n support
import meld.paths
import gettext
_ = gettext.gettext

gettext.bindtextdomain("meld", meld.paths.locale_dir())
gettext.textdomain("meld")

# Check requirements: Python 2.4, pygtk 2.8
pyver = (2,4)
pygtkver = (2,8,0)

def missing_reqs(mod, ver, exception=None):
    if isinstance(exception, ImportError):
        print _("Cannot import: ") + mod + "\n" + str(e)
    else:
        modver = mod + " " + ".".join(map(str, ver))
        print _("Meld requires %s or higher.") % modver
    sys.exit(1)

if sys.version_info[:2] < pyver:
    missing_reqs("Python", pyver)

# gtk+ and related imports
try:
    import pygtk
    pygtk.require("2.0")
except (ImportError, AssertionError), e:
    missing_reqs("pygtk", pygtkver, e)

try:
    import gtk
    assert gtk.pygtk_version >= pygtkver
except (ImportError, AssertionError), e:
    missing_reqs("pygtk", pygtkver, e)

try:
    import gtk.glade
except ImportError, e:
    missing_reqs("gtk.glade", pygtkver, e)

# Ignore deprecation warnings from pygtk > 2.6
#if gtk.pygtk_version >= (2,8,0):
#    import warnings
#    warnings.filterwarnings("ignore", category=DeprecationWarning)

gtk.glade.bindtextdomain("meld", meld.paths.locale_dir())
gtk.glade.textdomain("meld")
gtk.icon_theme_get_default().append_search_path(meld.paths.icon_dir())

def main():
    import meld.meldapp
    app = meld.meldapp.MeldApp()
    app.parse_args(sys.argv[1:])
    gtk.main()

if profiling:
    import profile
    profile.run("main()")
else:
    main()
