#!/usr/bin/env python
#
# Copyright (C) 2004,2005 by SICEm S.L.
#
# 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
# 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 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.
"""Set up the environment to run Gazpacho and launch the application.

Gazpacho can run either from the source tree or from an installed version
and is able to configure itself to use the right code location.
"""

import sys
import os

import pygtk 
pygtk.require('2.0')
import gtk

if gtk.pygtk_version < (2,4,0):
    print 'PyGTK 2.4.0 or later required for Gazpacho'
    raise SystemExit

print 'GTK Version:', gtk.gtk_version
print 'PyGTK Version:', gtk.pygtk_version

currentdir = os.path.dirname(os.path.abspath(sys.argv[0]))
prefix = os.path.abspath(os.path.join(currentdir, '..'))

# check from where the script has been launched and configure env accordingly
if 'setup.py' in os.listdir(prefix):
    print 'Running Gazpacho from source tree'
    if prefix not in sys.path:
        print 'Adding %s to sys.path' % prefix
        # put gazpacho in sys.path so we don't need to update PYTHONPATH
        sys.path.insert(0, prefix)
        
    if os.path.exists(os.path.join(prefix, 'gazpacho', 'path.py')):
        print 'Retrieving path.py file ... '
        from gazpacho import path
        path.pixmaps_dir = os.path.join(prefix, 'pixmaps')
        path.languages_dir = os.path.join(prefix, 'locale')
        path.catalogs_dir = os.path.join(prefix, 'catalogs')
    else:
        print 'Generating the path.py file ... ',
        fd = file (os.path.join(prefix, 'gazpacho', 'path.py'), 'w')
        fd.write('pixmaps_dir = r"%s"\n' % os.path.join(prefix, 'pixmaps'))
        fd.write('languages_dir = r"%s"\n' % os.path.join(prefix, 'locale'))
        fd.write('catalogs_dir = r"%s"\n' % os.path.join(prefix, 'catalogs'))
        fd.close()
        print 'done'
else:
    print 'Running Gazpacho from site-packages'
    major, minor = sys.version_info[0], sys.version_info[1]
    dir = os.path.join(prefix, 'lib', 'python%d.%d' % (major, minor),
                       'site-packages')
    if dir not in sys.path:
        print 'Adding %s to sys.path' % dir
        sys.path.append(dir)

# launch the main application window!
from gazpacho.main import main

if __name__ == '__main__':
    sys.exit(main(sys.argv))
