#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   ReadConfigFile()
#
#   This function reads the user parameter out of the config.ttl file.
#
#   \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     29.08.2013  STW/A.Appelt
#
#-----------------------------------------------------------------------------

function ReadConfigFile() 
{
   # Read User Settings from Config File   
   
   filename="$script_dir/config.ttl"
   activated=" = 1"
   ip_structure="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" 

   # Check if config.ttl file exists
   if [ ! -s "$filename" ]; 
   then
      echo -e "config.ttl file does not exist!"
      return 1
   fi

   # read in: VarRootFileSystemStatus
   if (! ReadVariable "VarRootFileSystemStatus" $filename );
   then
      VarRootFileSystemStatus=false
   else
      VarRootFileSystemStatus=true
   fi

   # read in: VarComponentUboot
   if (! ReadVariable "VarComponentUboot" $filename );
   then
      VarComponentUboot=false
   else
      VarComponentUboot=true
   fi

   # read in: VarComponentUbootEnvVars
   if (! ReadVariable "VarComponentUbootEnvVars " $filename );
   then
      VarComponentUbootEnvVars=false
   else
      VarComponentUbootEnvVars=true
   fi

   # read in: VarComponentKernel
   if (! ReadVariable "VarComponentKernel" $filename );
   then
      VarComponentKernel=false
   else
      VarComponentKernel=true
   fi

   # read in: VarComponentFdt
   if (! ReadVariable "VarComponentFdt" $filename );
   then
      VarComponentFdt=false
   else
      VarComponentFdt=true
   fi

   # read in: VarComponentRootfs
   if (! ReadVariable "VarComponentRootfs" $filename );
   then
      VarComponentRootfs=false
   else
      VarComponentRootfs=true
   fi

   # read in: VarPrepareNandFlash
   if (! ReadVariable "VarPrepareNandFlash" $filename );
   then
      VarPrepareNandFlash=false
   else
      VarPrepareNandFlash=true
   fi

   # read in: VarTftpDownload
   if (! ReadVariable "VarTftpDownload" $filename );
   then
      VarTftpDownload=false
   else
      VarTftpDownload=true
   fi

   if ( "$VarTftpDownload" ); 
   then

      # read in: VarServerIP
      ReadIPAddress "VarServerIP" $filename

      if [ "$ret_ip" != false ];
      then
         VarServerIP=$ret_ip
      else
         echo -e "Could not read server IP address from config file."
         return 1
      fi   

      # read in: VarClientIP
      ReadIPAddress "VarClientIP" $filename

      if [ "$ret_ip" != false ];
      then
         VarClientIP=$ret_ip
      else
         echo -e "Could not read client IP address from config file."
         return 1
      fi   

      # read in: VarNetMask
      ReadIPAddress "VarNetMask" $filename

      if [ "$ret_ip" != false ];
      then
         VarNetMask=$ret_ip
      else
         echo -e "Could not read net mask from config file."
         return 1
      fi   

      # read in: VarGatewayIP
      ReadSystemVariable "VarGatewayIP" $filename

      if [ "$ret_var" != false ];
      then
         VarGatewayIP=$ret_var
      else
         echo -e "Could not read gateway IP address from config file."
         return 1
      fi   
   fi

   echo -e "\n*********************** User Configurations for Linux Updater ***********************"
   echo -e "\nGeneral Settings:"
   echo -e "Serial Port\t = $serial_port"
   echo -e "Log-File\t = $script_dir/$log_file"
   echo -e "\nComponents to be installed:"
   echo -e "VarRootFileSystemStatus = $VarRootFileSystemStatus\t; TRUE  -> root file system is read write" 
   echo -e "\t\t\t\t; FALSE -> root file system will be set to read only"
   echo -e "VarComponentUboot\t= $VarComponentUboot\t; install uboot"
   echo -e "VarComponentKernel\t= $VarComponentKernel\t; install kernel"
   echo -e "VarComponentFdt\t\t= $VarComponentFdt\t; install flattened device tree"
   echo -e "VarComponentRootfs\t= $VarComponentRootfs\t; install root file system"
   echo -e "VarPrepareNandFlash\t= $VarPrepareNandFlash\t; prepare (format) the NAND data flash"
   echo -e "VarComponentUbootEnvVars= $VarComponentUbootEnvVars\t; update uboot enviroment variables"
   echo -e "\nUpdate mode:"   
   echo -e "VarTftpDownload\t = $VarTftpDownload \t; TRUE  -> enables update via TFTP Download" 
   echo -e "\t\t\t\t; FALSE -> enables update via TFTP Download" 
   if ( "$VarTftpDownload" );
   then
      echo -e "\nNetwork settings for TFTP data download:"   
      echo -e "VarServerIP \t = $VarServerIP \t; TFTP server ip (ip of host PC)"
      echo -e "VarClientIP \t = $VarClientIP \t; ip of TC3"
      echo -e "VarNetMask \t = $VarNetMask \t; network mask"
      echo -e "VarGatewayIP \t = $VarGatewayIP \t; gateway ip \n"
   fi
   echo -e "*************************************************************************************\n"


   # Read System Setting from Config File

   # read in: VarTftpDownload
   ReadSystemVariable "VarFileNameRootfs" $filename

   if [ "$ret_var" != false ];
   then
      VarFileNameRootfs=$ret_var
   else
      echo -e "Could not read system variable \"VarFileNameRootfs\" from config file."
      return 1
   fi   

   # read in: VarFileNameKernel
   ReadSystemVariable "VarFileNameKernel" $filename   

   if [ "$ret_var" != false ];
   then
      VarFileNameKernel=$ret_var
   else
      echo -e "Could not read system variable \"VarFileNameKernel\" from config file."
      return 1
   fi   

   # read in: VarFileNameFdt
   ReadSystemVariable "VarFileNameFdt" $filename   

   if [ "$ret_var" != false ];
   then
      VarFileNameFdt=$ret_var
   else
      echo -e "Could not read system variable \"VarFileNameFdt\" from config file."
      return 1
   fi  

   # read in: VarFileNameUboot
   ReadSystemVariable "VarFileNameUboot" $filename   

   if [ "$ret_var" != false ];
   then
      VarFileNameUboot=$ret_var
   else
      echo -e "Could not read system variable \"VarFileNameUboot\" from config file."
      return 1
   fi  

   # read in: VarFileNameUbootEnv
   ReadSystemVariable "VarFileNameUbootEnv" $filename   

   if [ "$ret_var" != false ];
   then
      VarFileNameUbootEnv=$ret_var
   else
      echo -e "Could not read system variable \"VarFileNameUbootEnv\" from config file."
      return 1
   fi  

   # read in: VarUbootAddress
   ReadSystemVariable "VarFlashAddrUbootStart2012_10" $filename   

   if [ "$ret_var" != false ];
   then
      VarUbootAddress="0x$ret_var"
   else
      echo -e "Could not read system variable \"VarFlashAddrUbootStart2012_10\" from config file."
      return 1
   fi  

   # read in: VarKernelAddress
   ReadSystemVariable "VarFlashAddrKernelStart" $filename   

   if [ "$ret_var" != false ];
   then
      VarKernelAddress="0x$ret_var"
   else
      echo -e "Could not read system variable \"VarFlashAddrKernelStart\" from config file."
      return 1
   fi  

   # read in: VarRootfsAddress
   ReadSystemVariable "VarFlashAddrRootfsStart" $filename   

   if [ "$ret_var" != false ];
   then
      VarRootfsAddress="0x$ret_var"
   else
      echo -e "Could not read system variable \"VarFlashAddrRootfsStart\" from config file."
      return 1
   fi  

   # read in: VarFdtAddress
   ReadSystemVariable "VarFlashAddrFdtStart2012_10" $filename   

   if [ "$ret_var" != false ];
   then
      VarFdtAddress="0x$ret_var"
   else
      echo -e "Could not read system variable \"VarFlashAddrFdtStart2012_10\" from config file."
      return 1
   fi  

return 0 
}

