#!/usr/bin/env python3

# Copyright (c) 2019 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

'''
Publish the value of PORT_NUMBERING_SCHEME in boot-to /etc/port-numbering.
/etc/port-numbering is created only if PORT_NUMBERING_SCHEME is set.
'''

import os
import re

BOOT_CONFIG_FILE = '/mnt/flash/boot-config'
PORT_NUMBERING_FILE = '/etc/port-numbering'

def main():
   if not os.path.isfile( BOOT_CONFIG_FILE ):
      return

   with open( BOOT_CONFIG_FILE ) as f:
      bootConfig = f.read()

   portNumRe = re.compile( '^PORT_NUMBERING_SCHEME=(.*)$',
                           flags=re.IGNORECASE|re.MULTILINE )
   reMatch = re.search( portNumRe, bootConfig )
   if not reMatch:
      return

   portNumberingScheme = reMatch.group( 1 )
   with open( PORT_NUMBERING_FILE, 'w' ) as f:
      # pylint: disable-next=consider-using-f-string
      f.write( "PORT_NUMBERING_SCHEME=%s\n" % portNumberingScheme )

   os.chmod( PORT_NUMBERING_FILE, 0o0644 )

if __name__ == "__main__":
   main()
