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

import Tac, sys, os, optparse, Tracing
import FdlIo

def p(*args):
   print args
   
t00 = p
t0 = Tracing.trace0
t1 = Tracing.trace1
t2 = Tracing.trace2

def parseOptions():
   parser = optparse.OptionParser()
   parser.add_option( "-t", "--test", action="store_true",
                      help="Run in diagnostic test mode" )
   parser.add_option( "-d", "--dir", action="store",
                      help="Where to write output files: (default: %default)",
                      default="/etc" )
   parser.add_option( "--sysname", dest="sysname", action="store",
                      type="string", default="ar",
                      help="Sysname (default=%default)" )
   parser.add_option( "-i", "--idpromOnly", action="store_true",
                      help="Read idprom only", default=False )
   return parser.parse_args()

def writeFdlFile( outputDir, fdl ):
   def path( filename ):
      return outputDir + "/" + filename
   if fdl:
      file( path('fdl'), 'w' ).write( fdl )
   else:
      if os.path.exists( path('fdl') ):
         os.unlink( path('fdl') )

def main():
   ( options, args ) = parseOptions()
   if args:
      print "Unexpected arguments."
      return 1

   fdl = FdlIo.readFdl( options.sysname, idpromOnly=options.idpromOnly )
   
   assert fdl, "Error reading fdl"

   if not options.test:
      # Now write the fdl
      writeFdlFile( options.dir, fdl )

   return 0

if __name__ == "__main__":
   sys.exit( main() )
