#!/usr/bin/python
# Copyright (c) 2004, 2005, 2006, 2007 Tom Hinkle
# You may use and distribute this software under the terms of the
# GNU General Public License, version 2 or later

#import sys
#import signal
#signal.signal(signal.SIGINT, signal.SIG_DFL)
from stat import ST_MTIME
import sys, os, os.path, glob

def maybe_intltool (fname):
    '''Check whether the file at fname has been updated since
    intltool-merge was last used on it. If it has, then use
    intltool-merge to update the output file.

    '''
    to_name = fname[:-3]
    if (
        (not os.path.exists(to_name))
        or
        os.stat(to_name)[ST_MTIME] < os.stat(fname)[ST_MTIME]
        ):
        try:
            os.system('intltool-merge -d i18n/ %s %s'%(fname, to_name))
        except:
            print "Can't to intltool-merge -- ignoring"

for desktop_file in glob.glob('lib/plugins/*plugin.in') + glob.glob('lib/plugins/*/*plugin.in'):
    maybe_intltool(desktop_file)

if os.path.exists('foo'):
    os.remove(os.path.join('foo','gourmet'))
    os.rmdir('foo')

os.mkdir('foo')
os.symlink(os.path.abspath('lib'),os.path.join('foo','gourmet'))
sys.path = [os.path.abspath('foo')] + sys.path
sys.argv.append('--data-directory=%s'%os.path.abspath(os.path.join('..','data')))
sys.argv.append('--glade-directory=%s'%os.path.abspath(os.path.join('..','glade')))
sys.argv.append('--image-directory=%s'%os.path.abspath(os.path.join('..','images')))

from gourmet.OptionParser import *

def thread_debug ():
    print 'THREADING DEBUG INFO: ',threading.enumerate()
    t=threading.Timer(options.thread_debug_interval,thread_debug)
    print '(starting timer: ',t,')'
    t.terminate = lambda *args: t.cancel()
    t.start()
    
if options.thread_debug:
    import threading
    thread_debug()
elif options.psyco:
    try:
        import psyco
        psyco.full()
    except ImportError:
        # ignore
        pass

import gourmet.GourmetRecipeManager
gourmet.GourmetRecipeManager.startGUI()
#for mod in sys.modules:
#    print mod,sys.modules[mod]
