#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   ClearUBootEnvVar()
#
#   Function clears the UBoot environment variables.
#
#   \param[in]     VarRootFileSystemStatus
#   \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 $VarTargetName promt.\n"
      return 1
   fi

   sync
   wait $!

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

   # clear variables
   echo "/usr/local/bin/fw_setenv update_en_tftp" > $serial_port
   sleep 0.1
   echo "/usr/local/bin/fw_setenv update_en_usb" > $serial_port
   sleep 0.1
   echo "/usr/local/bin/fw_setenv update_file" > $serial_port
   sleep 0.1
   echo "/usr/local/bin/fw_setenv update_pswd" > $serial_port
   sleep 0.1

   # Get rootfs status + overlay status
   ( "$VarRootFileSystemStatus" == true ) && VarRootFileSystemStatus=rw    || VarRootFileSystemStatus=ro
   ( "$VarOverlayStatus" == true        ) && VarOverlayStatus=overlay_on   || VarOverlayStatus=overlay_off

   # Set rootfs status + overlay status
   echo "/usr/local/bin/fw_setenv ubifsargs setenv bootargs console=ttyPSC3,115200 ubi.mtd=0 root=ubi0:rootfs "$VarRootFileSystemStatus" rootfstype=ubifs quiet "$VarOverlayStatus"" > $serial_port
   sleep 0.1
   if [ "$VarUpdateUBootPasswd" == "set password" -a "$VarNewUBootPassword" != "none" ]; then
      echo "/usr/local/bin/fw_setenv password $VarNewUBootPassword" > $serial_port
      sleep 0.1
   fi
   echo "sync" > $serial_port
   wait $!

   # wait for TC3G promt
   if (! waitForPromt "#"); then
      echo -e "\nError: Timeout!"
      echo -e "Unable to find $VarTargetName promt.\n"
      return 1
   fi

   echo -e "UBoot variables were cleared.\n"

   # activate cloud service if configured
   if [ "$VarMachinesCloudService" == "true" ]; then
      if [ "$VarOverlayStatus" == "overlay_on" ]; then
         echo "Start Overlay File System"

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

         # Check overlay directory structure
         echo "mkdir -p /mnt/dataflash/stw/overlay/etc/" > $serial_port
         sleep 0.1
         echo "mkdir -p /mnt/dataflash/stw/overlay/usr/" > $serial_port
         sleep 0.1
         echo "mkdir -p /mnt/dataflash/stw/overlay/opt/" > $serial_port
         sleep 0.1
         echo "mkdir -p /mnt/dataflash/stw/overlay/lib/" > $serial_port
         sleep 0.1
         echo "mkdir -p /mnt/dataflash/stw/overlay/home/" > $serial_port
         sleep 0.1
         echo "mkdir -p /mnt/dataflash/stw/overlay/root/" > $serial_port
         sleep 0.1
         # Call unionfs mount
         echo "mount -a -t unionfs" > $serial_port
         sleep 0.1
         echo "sync" > $serial_port
         sleep 0.1

         # wait for TC3G promt
         if (! waitForPromt "#"); then
            echo -e "\nError: Timeout!"
            echo -e "Unable to find $VarTargetName promt.\n"
            return 1
         fi
         echo -e "Overlay is started\n"
      fi

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

      echo "mv /etc/init.d/_ynetworkd.config /etc/init.d/ynetworkd.config" > $serial_port
      sleep 0.1
      echo "mv /etc/init.d/_ycumulocityd.config /etc/init.d/ycumulocityd.config" > $serial_port
      sleep 0.1

      # wait for TC3G promt
      if (! waitForPromt "#"); then
         echo -e "\nError: Timeout!"
         echo -e "Unable to find $VarTargetName promt.\n"
         return 1
      fi

      echo "machines.cloud is acivated"
   fi

   # restart TC3G
   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 $VarTargetName.\n"
         return 1
      elif ( waitForPromt "stop:" ); then
         break
      fi
   done

   # clear config files, fit image and $VarTmpFile
   if [ -f "$script_dir/$VarTmpFile" ]; then
      OUTPUT=`rm "$script_dir/$VarTmpFile"`
      wait $!
   fi
   if [ -f "script_dir/$VarFitConfigFile" ]; then
      OUTPUT=`rm "script_dir/$VarFitConfigFile"`
      wait $!
   fi
   if [ -f "$VarDataFolderPath/$VarUBootEnvFile" ]; then
      OUTPUT=`rm "$VarDataFolderPath/$VarUBootEnvFile"`
      wait $!
   fi
   if [ -d "$VarTftpFolder" ]; then
      OUTPUT=`echo -e "$PASSW\n" | sudo -S rm -R $VarTftpFolder`
      wait $!
   fi
   if [ -f "$script_dir/$VarTftpConfigFile" ]; then
      OUTPUT=`rm "$script_dir/$VarTftpConfigFile"`
      wait $!
   fi
   if [ -r "$serial_port" ]; then
      `echo -e "$PASSW\n" | sudo -S chmod -R 660 $serial_port`
   fi

   echo -e "Temporarily files were removed.\n"

return 0
}
