#!/usr/bin/env python
"""
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
"""
from __future__ import print_function

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, end=' ')
    except:
        print(__doc__)
    sys.exit(exitarg)

#############
def print_version():
    print('%s version 6.30.0_arenadata70 build 4618+git952567f9441' % script_name)
    sys.exit(0)

################
def parseCommandLine():
    global opt
    try:
        (options, args) = getopt.getopt(sys.argv[1:], '?amp', ['version'])
    except Exception as 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.memsize")
        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, end=' ')

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

print()
