#!/usr/bin/env python

# This will work when installed in site-packages on a 
# target system where the runner script is in /usr/bin
#
# So, you don't need anything special - no fancy path tricks.

import sys
import getopt
import broadcaster

def usage():
    print '''NAME
broadcaster - Library to easily use multiple types notifications

SYNOPSIS
broadcaster [-t|--type <value>] [-d|--desc] [-e|--event] [-p|--priority] [-h|--help]

DESCRIPTION
Library to easily use multiple types notifications

OPTIONS
-t, --type <value>
The <value> is mandatory argument, one of two options: 'log', 'push'
-d, --desc <value>
The <value> is mandatory argument, argument is set as message body.
-e, --event <value>
Event that is highlighted in push messages. Value is not used when type log is
selected.
-p, --priority <value>
The <value> is mandatory argument, one of three options: 'alert', 'notify', 'whisper'
-h, --help
Prints the synopsis and a list of the most commonly used commands.
-l, --licence
Prints broadcaster licence'''

def licence():
	print'''
LICENCE
Copyright (c) 2013, Richard Kellner
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

* Neither the name of the Erigones s.r.o. nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.'''

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "t:d:e:p:h", ["type=", "desc=", "event=", "priority=", "help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    type_ = None
    desc = None
    event = None
    prio = None

    for opt, arg in opts: # Loop throught parameters and set variables
        if opt in ("-t", "--type"):
            type_ = arg
        if opt in ("-d", "--desc"):
            desc = arg
        if opt in ("-e", "--event"):
            event = arg
        if opt in ("-p", "--priority"):
            prio = arg
        if opt in ("-l", "--licence"):
            licence()
            sys.exit()
        if opt in ("-h", "--help"):
            usage()
            sys.exit()

    type_ = check_type(type_)
    prio = check_prio(prio)
    event = check_event(event, type_)
    desc = check_desc(desc)

    b = broadcaster.get_client(type_, application='broadcaster')
    if prio == 'alert':
        b.alert(desc, event)
    if prio == 'notify':
        b.notify(desc, event)
    if prio == 'whisper':
        b.whisper(desc, event)

def check_type(type_):
    if type_ not in ('log', 'push'):
        type_ = raw_input('What type of message we will broadcast? (log, push): ')
        type_ = check_type(type_)
    return type_

def check_prio(prio):
    if prio not in ('alert', 'notify', 'whisper'):
        prio = raw_input('What priority you want to set? (alert, notify, whisper): ')
        prio = check_prio(prio)
    return prio

def check_event(event, type_):
    if event is None and type_ == 'push':
        event = raw_input('What message event: ')
        event = check_event(event, type_)
    return event

def check_desc(desc):
    if desc is None:
        desc = raw_input('What message description: ')
        desc = check_desc(desc)
    return desc

if __name__ == '__main__':
    main(sys.argv[1:])
