#!/usr/bin/env python
#
#	THIS FILE IS PART OF THE JOKOSHER PROJECT AND LICENSED UNDER THE GPL. SEE
#	THE 'COPYING' FILE FOR DETAILS
#	
#	This script will try to import Jokosher from somewhere in the 
#	PYTHONPATH,	and use JOKOSHER_DATA_PATH, JOKOSHER_IMAGE_PATH, 
#	and JOKOSHER_LOCALE_PATH environment variables to find the other files it needs.
#	If those variables are not set, they will be set from their values in the ENV_PATHS
#	dictionary, which is defined below.
#	This script is also responsible for parsing comment line arguments.
#
#-------------------------------------------------------------------------------
import os, sys

ENV_PATHS = {"JOKOSHER_DATA_PATH" : "/usr/share/jokosher/",
			"JOKOSHER_IMAGE_PATH" : "/usr/share/jokosher/pixmaps/",
			"JOKOSHER_LOCALE_PATH" : "/usr/share/locale/",
			"JOKOSHER_HELP_PATH" : "/usr/share/gnome/jokosher/"
			}
#must set variables before importing Globals because it requires them
for var, path in ENV_PATHS.iteritems():
	#if it is not already set, set the enviroment variable.
	os.environ.setdefault(var, path)

import Jokosher.Globals as Globals

#for parsing out command line arguments
import optparse
#for i18n "--help" message
import locale, gettext
_ = gettext.gettext

try:
	locale.setlocale(locale.LC_ALL, '')
	gettext.bindtextdomain(Globals.LOCALE_APP, Globals.LOCALE_PATH)
	gettext.bind_textdomain_codeset(Globals.LOCALE_APP, "UTF-8");
	gettext.textdomain(Globals.LOCALE_APP)
except locale.Error:
	print "Locale unsupported, defaulting to english for all Jokosher specific text"
	
# parse command line
openproject = None
loadExtensions = True

parser = optparse.OptionParser(usage="%prog [options] [project-file]", version="0.2-SVN")
#command line options
parser.add_option("-d", "--debug", action="store_true", dest="debug", 
				help=_("Print debug output to stdout"))
parser.add_option("-g", "--gst-debug", action="store_true", dest="gstDebug", 
				help=_("Sent debug output to Gstreamer's debug system"))
parser.add_option("-s", "--safe-mode", action="store_true", dest="safeMode", 
				help=_("Don't load extensions or last project on startup (same as -ne)"))
parser.add_option("-w", "--welcome-dialog", action="store_true", dest="forceWelcome", 
				help=_("Force the welcome dialog to show on startup"))
parser.add_option("-n", "--no-project", action="store_true", dest="forceNoProject", 
				help=_("Force Jokosher to load without a welcome dialog or project"))
parser.add_option("-e", "--no-extensions", action="store_true", dest="noextensions", 
				help=_("Do not load extensions on startup"))

(options, args) = parser.parse_args()
if len(args) > 0:
	openproject = args[0]
	
Globals.DEBUG_STDOUT = options.debug
Globals.DEBUG_GST = options.gstDebug

if options.forceWelcome:
	startupType = 2
elif options.forceNoProject:
	startupType = 1
else:
	startupType = None

if options.safeMode:
	loadExtensions = False
	startupType = 1

if options.noextensions:
	loadExtensions = False

#wait until after we parse the args to import JokosherApp
#because it will import gst which replaces our "--help" message.
import Jokosher.JokosherApp as JokosherApp

#Launch the program
JokosherApp.MainApp(openproject, loadExtensions, startupType)
JokosherApp.gtk.main()
