#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   PreparenNandFlash()
#
#   Function prepares NAND flash.
#
#   \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    02.09.2013  STW/A.Appelt
#
#-----------------------------------------------------------------------------

function PrepareNandFlash()
{
   filename="$script_dir/$VarTmpFile"

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

   # check if mounted
   echo "nand_ismounted.sh" > $serial_port

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

   # read in: $VarTmpFile
   tmp_input=$( grep -o "NAND flash mounted" "$filename" )

   if [ ! -z "$tmp_input" ]; then
      # start buffering RS232 traffic
      OUTPUT=`cat $serial_port > "$script_dir/$VarTmpFile" &`
      sleep 0.1
      # unmount nand flash
      echo "nand_umount.sh" > $serial_port
      # wait for TC3G promt
      if (! waitForPromt "#"); then
         echo -e "\nError: Timeout!"
         echo -e "Unable to find $VarTargetName promt.\n"
         return 1
      fi
      # read in: $VarTmpFile
      tmp_input=$( grep -o "error" "$script_dir/$VarTmpFile" )
      if [ ! -z "$tmp_input" ]; then
         echo -e "\nError while executing nand_umount script."
         return 1
      fi
   fi

   echo -e "NAND flash is not mounted\n"

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

   # prepare NAND flash
   echo "nand_prepare.sh" > $serial_port

   if (! waitForPromt "CAUTION:")
   then
      echo -e "\Could not prepare NAND flash."
      return 1
   fi

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

   echo -e "Preparing NAND flash... (could take up to 5 minutes)"

   # acknowledge
   echo "Y" > $serial_port

   # start spinning in the background
   SpinString &
   ProcessID=$!

   if (! waitForNandFlash "#")
   then
      echo -e "\nError: Could not find $VarTargetName prompt."
      return 1
   fi

   # stop spinning in the background
   kill $ProcessID
   wait $! 2>/dev/null  # suppress kill message
   sleep 1

   # clear screen
   echo "                                                                           "

   # print NAND flash summary
   cat "$script_dir/$VarTmpFile" |
   while read line; do
      count=$(echo $line | wc -c)
      if [ $count -le 200 ]; then
         echo "$line"
      fi
   done

   # read in: $VarTmpFile
   tmp_input=$( grep -o "error" "$script_dir/$VarTmpFile" )
   if [ ! -z "$tmp_input" ]; then
      echo -e "\nError while executing nand_prepare script."
      return 1
   fi

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

   echo "nand_mount.sh" > $serial_port
   sleep 0.1      # wait for TC1 promt
   echo "sync" > $serial_port

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

   echo -e "NAND flash is prepared.\n"

return 0
}


# function to search for a string
function waitForNandFlash()
{
   StartTime=$(date +%s)
   while true;
   do
      CurrTime=$(date +%s)
      TimeDiff=$(( $CurrTime - $StartTime ))
      line=$(tail -1 "$script_dir/$VarTmpFile")
      if [ $TimeDiff -ge 300 ]; then
         killall tail 2>/dev/null
         return 1
      elif test "${line#*"$1"}" != "$line"; then
         killall cat & > /dev/null &
         return 0
      fi
   done
   killall cat & > /dev/null &
return 1
}

# function to show that the updater is still working
function SpinString()
{
   spinstr="|/-\\"
   index=0

   while :; do
      i=${spinstr:$index:1}
      echo -e -n "$i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i $i \r"
      ((index++))
      if [ "$index" == "4" ]; then
         index=0
      fi
      sleep 0.2
   done
}


