#!/usr/bin/python
"""
This script ensures that all necessary python modules are available
before running the actual program, if any modules are missing the
script will exit and print the name of the missing module to stdout.
"""
# Font Manager, a font management application for the GNOME desktop
#
# Copyright (C) 2009, 2010 Jerry Casiano
#
# 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 3
# 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:
#
# Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor
# Boston, MA  02110-1301, USA.
#
#
# Suppress warnings due to unused imports
# pylint: disable-msg=W0612

import os
import sys

# Allow running without installation
if os.path.exists(os.path.join(os.path.dirname(__file__), 'font-manager.in')):
    PACKAGE_DIR = os.path.dirname(os.path.abspath(__file__))
    LIB_DIR = os.path.dirname(os.path.abspath(__file__))
else:
    PACKAGE_DIR = '/usr/share/font-manager'
    LIB_DIR = '/usr/lib/font-manager'

for directory in PACKAGE_DIR, LIB_DIR:
    if not directory in sys.path:
        sys.path.insert(0, directory)

import constants

def _exit_with_error(msg, error):
    """
    Print an error message letting user know which python module is missing
    """
    sys.stderr.write('Error: {0} ({1!s})\n'.format(msg, error))
    sys.exit(1)

def main():
    """
    Checks that necessary modules are available before running the program
    """
    try:
        import pygtk
        pygtk.require('2.0')
        import gtk
    except (ImportError, AssertionError), error:
        _exit_with_error('Importing pygtk and gtk modules failed',  error)

    try:
        import glib
    except ImportError, error:
        _exit_with_error('Importing glib module failed',  error)

    try:
        import gobject
    except ImportError, error:
        _exit_with_error('Importing gobject module failed',  error)

    try:
        import cairo
    except ImportError, error:
        _exit_with_error('Importing pycairo module failed',  error)

    try:
        import libxml2
    except ImportError, error:
        _exit_with_error('Importing libxml2 module failed',  error)

    from main import Main

    Main()

    try:
        gtk.main()
    except (KeyboardInterrupt):
        sys.exit(0)


if __name__ == '__main__':
    main()
