#!/usr/bin/python

import sys, getopt
sys.path.append('/usr/local/bin/thrift-ice-py')
 
from ice import Ice
from ice.ttypes import *
 
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:],"hrln:u:a:sf:", ["help", "refresh", "list", "notes=", "update=", "ack=", "status", "feature="])
  except getopt.GetoptError as err:
    print str(err)
    Usage()
    sys.exit(2)
  for opt, arg in opts:
    if opt in ("-r", "--refresh"):
       OTARefresh()
       sys.exit()
    elif opt in ("-l", "--list"):
       OTAList()
       sys.exit()
    elif opt in ("-n", "--notes"):
       OTANotes(arg)
       sys.exit()
    elif opt in ("-u", "--update"):
       OTAUpdate(arg)
       sys.exit()
    elif opt in ("-u", "--feature"):
       OTAInstallFeature(arg)
       sys.exit()
    elif opt in ("-a", "--ack"):
       OTAAck(arg)
       sys.exit()
    elif opt in ("-s", "--status"):
       OTAStatus()
       sys.exit()
    elif opt in ("-h", "--help"):
      Usage()
      sys.exit()
    else:
      Usage()
      sys.exit(2)

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

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

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

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

def OTAUpdate( version ):
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()

    print 'Beginning update to ' + version + ":   Query status to see install state"

    #autodownload/install by default, could use UpdateInitiateType
    print client.OTA_UpdateInitiate(version)
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def OTAInstallFeature( feature ):
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()

    print 'Installing Feature ' + feature + ":   Query status to see install state"

    #autodownload/install by default
    print client.OTA_UpdateInitiateFeature(feature)
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def OTAAck( type ):
  try:
    transport = TSocket.TSocket('127.0.0.1', 9090)
    transport = TTransport.TBufferedTransport(transport)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Ice.Client(protocol)
    transport.open()

    status = client.OTA_Status()
    
    if(type == "cancel"):
        print client.OTA_Ack(status.session_id, eAckType.CANCEL)
    elif (type == "download"):
        print client.OTA_Ack(status.session_id, eAckType.ACCEPT_DOWNLOAD)
    elif (type == "install"):
        print client.OTA_Ack(status.session_id, eAckType.ACCEPT_INSTALL)
    elif (type == "notify"):
        print client.OTA_Ack(status.session_id, eAckType.ACCEPT_NOTIFY)
    elif (type == "list"):
        print client.OTA_Ack(status.session_id, eAckType.ACCEPT_LIST)
    else:
        print ("Invalid ack type")
 
    transport.close()
 
  except Thrift.TException, tx:
    print str(tx)

def Usage():
	print 'ice-ota [option]'
	print '   --refresh           Refreshes List of Updates from server'
	print '   --list              Lists updates available'
	print '   --update [version]  Update to version X.X.X.X'
	print '   --ack [type]        Sends ack of [download|install|notify|list|cancel]'
	print '   --status            Shows install status'
	print '   --feature [package] Installs feature package via OTA'

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