# Read Value of Variable
function ReadVariable() 
{
   while read line; 
   do
      if test "${line#*"$1"}" != "$line"
      then
         param="$1"
         length=${#param}
         character=$(expr substr "$line" 1 "$length")

         if [ "$character" == "$1" ];
         then

            # find index of '='
            index=$(grep -o -b "=" <<< "$line" | cut -c1-2 )

            if [ ! "$index" ]; then
               return 1;
            fi
            ((index++))

         while (true); 
         do
            ((index++))
            # get character
            character=$(expr substr "$line" "$index" 1)
        
            # compare character
            if [ "$character" == " " ];
            then
               : # do nothing
            elif [ "$character" == "1" ];
            then
               # found '1'
               return 0
            else
               # found not '1'
               return 1
            fi
         done
         fi
      fi
   done < "$2"
}

# Read Value of Variable
function ReadSystemVariable() 
{
   ret_var=false;
   while read line; 
   do
      if test "${line#*"$1"}" != "$line"
      then
         param="$1"
         length=${#param}
         character=$(expr substr "$line" 1 "$length")

         if [ "$character" == "$1" ];
         then

            # extract substring between "'" and "'"
            #ret_var=`echo "$line"  | sed -n -e "/=/ s|.*'\(.*\)'.*|\1|p"` 
            ret_var=`echo "$line" | awk -F"'" '{print $2}' | awk -F"'" '{print $1}'`
            return 0
         fi
      fi
   done < "$2"
}

# Read IP Address
function ReadIPAddress() 
{
   ret_ip=false;
   while read line; 
   do
      if test "${line#*"$1"}" != "$line"
      then
         param="$1"
         length=${#param}
         character=$(expr substr "$line" 1 "$length")

         if [ "$character" == "$1" ];
         then
            if grep -o -q "$ip_structure" <<< "$line" 
            then 
               ret_ip=$(grep -o "$ip_structure" <<< "$line")
               return 0
            fi
         fi
      fi
   done < "$2"
}






















