#!/usr/bin/env python
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

"""
gpsys1 -- print system info on a host

Usage:
	gpsys1 [--version] [-?amp]

	   --version : print version information
	   -?        : print this help screen
	   -a        : [default] print "platform memory" 
	   -m        : print "memory"
	   -p        : print "platform"

Output:
	platform: can be 'linux', 'darwin' or 'sunos5'
	memory:   system memory installed in bytes

	e.g. linux 1073741824
"""

import sys, os, getopt

script_name = os.path.split(__file__)[-1]
opt = {}
opt['-a'] = False
opt['-m'] = False
opt['-p'] = False

################
def usage(exitarg):
    help_path = os.path.join(sys.path[0], '..', 'docs', 'cli_help', script_name + '_help');
    try:
	f = open(help_path);
	for line in f: 
	    print line,
    except:
        print __doc__
    sys.exit(exitarg)

#############
def print_version():
    print '%s version $Revision$' % script_name
    sys.exit(0)

################
def parseCommandLine():
    global opt
    try:
	(options, args) = getopt.getopt(sys.argv[1:], '?amp', ['version'])
    except Exception, e:
	usage('Error: ' + str(e))

    for (switch, val) in options:
	if (switch == '-?'): usage(0)
	elif switch == '--version': print_version()
	else: opt[switch] = True

    val = False
    for k in opt: 
	val = val or opt[k]
    if not val: opt['-a'] = True


################
def run(cmd):
    f = None
    ok = False
    out = None
    try:
	f = os.popen(cmd)
	out = f.read()
	ok = not f.close()
    except:
	f.close()
	ok = False

    return (ok, out)


################
def getPlatform():
    if sys.platform.find('linux') >= 0: return 'linux'
    if sys.platform.find('darwin') >= 0: return 'darwin'
    if sys.platform.find('sunos5') >= 0: return 'sunos5'
    return '?'

################
def getMemory():
    if getPlatform() == 'linux':
	ok, out = run("sh -c 'cat /proc/meminfo | grep MemTotal'")
	if not ok: return '?'
	list = out.strip().split(' ')
	val = int(list[len(list) - 2])
	factor = list[len(list) - 1]
	if factor == 'kB': return val * 1024
	return '?'

    if getPlatform() == 'darwin':
	ok, out = run("sysctl hw.physmem")
	if not ok: return '?'
	list = out.strip().split(' ')
	val = int(list[1])
	return val
	
    if getPlatform() == 'sunos5':
	ok, out = run("sh -c \"prtconf | awk '/^Memory/{print}'\"")
	if not ok: return '?'
	list = out.strip().split(' ')
	val = int(list[2])
	factor = list[3]
	if factor == 'Megabytes': return val * 1024 * 1024
	return '?'

    return '?'


################
parseCommandLine()
if opt['-a'] or opt['-p']:
    try: 
	v = getPlatform()
    except:
	v = '?'
    print v,

if opt['-a'] or opt['-m']:
    try:
	v = getMemory()
    except:
	v = '?'
    print v,

print
