#!/usr/bin/python

import sys, getopt
sys.path.append('/usr/local/bin/thrift-ice-py')
 
from ice import Ice
 
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
 
def main(argv):
  try:
    opts, args = getopt.getopt(sys.argv[1:],"heds", ["help", "enable", "disable", "status"])
  except getopt.GetoptError as err:
    print str(err)
    Usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-e", "--enable"):
       RSEnable()
       sys.exit()
    elif opt in ("-d", "--disable"):
       RSDisable()
       sys.exit()
    elif opt in ("-s", "--status"):
       RSGetStatus()
       sys.exit()
    elif opt in ("-h", "--help"):
      Usage()
      sys.exit()
    else:
      Usage()
      sys.exit(2)

def RSGetStatus():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.remoteSupportStatus()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def RSEnable():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    #TODO:: get screen resolution here
    print client.requestRemoteSupport(1024,768)
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def RSDisable():
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()
 
    print client.stopRemoteSupport()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def Usage():
	print 'ice-rs [option]'
	print '   --enable          Enable remote support'
	print '   --disable         Disable remote support'
	print '   --status          Status of remote support session'

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