#!/usr/pkg/bin/python2.7
import sys, os
try:
	import logging
except ImportError:
	import mylogging as logging
	sys.modules['logging'] = logging

import getopt		# (optparse is only in Python 2.3)
import findrox; findrox.version(2, 0, 0)
import rox
from rox import choices, options
import constants
choices.migrate('ROX-Session', constants.site)

rox.setup_app_options('ROX-Session', 'Options', constants.site)

import logout
import log
import session

o_i18n_translation = options.Option('i18n_translation', 'None')

rox.app_options.notify()

if o_i18n_translation.value == 'None':
	langs = None
else:
	langs = [o_i18n_translation.value]

__builtins__._ = rox.i18n.translation(os.path.join(rox.app_dir, 'Messages'), languages = langs)

log_level = None
test_mode = False

usage = _("Usage: ROX-Session/AppRun [OPTION]...\n"	\
       "If ROX-Session is already managing your session then display\n" \
       "the logout window. If not, offer to make ROX-Session your\n"	\
       "session manager for future sessions.\n\n"			\
       "  -h, --help		display this help and exit\n"		\
       "      --install		make ROX-Session your session manager\n"  \
       "      --options		show options dialog\n"			\
       "      --setup		configure initial environment and apps\n" \
       "  -v, --version		display the version information and exit\n"   \
       "  -w, --wait		simply wait until run again (internal use)\n" \
       "\nThe latest version can be found at:\n"			\
       "\thttp://rox.sourceforge.net\n"					\
       "\nReport bugs to <tal197@users.sourceforge.net>.")

version = "ROX-Session 0.27\n" + _(
	    "Copyright (C) 2005 Thomas Leonard.\n"
	    "ROX-Session comes with ABSOLUTELY NO WARRANTY,\n"
	    "to the extent permitted by law.\n"
	    "You may redistribute copies of ROX-Session\n"
	    "under the terms of the GNU General Public License.\n"
	    "For more information about these matters, see the file named COPYING.")

try:
	options, args = getopt.getopt(sys.argv[1:], 'htvVwm',
			['help', 'install', 'messages', 'options', 'setup', 'verbose', 'version', 'wait'])
	if args:
		raise getopt.GetoptError('Unexpected arguments: ' + str(args))
except getopt.GetoptError, ex:
	print >>sys.stderr, "Error:", str(ex)
	print >>sys.stderr, usage
	sys.exit(1)

try:
	for opt, value in options:
		if opt in ('-h', '--help'):
			print usage
			sys.exit(0)
		elif opt in ('-V', '--version'):
			print version
			sys.exit(0)
		elif opt in ('-t', '--test'):
			test_mode = True
			constants.session_service += '-test'
		elif opt in ('-w', '--wait'):
			import main
			main.manage_session(test_mode)
			sys.exit(0)
		elif opt == '--install':
			import setup
			setup.setup()
			sys.exit(0)
		elif opt == '--setup':
			import AutoStart
			sys.exit(0)
		elif opt == '--options':
			import interactive
			interactive.show_options()
			sys.exit(0)
		elif opt == '--messages':
			import interactive
			interactive.show_messages()
			sys.exit(0)
		elif opt in ('-v', '--verbose'):
			logger = logging.getLogger()
			if log_level == logging.INFO:
				log_level = logging.DEBUG
			else:
				log_level = logging.INFO
			logger.setLevel(log_level)
			logging.info("Set verbose logging")
		else:
			raise Exception('Unknown option %s', opt)
	import interactive
	interactive.setup_or_logout()
except SystemExit:
	pass
except KeyboardInterrupt:
	raise
except:
	rox.report_exception()
