####################################################################################
#
#   This program is designed to run on a Digi TransPort router at startup. It will 
#   look for a CSV file on the USB flash stick and program in site specific
#   parameters that match the serial number.
#
#   Normally an autoexec.bat file (on the USB stick inserted) will have just
#   flashed the router with a .ALL file and rebooted it.
#
#   Note that a .ALL file is a complete image of the router's flash. It can be
#   extracted from a router using the Flashwriter tool:
#
#   http://ftp1.digi.com/support/firmware/FlashWriter.msi
#
#   The all file loaded by autoexec.bat (WR21.all in example below) must have been
#   prepared with the following commands:
#
#	cmd 0 autocmd "python setup.py"
#	usbcon 0 batfile off
#	config 0 save
#
#   In addition this program setup.py must be in the root of the .ALL file.
#
#   Also ensure that the usb flash drive contains the params.csv
#   file and the autoexec.bat script.
#   
#   Example params.csv file:
#
#   serial,eth 0 ipaddr,eth 0 mask,eroute 0 ourid
#   123456,192.168.50.8,255.255.0.0,Site A
#   875103,192.168.50.9,255.255.255.240,Site B
#   201307,192.168.50.10,255.255.255.0,Site C
#
#   Example autoexec.bat file:
#
#   ERROR_EXIT
#   copy u:WR21.all all.all
#   scanr
#   reboot
#
#   Author: Alistair Craven Digi International Ltd 19/09/2013
####################################################################################

import time
import csv
from BaseWizard import write_cli

try:
    answer = write_cli('setevent "Starting setup.py program"')
    time.sleep(2) #Wait to allow USB to enumerate
    d = csv.DictReader(open('u:params.csv'),dialect='excel')
    answer = write_cli('id')    
    pos1 = answer.find("Ser#:")
    if pos1 > 0:
        serial = answer[pos1+5:pos1+6+5]
    else:
        answer = write_cli('setevent "ERROR EXIT: SERIAL NUMBER NOT FOUND"')
        quit()

    for row in d:
        if row['serial'] == serial:
            for name in d.fieldnames:
                if name != 'serial':
                    print name + ' "' + row[name] + '"'
                    answer = write_cli (name + ' "' + row[name] + '"')
                    if not "OK" in answer:
                        answer = write_cli('setevent "ERROR EXIT: OK NOT RECEIVED FOR' + name + '"')
                        
    answer = write_cli('cmd 0 autocmd "usbcon 0 batfile !"')
    if not "OK" in answer:
        answer = write_cli('setevent "ERROR EXIT: OK NOT RECEIVED FOR usbcon 0 batfile !"')
        quit()
    answer = write_cli('config 0 save')
    if not "OK" in answer:
        answer = write_cli('setevent "ERROR EXIT: OK NOT RECEIVED FOR config 0 save"')
        quit()

    write_cli('flashleds')
    write_cli('setevent "setup.py completed successfully"')

except:
	print 'Python script terminated due to error'
	write_cli('setevent "Python script terminated due to error"')