#!/usr/bin/env python
'''
This software code is made available "AS IS" without warranties of any kind.
You may NOT copy, display, modify or redistribute the software code or binaries
either by itself or as incorporated into your code, without the explicit
written permission from SixSq.  Your use of this software code is at your own
risk and you waive any claim against SixSq Sarl with respect to your use of
this software code. (c) 2009 SixSq Sarl. All rights reserved.

SixSq: http://www.sixsq.com

Authors: Marc-Elian Begin <meb@sixsq.com>
'''

from optparse import OptionParser

from com.sixsq.slipstream.CommandBase import CommandBase
from com.sixsq.slipstream.Client import Client   
from com.sixsq.slipstream.ConfigHolder import ConfigHolder
from com.sixsq.slipstream.NodeDecorator import NodeDecorator

class MainProgram(CommandBase):
    '''A command-line program to set the abort key/value pair in the info sys restlet.'''
    
    def __init__(self, argv=None):
        self.reason = None
        self.cancel = False
        super(MainProgram,self).__init__(argv)

    def parse(self):
        usage = '''usage: %prog [options] [<reason>]

<reason>         Reason for abort.'''

        self.parser.usage = usage

        self.parser.add_option('--cancel', dest='ignoreAbort',
                               help='cancel the abort status',
                               default=False, action='store_true')

        self.options, self.args = self.parser.parse_args()

        self._checkArgs()

        self.reason = (len(self.args) and self.args[0]) or None

    def _checkArgs(self):
        if len(self.args) > 1:
            self.usageExitTooManyArguments()

    def doWork(self):
        configHolder = ConfigHolder(self.options)
        client = Client(configHolder)
        value = client.setRuntimeParameter(NodeDecorator.ABORT_KEY, self.reason)

main = MainProgram

##############################################################################
# Executing this module from the command line
##############################################################################

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print '\n\nExecution interrupted by the user... goodbye!'
