#!/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:],"hsla:", ["help", "status", "locked", "active="])
  except getopt.GetoptError as err:
    print str(err)
    Usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-l", "--locked"):
       RTKGetLocked()
       sys.exit()
    elif opt in ("-s", "--status"):
       RTKGetSources()
       sys.exit()
    elif opt in ("-a", "--active"):
      Usage()
      sys.exit()
    elif opt in ("-h", "--help"):
      Usage()
      sys.exit()
    else:
      Usage()
      sys.exit(2)

def RTKGetLocked():
  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.RTK_GetLockedSource()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def RTKGetSources():
  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.RTK_GetSources()
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def Usage():
    print 'ice-rtk [option]'
    print '   --locked          Status of Locked RTK source'
    print '   --status          Status of RTK sources'
    print '   --active=[NAME]   Switch locked source to NAME - Not implemented'

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