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


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

VarTftpFolder="/tmp/tc3_tftpboot"
VarImageName="image.fit"
VarTftpConfigFile="tftp/tftp.config"
VarFitConfigFile="make_FIT/udimage.cfg"
VarUBootEnvFile="u-boot.env"
VarFitPassword="skywalker"
VarRootfsSize="0x3700000"
VarTimeoutUBootVersion=60
VarTimeoutConnectUsb=60
VarUBootVersionForTftp=2010
VarPrepareNandFlashOnly=false
VarNewUBootPassword="none"

VarRootFileSystemStatus=
VarOverlayStatus=
VarMachinesCloudService=
VarComponentUboot=
VarComponentUbootEnvVars=
VarComponentKernel=
VarComponentFdt=
VarComponentRootfs=
VarTftpDownload=
VarPrepareNandFlash=
VarServerIP=
VarClientIP=
VarNetMask=
VarGatewayIP=
VarFileNameUboot=
VarUbootAddress=
VarFileNameKernel=
VarKernelAddress=
VarFileNameRootfs=
VarRootfsAddress=
VarFileNameFdt=
VarFdtAddress=
VarFileNameUbootEnv=
VarUpdateViaTftp=
VarUpdateViaUSB=
VarRootfsReadWrite=
VarRootfsReadOnly=
VarOverlayOff=
VarOverlayOn=
VarUBootPwdSet=
VarUBootPassword=
VarNewUBootPasswordConfirm=
VarUpdateUBootPasswd=
VarNoUpdatePassword=
VarUpdatePassword=
serial_port=

############################ 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
   if [ "$VarUpdateNow" == "true" ]; then
      echo -e "\033[31mError: Could not read config.ttl - file!\n\033[37m"
   else
      $(zenity --info \
        --title "$VarTargetName Updater" \
        --text '<span color="red"><b>Error: Could not read config.ttl - file!</b></span>')
   fi
   exit
fi

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

# create FIT image
CreateFitImage
ret_val=$?

if [ "$ret_val" != 0 ]; then
   if [ "$VarUpdateNow" == "true" ]; then
      echo -e "\033[31mError: Could not create FIT image!\n\033[37m"
   else
      $(zenity --info \
        --title "$VarTargetName Updater" \
        --text '<span color="red"><b>Error: Could not create FIT image!</b></span>')
   fi
   exit
fi

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

if [ "$VarTftpDownload" == true -a "$VarPrepareNandFlashOnly" == false ]; then
   # start TFTP server
   StartTftpServer
   ret_val=$?

   if [ "$ret_val" != 0 ]; then
      if [ "$VarUpdateNow" == "true" ]; then
         echo -e "\033[31mError: Could not start TFTP server!\n\033[37m"
      else
         $(zenity --info \
           --title "$VarTargetName Updater" \
           --text '<span color="red"><b>Error: Could not start TFTP server!</b></span>')
      fi
      exit
   fi
fi

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

if [ "$VarTftpDownload" == false -a "$VarPrepareNandFlashOnly" == false ]; then
   # connect flash drive to PC
   PrepareFlashDrive
   ret_val=$?

   if [ "$ret_val" != 0 ]; then
      if [ "$VarUpdateNow" == "true" ]; then
         echo -e "\033[31mError: Could not prepare flash drive!\n\033[37m"
      else
         $(zenity --info \
           --title "$VarTargetName Updater" \
           --text '<span color="red"><b>Error: Could not prepare flash drive!</b></span>')
      fi
      exit
   fi
fi

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

# Enter the UBoot
EnterUBoot "hard_reset"
ret_val=$?

if [ "$ret_val" != 0 ]; then
   if [ "$VarUpdateNow" == "true" ]; then
      echo -e "\033[31mError: Could not enter the UBoot!\n\033[37m"
   else
      $(zenity --info \
        --title "$VarTargetName Updater" \
        --text '<span color="red"><b>Error: Could not enter the UBoot!</b></span>')
   fi
   exit
fi

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

# Set UBoot environment variables
SetUBootEnvVar "$VarTftpDownload"
ret_val=$?

if [ "$ret_val" != 0 ]; then
   if [ "$VarUpdateNow" == "true" ]; then
      echo -e "\033[31mError: Could not set the UBoot environment variables!\n\033[37m"
   else
      $(zenity --info \
        --title "$VarTargetName Updater" \
        --text '<span color="red"><b>Error: Could not set the UBoot environment variables!</b></span>')
   fi
   exit
fi

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

# Check if update was successful
CheckUpdateSuccess
ret_val=$?

if [ "$ret_val" != 0 ]; then
   if [ "$VarUpdateNow" == "true" ]; then
      echo -e "\033[31mError: Update failed!\n\033[37m"
   else
      $(zenity --info \
        --title "$VarTargetName Updater" \
        --text '<span color="red"><b>Error: Update failed!</b></span>')
   fi
   exit
fi

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

if [ "$VarPrepareNandFlash" == true ]; then
   # Check if preparing was successful
   PrepareNandFlash
   ret_val=$?

   if [ "$ret_val" != 0 ]; then
      if [ "$VarUpdateNow" == "true" ]; then
         echo -e "\033[31mError: Preparing NAND Flash failed!\n\033[37m"
      else
         $(zenity --info \
           --title "$VarTargetName Updater" \
           --text '<span color="red"><b>Error: Preparing NAND Flash failed!</b></span>')
      fi
      exit
   fi
fi

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

# Clear UBoot environment variables
ClearUBootEnvVar
ret_val=$?

if [ "$ret_val" != 0 ]; then
   if [ "$VarUpdateNow" == "true" ]; then
      echo -e "\033[31mError: Could not clear the UBoot environment variables!\n\033[37m"
   else
      $(zenity --info \
         --title "$VarTargetName Updater" \
         --text '<span color="red"><b>Error: Could not clear the UBoot environment variables!</b></span>')
   fi
   exit
fi


################################# print success ##################################

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

if [ "$VarUpdateNow" != "true" ]; then
   $(zenity --info \
     --title "$VarTargetName Updater" \
     --text "Update of the $VarTargetName finished successfully.")
fi

###################################### EOF ########################################
