#insmod /home/gps/bin/tkusb.ko
#modprobe hal_x86
#modprobe pehci
#Startup eth0
(ifconfig eth0 down; udhcpc -i eth0 -b -T 3 -t 3) &

depmod
modprobe omap_bl
modprobe omap_jtag
modprobe pcinet
rmmod pcinet
modprobe pcinet

#check this twice if necessary
checkip=1
while true
do
  #check ip of digital board
  ifconfig eth1 192.168.201.221 up
  sleep 30  #sleep 30 seconds to establish ethernet connection
  if (ping -c 1 192.168.201.222 | grep -i "1 packets transmitted, 1 packets received" )
  then
    ipadd=192.168.201.222
    break
  else
    ifconfig eth1 down
    ifconfig eth1 192.168.2.1 up

    sleep 30 #sleep 30 seconds to establish ethernet connection

    if (ping -c 1 192.168.2.2 | grep -i "1 packets transmitted, 1 packets received" )
    then
      #setup old ip address for digital board update
      #will reset it to 192.168.201.221 after update
      ipadd=192.168.2.2
      break
    else
      let "checkip-=1"
      if [ $checkip -lt 0 ]
      then
        echo "halting system"
        halt  #if the digital board doesn't start then halt, not sure what else to do
      else 
        #reset digital board
        echo "reboot the digital board"
        /home/gps/bin/pci_testr
      fi
    fi
  fi
done
echo "Digital Board IP address is $ipadd"

export LD_LIBRARY_PATH=/opt/qt-embedded/lib
export QWS_SIZE=800x600
mkdir -p /USER
e2fsck -y /dev/mmcblk0p2
mount -o noatime /dev/mmcblk0p2 /USER
mkdir -p /USER/Routes 
mkdir -p /USER/almanacs
mkdir -p /USER/Settings
mkdir -p /USER/Nmea
mkdir -p /USER/Kml
# Ensure that the airport file is on the SD Card.
cp /home/gps/bin/airports.csv /USER

