#!/bin/sh

MOUNT='mount /dev/mtdblock/3 /onboardflash'
#MOUNT=''
AWK='/onboardflash/root/tools/busybox awk' 
#AWK='/usr/bin/awk'  

UMOUNT='umount /onboardflash'
#UMOUNT=''

DST_LIVE="/onboardflash/root/live/configs/endpoint.xml"
#DST_LIVE="./testout/live/endpoint.xml"
DST_BACK="/onboardflash/root/backup/configs/endpoint.xml"
#DST_BACK="./testout/backup/endpoint.xml"

SRC_XML="/mnt/root2/xml/endpoint.xml"
#SRC_XML="./xml/endpoint.xml"

SRC_UUIDS="/mnt/root2/uuids"
#SRC_UUIDS="./uuids"
  
LOGFILE="/mnt/root2/out.log"
#LOGFILE="./out.log"

MACLOG='/mnt/root2/maclog.log'
#MACLOG='./maclog.log'

#Rather than printing the console out we log
#it to the USB drive in /log/__MAC__.log
MAC=`ifconfig | grep HWaddr | cut -d' ' -f11 | sed -e s/://g`
LOG="/mnt/root2/production_log/$MAC.log"

###################
###START PROGRAM
##################

mkdir -p /mnt/root2/production_log

if [ -e /updated-checkusb ]; then
    echo "check usb script already updated" >> $LOG
else                                       
    echo "updating check-usb-script" >> $LOG
    cp /mnt/root/check-usb-update /bin/check-usb-update
    echo "updated" > /updated-checkusb
    save  
    
    reboot    
fi

if [ -e /gp_boot.sh ];then
    echo "Board already programming.  Configuring..." >> $LOG
else            
    . /ts7xxx.subr
    cd /
    #insmod -q -f /sdcard.o dmaenable=1
    #mount /dev/sdcard0/disc0/part3 /mnt/sdmnt
    sdmount
    cd /mnt/root/reset7400
    ./reset7400.sh
    reboot
fi
#Mount the onboard flash so we can put the endpoint files in place
$MOUNT

UUIDFILE=`ls -1 $SRC_UUIDS/*.uuid | head -n 1`
if [ ! -f $UUIDFILE ] || [ -z $UUIDFILE ]; then
	echo -n "Please notify a Process Engineer:"
	echo "No un-used uuid file found, cannot configure board, exiting"
	exit 1
fi

#get the uuid for the user and password
UUIDUSER=`cat $UUIDFILE | cut -d ',' -f 1`
UUIDPW=`cat $UUIDFILE | cut -d ',' -f 2`                                     
if [ -z $UUIDUSER ] || [ -z $UUIDPW ]; then
	echo -n "Please notify a Process Engineer:"
	echo "No valid username or password found in UUID file $UUIDFILE, cannot configure board, exiting"
	exit 1
fi

#get the mac address of the unit
MACIF=`ifconfig eth0 | grep HWaddr | /onboardflash/root/tools/busybox grep -o -E '[0-9A-F\:][0-9A-F\:][0-9A-F\:][0-9A-F\:]+'`                 
#MACIF=`ifconfig en0 | grep ether | grep -o -E '[0-9a-f\:][0-9a-f\:][0-9a-f\:][0-9a-f\:]+'`

#test to see if we have already burned a uuid pair for this board
MACEXISTS=`cat $LOGFILE | grep $MACIF -c`

if [ $MACEXISTS -gt 0 ]; then
    echo "MAC $MACIF has already been programmed.  Exiting..." >> $LOG
    exit 1
else
    echo "MAC $MACIF needs programming...continuing" >> $LOG
fi

# if we are here, we haven't configured this board yet
echo "User: $UUIDUSER" | tee -a $LOG
echo "Pass: $UUIDPW" | tee -a $LOG  
echo "MAC: $MACIF" | tee -a $LOG

#put the the and backup files into place
cat $SRC_XML | $AWK '{sub(/VVVV/,"'${UUIDUSER}'");sub(/PPPP/,"'${UUIDPW}'");print}' > $DST_LIVE
cat $SRC_XML | $AWK '{sub(/VVVV/,"'${UUIDUSER}'");sub(/PPPP/,"'${UUIDPW}'");print}' > $DST_BACK

#log what we've done
NOW=`date`
echo "RecordMarker,$MACIF,$NOW,$UUIDUSER,$UUIDPW" >> $LOGFILE
echo "RecordMarker,$MACIF,$NOW,$UUIDUSER,$UUIDPW" | tee -a $LOG

#archive the uuid pair that was just used to program the board
`mv $UUIDFILE $SRC_UUIDS/used_uuids`  
echo "$MACIF" >> $MACLOG
#unmount the onboard flash
$UMOUNT
