#!/usr/bin/env python
# -*- Mode: Python -*-
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
#
# Codeina, Codec Installation Application
# Copyright (C) 2007 Fluendo S.A.
# Copyright (c) 2005 Edward Hervey <bilboed@bilboed.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser 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
import string
import locale
import gettext

# variables
LIBDIR = '/usr/lib'

localedir = ""

# Check if we're in development or installed version
# Add the path of codeina stuff

def _add_codeina_path():
    # this adds our (possibly uninstalled) codeina python directory
    global localedir
    dir = os.path.dirname(os.path.abspath(__file__))
    if (os.path.exists(os.path.join(dir, 'CVS')) or 
        os.path.exists(os.path.join(dir, '.svn'))):
        root = os.path.split(dir)[0]
        localedir = os.path.join(root, 'locale')
        if not root in sys.path:
            sys.path.insert(0, root)

    else:
        root = os.path.join(LIBDIR, 'codeina', 'python')
    	localedir = "/usr/share/locale"

    # for i18n
    try:
        locale.setlocale(locale.LC_ALL, '')
        locale.bindtextdomain('codeina', localedir)
        locale.textdomain('codeina')
        gettext.bindtextdomain('codeina', localedir)
        gettext.textdomain('codeina')
    except (ImportError, locale.Error):
        print "Locale setting not supported"

def _init_gobject_gtk_gst():
    global localedir
    try:
        import pygtk
        pygtk.require("2.0")

        import gtk

        import gobject
        gobject.threads_init()
    except ImportError:
        raise SystemExit("PyGTK couldn't be found !")

    gobject.threads_init()
    
    try:
        import pygst
        pygst.require('0.10')
        
        import gst
    except ImportError:
        raise SystemExit("Gst-Python couldn't be found!")

def _run_codeina():
    from codeina import main

    try:
        sys.exit(main.main(sys.argv))
    except StandardError, e:
        from codeina.extern.log import log
        log.warning("codeina", log.getExceptionMessage(e))
        print "Error: ", e
        # 2 = GST_INSTALL_PLUGINS_ERROR
        sys.exit(2)

try:
    _add_codeina_path()
    _init_gobject_gtk_gst()
    _run_codeina()
except KeyboardInterrupt:
    print "Interrupted by user!"
    # 4 = GST_INSTALL_PLUGINS_USER_ABORT
    sys.exit(4)
