#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   ClearUBootEnvVar()
#
#   Function clears the UBoot environment variables.
#
#   \param[in]     none         
#   \param[out]    none       
#   \param[in,out] none       
#
#   \return
#   possible return value(s) and description
#   return 0 -> C_NO_ERROR
#   return 1 -> C_UNKOWN_ERROR
# 
#   \created    30.08.2013  STW/A.Appelt
#
#-----------------------------------------------------------------------------

function ClearUBootEnvVar() 
{
   OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
   sleep 0.5   

   # clear update status
   echo "stw_eep -w udstat 0" > $serial_port
   
   # wait for TC3 promt (this function can be found in the 'EnterUBoot' script)
   if (! waitForPromt "#")
   then
      echo -e "\nError: Timeout!"
      echo -e "Unable to find TC3 promt.\n"
      return 1
   fi   
   
   sync
   wait $!

   # restart TC3
   StartTime=$(date +%s)
   while ( true )
   do
      CurrTime=$(date +%s)
      TimeDiff=$(( $CurrTime - $StartTime ))
      OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
      sleep 0.5       
      echo -e "Sending reboot...\n"
      echo "reboot" > $serial_port
      sleep 0.1
      if [ $TimeDiff -ge 300 ]; 
      then
         killall cat & > /dev/null &
         echo -e "\nError: Timeout!"
         echo -e "Unable to reboot TC3.\n"
         return 1
      elif ( waitForPromt "stop:" )
      then
         break
      fi     
   done
   
   # Enter the UBoot (this function can be found in the 'EnterUBoot' script)
   if (! EnterUBoot "soft_reset")
   then
      echo -e "\033[31mError: Could not enter the UBoot!\n\033[37m"
      exit
   fi

   # start buffering RS232 traffic
   OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
   sleep 0.1    
  
   # clear variables
   echo "setenv update_en_tftp" > $serial_port
   sleep 0.1
   echo "setenv update_en_usb" > $serial_port
   sleep 0.1
   echo "setenv update_file" > $serial_port
   sleep 0.1
   echo "setenv update_pswd" > $serial_port
   sleep 0.1

   # Check if VarRootFileSystemStatus == true
   if [ "$2" == true ]; 
   then
      # set Rootfs Read/Write
      echo "setenv ubifsargs setenv bootargs console=ttyPSC3,115200 ubi.mtd=0 root=ubi0:rootfs rw rootfstype=ubifs quiet" > $serial_port
   else
      # set Rootfs Read only
      echo "setenv ubifsargs setenv bootargs console=ttyPSC3,115200 ubi.mtd=0 root=ubi0:rootfs ro rootfstype=ubifs quiet" > $serial_port
   fi

   # wait for UBoot promt (this function can be found int the 'EnterUBoot' script)
   if (! waitForPromt "=>")
   then
      echo -e "\nError: Timeout!"
      echo -e "Unable to find UBoot promt.\n"
      return 1
   fi  

   # start buffering RS232 traffic
   OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
   sleep 0.1      
   
   # save variables
   echo "saveenv" > $serial_port

   # wait for UBoot promt (this function can be found int the 'EnterUBoot' script)
   if (! waitForPromt "=>")
   then
      echo -e "\nError: Timeout!"
      echo -e "Unable to find UBoot promt.\n"
      return 1
   fi

   # restart TC3
   echo "reset" > $serial_port
   
   echo -e "UBoot variables are cleared.\n" 

   # clear config files, fit image and $VarTmpFile
   OUTPUT=`rm $script_dir/$VarTmpFile`
   OUTPUT=`rm $script_dir/$VarFitConfigFile`
   OUTPUT=`rm -R $VarTftpFolder`
   OUTPUT=`rm $VarDataFolderPath/$VarUBootEnvFile`
   if [ "$1" == true ]; 
   then
      OUTPUT=`rm $script_dir/$VarTftpConfigFile`
   fi

   echo -e "Removed temporarily files.\n" 

return 0
}
