#!/usr/bin/env pmpython
#
# Copyright (C) 2014,2017 Red Hat.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#

import sys as system
from pcp import pmapi
import cpmapi as c_api

""" Create an options object and set/get everything possible """

options = pmapi.pmOptions("a:bfl:D:h:H:K:LS:T:O:A:s:t:VZ:z?")
options.pmSetOptionFlags(c_api.PM_OPTFLAG_BOUNDARIES)
options.pmSetOptionFlags(c_api.PM_OPTFLAG_MULTI)
options.pmSetOptionFlags(c_api.PM_OPTFLAG_MIXED)

options.pmSetLongOptionHeader("General Options")
options.pmSetLongOptionAlign()
options.pmSetLongOptionArchive()
options.pmSetLongOptionDebug()
options.pmSetLongOptionHost()
options.pmSetLongOptionHostsFile()
options.pmSetLongOptionSpecLocal()
options.pmSetLongOptionLocalPMDA()
options.pmSetLongOptionOrigin()
options.pmSetLongOptionStart()
options.pmSetLongOptionSamples()
options.pmSetLongOptionFinish()
options.pmSetLongOptionInterval()
options.pmSetLongOptionVersion()
options.pmSetLongOptionTimeZone()
options.pmSetLongOptionHostZone()
options.pmSetLongOptionHelp()

# extract any options we can (many are consumed internally) & dump 'em;
# this test variant focusses on context creation and using time windows.

ctx = c_api.PM_ERR_NOCONTEXT
try:
    ctx = pmapi.pmContext.fromOptions(options, system.argv)
except pmapi.pmUsageErr as usage:
    usage.message()
    system.exit(1)
except pmapi.pmErr as error:
    print(error)

nonoptions = options.pmGetOperands()
if nonoptions != None:
    print("Non-option arguments: %s" % nonoptions)

# for host and archive lists exercise the creation of multiple contexts
hosts = options.pmGetOptionHosts()
if hosts != None:
    print("Host list: %s" % hosts)
    print("Host %s pmcd.hostname: %s" % (hosts[0], ctx.pmGetContextHostName()))
    if len(hosts) > 1:
        typed = c_api.PM_CONTEXT_HOST
        for index in range(1, len(hosts)):
            try:
                ctx = pmapi.pmContext.fromOptions(options, system.argv, typed, index)
                print("Host %s pmcd.hostname: %s" % (hosts[index], ctx.pmGetContextHostName()))
            except pmapi.pmErr as error:
                print("pmContext failed for host %s - %s" %s (hosts[index], error))

archives = options.pmGetOptionArchives()
if archives != None:
    print("Archive list: %s" % archives)
    print("Archive %s hostname: %s" % (archives[0], ctx.pmGetContextHostName()))
    if len(archives) > 1:
        typed = c_api.PM_CONTEXT_ARCHIVE
        for index in range(1, len(archives)):
            try:
                ctx = pmapi.pmContext.fromOptions(options, system.argv, typed, index)
                print("Archive %s hostname: %s" % (archives[index], ctx.pmGetContextHostName()))
            except pmapi.pmErr as error:
                print("pmContext failed for archive %s - %s" % (archives[index], error))

timezone = options.pmGetOptionTimezone()
if timezone != None:
    print("Timezone: %s" % timezone)

samples = options.pmGetOptionSamples()
if samples != None:
    print("Samples: %d" % samples)

interval = options.pmGetOptionInterval()
if interval != None:
    print("Interval: %s" % interval)

start = options.pmGetOptionStart()
if start != None:
    print("Start: %s" % start)

finish = options.pmGetOptionFinish()
if finish != None:
    print("Finish: %s" % finish)

align = options.pmGetOptionAlignment()
if align != None:
    print("Align: %s" % align)

origin = options.pmGetOptionOrigin()
if origin != None:
    print("Origin: %s" % origin)

print("Done!")
