#!/usr/bin/python
# Copyright (c) 2010 Adam Tauno Williams <awilliam@whitemice.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

# TODO: Add usage information

from coils.core  import *
import getopt, sys, os

def usage():
    print """

    """
    return

def main(argv):

    def callback(uuid, source, target, data):
        status = data.get('status')
        if (status == 200):
            print 'Schedule entry {0} deleted'.format(data.get('uuid'))
        elif (status == 404):
            print 'No such scheduler entry as {0} found'.format(data.get('uuid'))
        else:
            print 'Failed to remove requested scheduler entry'
        return True

    try:
        ctx, params = initialize_tool('unschedule-pocess', argv, arguments=('u:', ['uuid=']))
    except getopt.GetoptError, e:
        print 'unable to initialize OpenGroupware Coils subsystem'
        print e
        sys.exit(2)

    uuid = params.get('--uuid', None)
    if (uuid is None):
        print 'No schedule UUID specified'
        sys.exit(4)

    ctx.run_command('process::unschedule', uuid=uuid, callback=callback)
    ctx.wait()
    ctx.close()

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