#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   SetUBootEnvVar()
#
#   Function sets the environmental variables in the UBoot.
#
#   \param[in]     VarTftpDownload
#   \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     29.08.2013  STW/A.Appelt
#
#-----------------------------------------------------------------------------

function SetUBootEnvVar()
{
   # start buffering RS232 traffic
   OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
   sleep 0.1

   if [ "$VarPrepareNandFlashOnly" == false ]; then
      # Check if VarTftpDownload == true
      if [ "$1" == true ]; then
         # set env. variables for update via tftp
         if [ "$VarGatewayIP" != "can stay empty" ]; then
            echo "setenv gatewayip $VarGatewayIP" > $serial_port
            sleep 0.1
         fi
         echo "setenv ipaddr $VarClientIP" > $serial_port
         sleep 0.1
         echo "setenv netmask $VarNetMask" > $serial_port
         sleep 0.1
         echo "setenv serverip $VarServerIP" > $serial_port
         sleep 0.1

         echo "setenv update_en_tftp 1" > $serial_port
         sleep 0.1
         echo "setenv update_en_usb 0" > $serial_port
         sleep 0.1

      else
         # set env. variables for update via USB
         echo "setenv update_en_tftp 0" > $serial_port
         sleep 0.1
         echo "setenv update_en_usb 1" > $serial_port
         sleep 0.1
      fi

      echo "setenv update_file $VarImageName" > $serial_port
      sleep 0.1
      echo "setenv update_pswd $VarFitPassword" > $serial_port
      sleep 0.1
   else
      echo "" > $serial_port
   fi

   if [ "$VarPrepareNandFlash" == true ]; then
      # Set rootfs rw + overlay off
      echo "setenv ubifsargs setenv bootargs console=ttyPSC3,115200 ubi.mtd=0 root=ubi0:rootfs rw rootfstype=ubifs quiet overlay_off" > $serial_port
      sleep 0.1
   fi

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

   if [ "$VarPrepareNandFlashOnly" == false ]; then
      # start buffering RS232 traffic
      OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
      sleep 0.1

      # save env. variables
      echo "saveenv" > $serial_port

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

      echo -e "UBoot variables are saved.\n"
   fi

   # start buffering RS232 traffic
   cat $serial_port | tee "$script_dir/$VarTmpFile" &
   sleep 0.1

   if [ "$VarPrepareNandFlashOnly" == false ]; then
      # perform reset
      echo "reset" > $serial_port
      echo -e "Performing update...\n"
   else
      # boot system
      echo "boot" > $serial_port
      echo -e "Booting $VarTargetName...\n"
   fi

return 0
}
