#!/bin/bash
#--------------------------------------------------------------------------------
#
#   \internal    Update_linux
#   \file        Update_linux
#   \brief       Main Script of the TC3 Updater for Linux
#
#   detailed description
#
#   \implementation
#   project     tc3_updater_for_linux
#   copyright   STW (c) 1999-200x
#   license     use only under terms of contract / confidential
#
#   created     28.08.2013  STW/A.Appelt
#   \endimplementation
#
#--------------------------------------------------------------------------------

# TODO: Timeout for update procedure (CheckUpdateSuccess) 
# TODO: (TBC) Timout for connecting usb to tc3???
# TODO: Set DEFAULT Env Variables (SetUBootEnvVar)
# TODO: get rid of terminal message: 
# /esx-tc3/updater/scripts/PrepareNandFlash: line 22: 10674 Terminated
#              cat $serial_port > "$script_dir/$VarTmpFile"
#
# TODO: Error: - Setting Env Variabees in UBoot failed (occasionally)
#              - Entering UBoot (the second time) after soft reset failed (occasionally)
#              - Preparing NAND flash failed (occasionally)
# Reason for errors might be the old BSP. 

############################ define global variables ##############################

VarTmpFile="temp.txt"
VarTftpFolder="/tc3_tftpboot"
VarImageName="image.fit"
VarTftpConfigFile="tftp/tftp.config"
VarFitConfigFile="make_FIT/udimage.cfg"
VarDataFolderPath="$script_dir/data"
VarFitPassword="skywalker"
VarRootfsSize="0x3700000"
VarTimeoutUBootVersion=60
VarTimeoutConnectUsb=60
VarUBootVersionForTftp=2010

VarRootFileSystemStatus=
VarComponentUboot=
VarComponentUbootEnvVars=
VarComponentKernel=
VarComponentFdt=
VarComponentRootfs=
VarTftpDownload=
VarPrepareNandFlash=
VarServerIP=
VarClientIP=
VarNetMask=
varGatewayIP=
VarFileNameUboot=
VarUbootAddress=
VarFileNameKernel=
VarKernelAddress=
VarFileNameRootfs=
VarRootfsAddress=
VarFileNameFdt=
VarFdtAddress=
VarFileNameUbootEnv=

############################ includes #############################################

. $script_dir/scripts/ReadConfigFile
. $script_dir/scripts/CreateFitImage
. $script_dir/scripts/StartTftpServer
. $script_dir/scripts/EnterUBoot
. $script_dir/scripts/SetUBootEnvVar
. $script_dir/scripts/CheckUpdateSuccess
. $script_dir/scripts/ClearUBootEnvVar
. $script_dir/scripts/PrepareNandFlash
. $script_dir/scripts/PrepareFlashDrive

############################ Read Config File config.ttl ##########################

# Read Config File
ReadConfigFile
ret_val=$?

if [ "$ret_val" != 0 ];
then
   echo -e "\033[31mError: Could not read config.ttl - file!\n\033[37m"
   exit
fi

############################ create FIT Image #####################################

# create FIT image
if (! CreateFitImage )
then
   echo -e "\033[31mError: Could not create FIT image!\n\033[37m"
   exit
fi

############################ create tftp.config and start TFTP Server #############

if [ "$VarTftpDownload" == true ];
then
   # start TFTP server
   if (! StartTftpServer)
   then
      echo -e "\033[31mError: Could not start TFTP server!\n\033[37m"
      exit
   fi
fi

############################ Connect Flash Drive to PC ############################

if [ "$VarTftpDownload" == false ];
then
   # connect flash drive to PC
   if (! PrepareFlashDrive "forPC")
   then
      echo -e "\033[31mError: Could not prepare flash drive!\n\033[37m"
      exit
   fi
fi

############################ enter UBoot ##########################################

# Enter the UBoot
if (! EnterUBoot "hard_reset") 
then
   echo -e "\033[31mError: Could not enter the UBoot!\n\033[37m"
   exit
fi

############################ set UBoot Env.Variables ##############################

# Set UBoot environment variables
if (! SetUBootEnvVar "$VarTftpDownload" "$VarRootFileSystemStatus" )
then
   echo -e "\033[31mError: Could not set the UBoot environment variables!\n\033[37m"
   exit
fi

############################ Connect Flash Drive to TC3 ###########################

if [ "$VarTftpDownload" == false ];
then
   # connect flash drive to TC3
   if (! PrepareFlashDrive "forTC3")
   then
      echo -e "\033[31mError: Could not prepare flash drive!\n\033[37m"
      exit
   fi
fi

############################ Check Update Success #################################

# Check if update was successful
if (! CheckUpdateSuccess)
then
   echo -e "\033[31mError: Update failed!\n\033[37m"
   exit
fi

############################ Prepare NAND Flash ###################################

if [ "$VarPrepareNandFlash" == true ];
   then
   # Check if preparing was successful
   if (! PrepareNandFlash)
   then
      echo -e "\033[31mError: Preparing NAND Flash failed!\n\033[37m"
      exit
   fi
fi

############################ clear UBoot Env.Variables ############################

# Clear UBoot environment variables
if (! ClearUBootEnvVar "$VarTftpDownload" )
then
   echo -e "\033[31mError: Could not clear the UBoot environment variables!\n\033[37m"
   exit
fi

echo -e "\n\033[40;32mUpdate of the TC3 finished successfully.\033[0m\n"

###################################################################################

