#!/usr/bin/python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# Flumotion - a streaming media server
# Copyright (C) 2004,2005 Fluendo, S.L. (www.fluendo.com). All rights reserved.

# This file may be distributed and/or modified under the terms of
# the GNU General Public License version 2 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "LICENSE.GPL" in the source distribution for more information.

# Licensees having purchased or holding a valid Flumotion Advanced
# Streaming Server license may use this file in accordance with the
# Flumotion Advanced Streaming Server Commercial License Agreement.
# See "LICENSE.Flumotion" in the source distribution for more information.

# Headers in this file shall remain intact.


# Be very careful about adding new imports here, we want to catch
# Ctrl-C, so we must be able to enter the try/except for KeyboardInterrupt
# as quick as possible
import os
import sys

# Variable templates 
LIBDIR = '/usr/lib64'
PYGTK_DIR = '/usr/lib64/python2.4/site-packages'
PYGST_DIR = '/usr/lib64/python2.4/site-packages'
PYGTK_VERSION = '2.4.0'
PROGRAM_PATH = 'flumotion.worker.main.main'

def _init_gobject():
    """
    I setup paths for pygtk from configure and
    make sure that the installed version of pygtk
    is sane enough to be usable by us
    """
    
    if not PYGTK_DIR in sys.path:
        sys.path.insert(0, PYGTK_DIR)

    try:
        import pygtk
        pygtk.require('2.0')

        import gobject
    except ImportError:
        raise SystemExit('ERROR: PyGTK could not be found')

    if gobject.pygtk_version < PYGTK_VERSION.split('.'):
        raise SystemExit('ERROR: PyGTK %s or higher is required' % PYGTK_VERSION)

    # To be able to have other threads emitting signals connected
    # python callbacks we need to use the new PyGILThread_STATE API
    # Which in PyGTK 2.4.0 only can be turned on by using an
    # environment variable
    os.environ['PYGTK_USE_GIL_STATE_API'] = ''
    gobject.threads_init()

def _init_gst():
    """
    I setup paths for gst-python from configure and
    make sure that the installed version of gst-python
    is sane enough to be usable by us
    """

    if PYGST_DIR != PYGTK_DIR:
        if not PYGST_DIR in sys.path:
            sys.path.insert(0, PYGST_DIR)
        
    try:
        import gst
    except ImportError:
        raise SystemExit('ERROR: gst-python could not be found')
    
def _setup_project_root():
    dir = os.path.dirname(os.path.abspath(__file__))
    if os.path.exists(os.path.join(dir, '.svn')):
        root = os.path.split(dir)[0]
    else:
        root = os.path.join(LIBDIR, 'flumotion', 'python')
    sys.path.insert(0, root)

def _install_reactor():
    from flumotion.twisted import compat
    """
    Installs our reactor, so we can use twisted.internet from the
    whole code base without caring about the default reactor
    """
    compat.install_reactor()    

def _run_main(path):
    from flumotion.twisted import reflect
    
    # Import errors first, otherwise the exception fails
    from flumotion.common import errors
    from flumotion.common import setup
    setup.setup()

    main = reflect.namedAny(path)
    
    try:
        sys.exit(main(sys.argv))
    except errors.SystemError, e:
        print 'ERROR:', e
        sys.exit(1)
    
try:
    _setup_project_root()
    _init_gobject()
    _init_gst()
    _install_reactor()
    _run_main(PROGRAM_PATH)
except KeyboardInterrupt:
    print 'Interrupted'
