#! /usr/bin/env python
# Copyright (c) 2015 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

import shutil
import optparse
import os

import QuickTrace
import Tac
import Tracing
from AlertBaseImporter import AlertBaseImporter

t0 = Tracing.trace0
Tracing.traceSettingIs( ",".join(
      [ os.environ.get( "TRACE", "" ) ] + [ 'AlertBaseImporter/*' ] ) )

qv = QuickTrace.Var
qt0 = QuickTrace.trace0

defaultPath = '/persist/sys/AlertBase.json'

def main():
   t0( "Running AlertBaseImporter" )
   qt0( "Running AlertBaseImporter" )
   parser = optparse.OptionParser( usage="usage: %prog OPTIONS" )
   parser.add_option( "-s", "--sysname", action="store", default="ar",
                      help="System name (default: %default)" )

   parser.add_option( "-l", "--load", action="store", default=defaultPath,
                      help="Absolute path to AlertBase " )
   (options, args) = parser.parse_args()
   if args:
      parser.error( "Unexpected arguments" )

   sysname = options.sysname
   alertBaseFile = options.load

   if not os.path.isfile( alertBaseFile ):
      print "AlertBase (", alertBaseFile, ") doesn't exist"
      exit( 1 )

   alertBaseImporter = AlertBaseImporter( alertBaseFile, sysname )
   alertBaseImporter.loadAlertBase()
   if alertBaseFile != defaultPath and os.path.isdir( "/persist/sys" ):
      shutil.copyfile( alertBaseFile, defaultPath )

if __name__ == '__main__':
   main()
   