#update software
if [ -f /root/.sync ]; then

  firmwareUpdate=0  #firmware update has not been performed

  #display update image
  cd /home/gps/bin   
  chmod 755 ./fbi
  ./fbi -d /dev/fb0 -1 -t 1 logo_linux_clut224c.bmp
                                                                                  
  umount /var
  
  digtimeout=6  #wait up to 60 seconds for the digital board
  digreset=1  #reset 1 time
  while true                                                                             
  do                                                      
     if (ping -c 1 $ipadd | grep -i "1 packets transmitted, 1 packets received" )
     then 
        #update digital board
        echo "Update digital board"
                
        if [ -f /home/gps/bin/CleanSlot0.py ]; then
           echo "CleanSlot0.py present"
           python /home/gps/bin/CleanSlot0.py        
        elif [ -f /home/gps/bin/CleanSlot0 ]; then
           echo "CleanSlot0 present"
           /home/gps/bin/CleanSlot0
        else
           echo "CleanSlot0.py and CleanSlot0 NOT present"
           (
           sleep 2
           echo "mount -o remount,rw /"
           sleep 2
           echo "rm -rf /USER/rpm"
           sleep 5
           echo "mkdir -p /USER/rpm"
           sleep 2
           echo "depmod"
           sleep 2
           echo "sync"
           sleep 2
           ) | telnet Digital-Board
        fi
        
        if [ -f /home/gps/bin/updateSlot0.py ]; then
           echo "updateSlot0.py present"
           python /home/gps/bin/updateSlot0.py        
        else
           echo "updateSlot0 present"           
           /home/gps/bin/updateSlot0
        fi
        break
     else
        let "digtimeout-=1"
        if [ $digtimeout -lt 0 ] 
        then
           echo "***********UNABLE TO UPDATE DIGITAL BOARD***********"
           echo "***********     CANNOT PING $ipadd     ***********"
           
           if [  $digreset -gt 0 ]
           then
              let "digreset-=1"
              #Reset digital board and try again
              echo "***********       RESET DIGITAL BOARD       ***********"
              digtimeout=6
              /home/gps/bin/pci_testr
              sleep 10
           else
              break
           fi
        else
           sleep 10
        fi
     fi  
  done

  #update firmware    
  if [ -f /USER/firmware/gpsFirmware.rpd ]; then
     echo "Starting firmware update please wait ..."
     let "firmwareUpdate=1"
     digtimeout=6 #wait up to 60 seconds for the digital board
     digreset=1  #reset 1 time
     
     cp /USER/firmware/gpsFirmware.rpd /home/gps/bin/gpsFirmware.rpd
     cd /home/gps/bin
     #chmod 777 ./fbi
     rm -f ./lastupdate
     ./fbi -d /dev/fb0 -1 -t 1 logo_linux_clut224a.bmp
     
     while true                                                                             
     do      
        if (ping -c 1 $ipadd | grep -i "1 packets transmitted, 1 packets received" )
        then 
           ./loadertest2 || date > lastupdate  &           
           ./fbi -d /dev/fb0 -1 -t 2 -l ./images.txt > /dev/null
   
           until [ -f ./lastupdate ]
           do
             sleep 5 
           done
   
           if [ -f /home/gps/bin/fpgaError.txt ]; then
              echo "Error updating firmware, leaving the firmware update file alone."
           else
              rm /home/gps/bin/gpsFirmware.rpd
              rm /USER/firmware/gpsFirmware.rpd
              #cd /home/gps/bin
              #chmod 777 ./fbi
              #./fbi -d /dev/fb0 -1 -t 1 logo_linux_clut224b.bmp     
           fi
           
           break
        else
           let "digtimeout-=1"
           if [ $digtimeout -lt 0 ] 
           then
              echo "***********UNABLE TO PERFORM FIRMWARE UPDATE***********"
              echo "***********     CANNOT PING $ipadd     ***********"
                         
              if [  $digreset -gt 0 ]
              then
                 let "digreset-=1"
                 #Reset digital board and try again
                 echo "***********       RESET DIGITAL BOARD       ***********"
                 digtimeout=6
                 /home/gps/bin/pci_testr
                 sleep 10
              else
                 break
              fi
           else
              sleep 10
           fi     
        fi
     done
  fi
                                                
  #display update image
  cd /home/gps/bin   
  ./fbi -d /dev/fb0 -1 -t 1 logo_linux_clut224c.bmp
                                                 
  echo "Starting software update please wait ..."  

  #update slot1 application
  echo "Update slot1"
  cd /USER/rpm
  rpm -Uvh --force *.rpm
  sync
  
  rm -rf /root/.sync
  rm -Rf /USER/space/cp/*
  mount -a 
  
  echo "Update Done..."
  
  #if firmware updated then display reboot screen
  if [ $firmwareUpdate -eq 1 ]; then
    #display reboot image
    echo "Display reboot image"
    cd /home/gps/bin
    ./fbi -d /dev/fb0 -1 -t 1 logo_linux_clut224b.bmp
    
    #exit script
    echo "Exiting startup script"
    exit 1
  fi
  
fi

#Reset IP address if necessary 
if [ $ipadd == 192.168.2.2 ]
then
  #change IP address back to 192.168.201.221
  ipadd=192.168.201.222
  ifconfig eth1 down
  ifconfig eth1 192.168.201.221 up
    
  #reset digital board
  /home/gps/bin/pci_testr
  sleep 10
    
  echo "Digital Board IP address has changed to $ipadd"
fi

#force removal of system.xml
if [ -f /space/cp/target/ARM/rci/xml/system.xml ]; then
  rm /space/cp/target/ARM/rci/xml/system.xml
fi

swapon /dev/mmcblk0p1

#moved this for the new touchscreen controller since
#there are changes to the evars file
source /etc/evars

#start the touch screen driver
/usr/bin/eGTouchD &

rsync --daemon     
ulimit -s 2048
mkdir -p /opt
cd /opt; ln -sf /space/cp/arm/qt-embedded qt-embedded
cd /opt; ln -sf /space/cp/target/ARM/engines/ engines
cd /opt; ln -sf /space/cp/UI/ UI

#Read home button
homebutton=`/home/gps/bin/i2c_read 3 0x62 0x04 1`
hexhomebutton="0x"${homebutton}
#Check if homebutton is depressed
dotouchcal=$(( $hexhomebutton & 0x20 ? 1 : 0 ))
if [ $dotouchcal == 1 ]; then
  echo "Write out .touchcal"
  touch /space/cp/.touchcal
fi

if [ -f /space/cp/.touchcal ]; then       
  sleep 15
  echo "Starting touchscreen cal Please Wait ..."
  rm -rf /etc/pointercal-2
  ts_calibrate
  rm -rf /space/cp/.touchcal
  cat /etc/pointercal-2                              
fi

#Handhelds Slot-1 controller - Backplane TCXO & Pullups
#Check to see if the i2c DS4520 part has the backplane clock on
#If it is on, turn it off.
# 0100E101 = Check if on, turn off
# 0000E001 = Check if off, turn on
mytemp=`/home/gps/bin/i2c_read 3 0x51 0xf0 4`
I2CBYTES=${mytemp//[[:space:]]}
if [ $I2CBYTES != "0100E101" ]; then
   echo "Programming DS4520 TCXO/Pullups..."
   /home/gps/bin/i2c_write 3 0x51 0xf4 0x0
   sleep 1
# 3 0x51 0xf0 0 0 0xe0 0x01 = Turn clock on
# 3 0x51 0xf0 1 0 0xe1 0x01 = Turn clock off
   /home/gps/bin/i2c_write 3 0x51 0xf0 0 0 0xe0 0x01
   /home/gps/bin/i2c_write 3 0x51 0xf4 0x01
fi

/home/gps/bin/checklog &
python /home/gps/bin/copyDigBrdSlot1IP.py &
export LD_LIBRARY_PATH=/space/cp/arm/ACE_wrappers/lib:/space/cp/UI/:/space/cp/arm/qt-embedded/lib:/space/cp/arm/expat/lib:/space/cp/arm/dbus/lib:/space/cp/arm/DDS/lib:/space/cp/arm/sqlite/lib:/space/cp/arm/zlib/lib:/space/cp/target/ARM/engines/lib
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/space/cp/home/gps/bin:/space/cp/arm/ACE_wrappers/bin:/space/cp/arm/DDS/bin:/space/cp/arm/ACE_wrappers/TAO/orbsvcs/Naming_Service/:/space/cp/target/ARM/engines/lib:/space/cp/UI/:/space/cp/target/ARM/UI/plugins:/space/cp/arm/dbus/bin:/space/cp/arm/expat/bin:/space/cp/arm/sqlite/bin
export QT_PLUGIN_PATH=/space/cp/target/ARM/UI/plugins
cd /space/cp/target/ARM/engines/lib
echo 70bf6735fe623d0835da9c4b3a50791c > /space/cp/arm/dbus/var/lib/dbus/machine-id 
sync
tao_cosnaming -ORBListenEndpoints iiop://192.168.201.221:2700 &
sleep 3                                                                                                                                                         
mount -o remount,rw /                                                                                                                                            
rm -f /space/cp/target/ARM/engines/lib/repo.ior                                                                                                                  
DCPSInfoRepo -NOBITS -ORBListenEndpoints iiop://192.168.201.221:12345  -d /space/cp/target/ARM/engines/lib/domain_ids -ORBSvcConf /space/cp/target/ARM/engines/lib/tcp.conf &
timeout=15                                                                             
while true                                                                             
do                                                                                        
  if [ -f /space/cp/target/ARM/engines/lib/repo.ior ]                                  
  then                                                                                 
     break                                                                             
  else                                                                                 
     echo "waiting for repo.ior"                                                       
     sleep 1                                                                           
     let "timeout-=1"                                                                  
     if [ $timeout -lt 0 ]                                                                                                                                                                                       
     then                                                                                                                                                                                                        
        echo "TIMEOUT - could not find repo.ior"                                                                                                                                                                 
        break                                                                                                                                                                                                    
     fi                                                                                                                                                                                                          
   fi                                                                                                                                                                                                            
done                  
sleep 1                                                                                                                                                                                                                                                                                                                                                                                                   
sync
digtimeout=10
while true                                                                             
do                                                      
  if (ping -c 1 $ipadd | grep -i "1 packets transmitted, 1 packets received" ) then                                  
     break                                                                     
  else                                                                                 
     echo "waiting for digital board"                                                       
     sleep 1                                                                           
     let "digtimeout-=1"                                                                  
     if [ $digtimeout -lt 0 ]                                                                                                                                                                                       
     then                                                                                                                                                                                                        
        echo "TIMEOUT - could not find digital board"   	
        echo "*************** RESETTING DIGITAL BOARD! *******************"
		/home/gps/bin/pci_testr
		resettimer=60
		while true
		do
		  if (ping -c 1 $ipadd | grep -i "1 packets transmitted, 1 packets received" ) then   
		     echo "************* DIGITAL BOARD UP! ***********************"                               
     		 break                                                                     
 		  else
 		     sleep 1
 		     let "resettimer-=1"
 		     if [ $resettimer -lt 0 ]
 	        then
 	     
              #copy log file to usb stick in case the digital board never comes up
              usb_mount=`mount | grep /dev/sda1 | cut -d' ' -f3`
              if [ "$usb_mount" = "/media/usb_aeroflex" ]; then
                 mount -o remount,rw /media/usb_aeroflex
                 cp /USER/log/.lastrs232.txt /media/usb_aeroflex/gps1000last_rs232.txt  
                 cp /dev/shm/.rs232output.txt /media/usb_aeroflex/gps1000rs232.txt
                 mount -o remount,ro /media/usb_aeroflex     
              fi
              
 		        echo "********** DIGITAL BOARD NEVER CAME UP! *****************"
 		        break
 		     fi
 		  fi                                                                                                                                                          
        done                                                                                                                                                                                             
     fi                                                                                                                                                                                                          
   fi                                                                                                                                                                                                            
done 

echo "BEFORE digital server ########"
/home/gps/bin/loaddigital 
echo "AFTER digital server ########"
sync
sleep 3
killall syslogd
sleep 3
#copy log file to usb stick
usb_mount=`mount | grep /dev/sda1 | cut -d' ' -f3`
if [ "$usb_mount" = "/media/usb_aeroflex" ]; then
  mount -o remount,rw /media/usb_aeroflex
  cp /USER/log/.lastrs232.txt /media/usb_aeroflex/gps1000last_rs232.txt  
  cp /dev/shm/.rs232output.txt /media/usb_aeroflex/gps1000rs232.txt
  mount -o remount,ro /media/usb_aeroflex     
fi
mkdir -p /media
mount -o remount,ro /
./hardware_enumeration.engine -ORBInitRef NameService=corbaloc:iiop:192.168.201.221:2700/NameService -ORBSvcConf /space/cp/target/ARM/engines/lib/tcp.conf -ORBListenEndpoints iiop://192.168.201.221 -DCPSConfigFile /space/cp/target/ARM/engines/lib/dds.ini&
