#!/usr/bin/python
#
#	released under the terms of the GPL v 2.0 (or later, at your opinion)
#	(c) 2005,2006 Roland Lezuo <roland.lezuo@chello.at>
#

__author__ = "Roland Lezuo <roland.lezuo@chello.at>"


import logging
logger = logging.getLogger("sgf-info")

import gnomego.sgfparser
import sys
import os


# argv is supposed to look like:
# argv[0] == prognam
# argv[1] == sgffile
# argv[2] == property to query

if len(sys.argv) != 4 and len(sys.argv) != 2:
	print "Usage: %s sgffile...prints infos about file" % sys.argv[0]
	print "Usage: %s sgffile game property" % sys.argv[0]
	print "Game selects the game in the collection, starting with 0. Properties are SGF properties"
	print "The following properties are known for sure"
	for i in gnomego.sgfparser.SGFGameMeta.__dict__.keys():
		if i.startswith("__"): continue
		print "   %s" % i
	exit(1)

mode_number_games = False
if len(sys.argv) == 2:
	mode_number_games = True


if __name__ == '__main__':
	if os.getenv("GNOMEGO_DEBUG") != None:
		logging.basicConfig(level=logging.DEBUG)
	else:
		logging.basicConfig()

	try:
		parser = gnomego.sgfparser.SGFParser()
		parser.parseFile(sys.argv[1])
		if mode_number_games:
			print parser.getNumberOfSGFGames()
			exit(0)
		sgfgame = parser.getSGFGame(int(sys.argv[2]))		# this schould be capsulated and documented
		logger.debug(sgfgame)
		game = gnomego.sgfparser.GnomeGoGame(sgfgame)
	except Exception,e:
		logger.critical("Error parsing file %s" % sys.argv[1])
		logger.critical("%s" % e)
		exit(1)
	# print the info wanted
	try:
		print game.get_metadata().__dict__[sys.argv[3]]
	except:
		print ""
