#!/usr/bin/python -E
# -*- coding: UTF-8 -*-

# Copyright 2001-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Maintainer: Java Team <java@gentoo.org>
# Author: Karl Trygve Kalleberg <karltk@gentoo.org>
# Ported to python by : Adrian Almenar <strider@gentoo.org> and Todd Berman <tberman@gentoo.org>
# Author of Python version 1.1+ : Jason Mobarak <aether@gentoo.org>

# $Header: /var/cvsroot/gentoo-src/java-config/python/java-config,v 1.43 2006/09/06 23:35:52 nichoj Exp $

import sys, getopt, os
from java_config import jc_options
from java_config.jc_output import *

__version__ = "$Revision: 1.43 $" [11:-2]
__portage_version__ = "1.3.6"

def run(todo):

	for option_class in todo:
		func, args = option_class[0].lstrip('-'), option_class[1]
		if len(func) == 1: 
			if args != '' or not options.has_key(func):
				func += ':'
			name = options[func]
			func = name()
		else:
			if args != '' or not option_to_class.has_key(func):
				func += '='
			name = option_to_class[func]
			func = name()  
		try: 
			cn = str(name).split('.')[1]
			if func.require_root:
				if os.getuid() != 0: 
					raise jc_options.OptionRuntimeError(
					  'root access required'				
					)
			func(args)
		except jc_options.OptionRuntimeError, why: 
			sys.stderr.write(red('%s: ' % cn) + bold(str(why)) + '\n')
			sys.exit(1)
		except NotImplementedError:
			sys.stderr.write(
			  red('Error: ') + bold('option %s, has not been implemented.' % cn) + '\n'
			)
			sys.exit(1)

usage = jc_options.Help()

def check_no_color (args):
	
	if True in [nc in args for nc in ('-n', '--nocolor')]:
		jc_options.NoColors()(None)	

if __name__ == '__main__':

	args=sys.argv[1:]

	if len(args) == 0: 
		raise SystemExit, usage('help')

	options, option_to_class = jc_options.collect()	
	todo_queue = [] 
	options_allowed_no_args = ('h', 'help', 'p', 'classpath','f','show-active-vm')

	while True:
		try:
			todo = getopt.getopt(
				args, 
				''.join(options.iterkeys()), 
                option_to_class.iterkeys()
			)

			if todo[1] != []: 
				check_no_color(args)
				sys.stderr.write(red('\nError: extraneous stuff on the command line\n\n'))
				raise SystemExit(usage('help'))

		except getopt.GetoptError, error:

			option = error.opt
			check = len(option) == 1 and '-' + option or '--' + option

			check_no_color(args)

			if option in options_allowed_no_args: 
				todo_queue.append((option, ''))
				args = [A for A in args if not check.startswith(A)]

			elif option in ('version', 'V'): 
				raise SystemExit (
				  bold('Gentoo ') + green('java-config') + bold(' version ' + __portage_version__)
				)
			else:
				sys.stderr.write(red('\nError: %s\n' % str(error)) + '\n')
				raise SystemExit(usage('help'))

		else: 
			for I in todo_queue: todo[0].append(I)
			break

	def tweak(arg):

		if (arg, '') in todo[0]:
			jc_options.NoColors()(None)	
			todo[0].remove((arg, ''))

	tweak('-n')
	tweak('--nocolor')

	if len(todo[0]) == 0: 
		raise SystemExit, usage('help')

	# multiplicity
	option_list = []
	for option in todo[0]:
		if option not in option_list: 
			option_list.append(option)	

	run(option_list)

# vim: noet:ts=4:
