#!/usr/bin/python
####
#
#  Copyright (c) 2008-2012 Aerospike, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
####
#
# A short utility program which pings a given host and requests the 'info' about
# either all names or a certain name
#
#

import sys
import time
import types
import argparse
import getpass

try:
	import citrusleaf
except:
	print "citrusleaf.py not in your python path. Set PYTHONPATH!"
	sys.exit(-1)

parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("-h", "--host", default="127.0.0.1", help="server host name (default: %(default)s)")
parser.add_argument("-p", "--port", type=int, default=3000, help="server port (default: %(default)s)")
parser.add_argument("-U", "--user", help="user name")
parser.add_argument("-P", "--password", nargs="?", const="prompt", help="password")
parser.add_argument("-v", "--value", help="fetch single value (default all)")
parser.add_argument("-l", "--lineseperator", action="store_true", default=False, help="print in seperate lines (default: %(default)s)")
parser.add_argument("-u", "--usage", action='help', help="show program usage")
parser.add_argument("-d", "--debug", action='store_true', help="Print extra debug information")
args = parser.parse_args()

if args.value == "stats":
	args.value = "statistics"

user = None
password = None

if args.user != None:
	user = args.user
	if args.password == "prompt":
		args.password = getpass.getpass("Enter Password:")
	password = citrusleaf.hashpassword(args.password)

#
# MAINLINE
#
r = citrusleaf.citrusleaf_info(args.host, args.port, args.value, user, password)

if r == -1:
	print "request to ",args.host,":",args.port," returned error"
	sys.exit(0)

if r == None:
	print "request to ",args.host,":",args.port," returned no data"
	sys.exit(0)

if type(r) == types.StringType:
	if args.debug:
		print "requested value",args.value

	if args.lineseperator:
		r = r.replace(';', '\n')

	if args.debug:
		print "value is",r
	else:
		print r
else:
	# it better be a dict then
	i = 1
	for name, value in r.iteritems():
		print i,": ",name
		print "    ",value
		i += 1

