#!/bin/sh

export LD_LIBRARY_PATH=/usr/local/lib

# frpanel_update is used to monitor and update versions on the pc93
# The function will look in /usr/local/lib/frpanel for an update file
# and will compare that version to the current version on the system.
# If the frpanel needs to be updated it will re-program the PC93 to 
# the newest version.  This all needs to happen after the frpanel 
# module has been instered and before any applications start.
frpanel_update ()
{
   TAGNAME="frpanel_update"
   #path to the hex file for frpanel update
   UPDATEPATH=/usr/local/lib/frpanel/
   
   #file descriptor for check frpanel version
   VERSIONFILE="/proc/driver/frpanel/version"
   
   #if the frpanel should be updated, assume FALSE
   UPDATE="FALSE"
      
   #check if there is an update file
   UPDATEFILELIST=$(/usr/bin/find $UPDATEPATH -name "PC93_code_v*.hex")
   
   UPDATEVERSION=0
   
   #parse out the version number from the file with the highest version number
   if [ ${#UPDATEFILELIST} -gt 0 ]
   then
      for I in $UPDATEFILELIST ; do   
         FILE=${I##*/}
         FILENAME=${FILE%%.*}
         TEMPVERSION=${FILENAME##P*v}
         if [ $TEMPVERSION -ge $UPDATEVERSION ]
         then
            UPDATEFILE=$I
            UPDATEVERSION=$TEMPVERSION
         fi              
      done
   fi       
   
   #if the version file is not there, the frpanel did not load or it's an old version
   if [ ${#UPDATEFILE} -gt 0 ] && [ -e $VERSIONFILE ] 
   then
             
      DEV=$(cut -d' ' -f1 $VERSIONFILE)
      if [ "$DEV" == "PC93" ]
      then
         CURRENTVERSION=$(cut -d' ' -f2 $VERSIONFILE)      
         #echo FOUND $DEV FRPANEL VERSION $CURRENTVERSION
         OLDVERSION=$CURRENTVERSION

         #check if the update version is newer then the current version
         if [ $UPDATEVERSION -le $CURRENTVERSION ]
         then
            UPDATE="FALSE"
         else
            UPDATE="TRUE"
         fi
      fi          
   fi

   if [ "$UPDATE" == "TRUE" ]
   then
      #toggle must be after because of bootloader bug
      /usr/local/sbin/dev_update -d PC93 -i $UPDATEFILE -s toggle
      CURRENTVERSION=$(cut -d' ' -f2 $VERSIONFILE)
            
      if [ $UPDATEVERSION -eq $CURRENTVERSION ]
      then
         logger -t "$TAGNAME" "Updated frpanel from version $OLDVERSION to $CURRENTVERSION"
      else
         logger -t "$TAGNAME" "Failed to update to version $UPDATEVERSION"
      fi 
    else
       logger -t "$TAGNAME" "Frpanel up to date"
   fi
}

case "$1" in
  start|restart)
    # Read variable for sysconfig and load all mentioned modules
    echo -n "Loading required kernel modules... "
    if [ -f /etc/sysconfig/kernel ]; then
      . /etc/sysconfig/kernel
      for I in $MODULES_LOADED_ON_BOOT ; do
#	    if ! modprobe -n $I > /dev/null 2>&1 ; then continue ; fi
	      modprobe $I;
      done
      echo "done"
   fi
   #if this is a PC93 product check if it needs to update
   if [ "$USES_PC93" == "TRUE" ]
   then
      frpanel_update
   fi
    #rc_status -v1 -r
   ;;
  stop)
# Use with caution.  Some Adtec modules do not exit correctly and crash the system casuing reboot to fail.
# iptv module removal hangs the system
#    . /etc/sysconfig/kernel
#    for ((i=${#MODULES_LOADED_ON_BOOT[@]}-1; i>=0; i--)); do 
#       modprobe -r ${MODULES_LOADED_ON_BOOT[$i]}
#    done
# trying just frpanel
    rmmod frpanel 
    ;;
  status)
    #rc_failed 4
    #rc_status -v
    ;;
  *)
    echo "Usage: $0 {start|stop|status|restart}"
    exit 1
    ;;
esac
