#! /usr/bin/env python
#
# grc -- Ginga Remote Control example client
#
# Eric Jeschke (eric@naoj.org)
#
# Copyright (c) Eric R. Jeschke.  All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
"""
 See the plugin RC.py for details of the server side.

 In Ginga, start the RC plugin to enable Ginga remote control
  (Plugins->Start RC).
 
 Show example usage (plugin must be started):
 $ grc help

 
"""
import sys
import xmlrpclib
from optparse import OptionParser

version = '20140127'

def main(options, args):

    # Get proxy to server
    url = "http://%s:%d" % (options.host, options.port)
    ginga = xmlrpclib.ServerProxy(url)

    if len(args) == 0:
        method = getattr(ginga, 'help')
    else:
        method = getattr(ginga, args[0])
        args = args[1:]
        
    # invoke method on rest of parameters
    res = method(*args)
    print res
    

if __name__ == "__main__":
   
    usage = "usage: %prog [options] cmd [arg] ..."
    optprs = OptionParser(usage=usage, version=('%%prog %s' % version))
    
    optprs.add_option("--debug", dest="debug", default=False, action="store_true",
                      help="Enter the pdb debugger on main()")
    optprs.add_option("--host", dest="host", metavar="HOST",
                      default="localhost", help="Connect to server at HOST")
    optprs.add_option("--port", dest="port", type="int",
                      default=9000, metavar="PORT",
                      help="Connect to server at PORT")
    optprs.add_option("--profile", dest="profile", action="store_true",
                      default=False,
                      help="Run the profiler on main()")

    (options, args) = optprs.parse_args(sys.argv[1:])

    # Are we debugging this?
    if options.debug:
        import pdb

        pdb.run('main(options, args)')

    # Are we profiling this?
    elif options.profile:
        import profile

        print "%s profile:" % sys.argv[0]
        profile.run('main(options, args)')

    else:
        main(options, args)

# END
