#!/bin/bash
#-----------------------------------------------------------------------------
#
#   \brief   CreateFitImage()
#
#   This function creates a config file for the FIT image generator and after the
#   file is created successfully the FIT image.
#
#   \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 CreateFitImage() 
{
   filename="$script_dir/$VarFitConfigFile"
   filepath="$VarDataFolderPath"
   
   # check if component selected
   if [ "$VarComponentUboot" == false -a "$VarComponentKernel" == false \
       -a "$VarComponentRootfs" == false -a "$VarComponentFdt" == false \
       -a "$VarComponentUbootEnvVars" == false ];
   then
      echo -e "No component for update selected."
      echo -e "\033[33mPlease select components in config.ttl and restart update procedure.\033[37m"
      return 1
   fi

   # create tftpboot folder if necessary
   if [ ! -d "$VarTftpFolder" ];
   then
      OUTPUT=`mkdir $VarTftpFolder`
      # wait for process to end   
      wait $! 
   fi

   # Convert UBoot Variables into readable format
   sed 's/\r$//' "$filepath/$VarFileNameUbootEnv" > "$filepath/$VarUBootEnvFile"
   wait $!

   # Create Config File
   echo "# Result file name (FIT image name)" > $filename
   echo "result_file=\"$VarTftpFolder/$VarImageName\"" >> $filename
   echo "" >> $filename
   echo "# Update image header definitions" >> $filename
   echo "image_description=\"STW update image file\"" >> $filename
   echo "password=\"$VarFitPassword\"" >> $filename
   echo "" >> $filename

   if [ "$VarComponentUboot" == true ]; 
   then
      echo "# First image: data image" >> $filename
      echo "img1_name=\"uboot\"" >> $filename
      echo "img1_description=\"This is the uboot image\"" >> $filename
      echo "img1_file=\"$filepath/$VarFileNameUboot"\" >> $filename
      echo "img1_addr=\"$VarUbootAddress\"" >> $filename
      echo "" >> $filename
   fi
    
   if [ "$VarComponentKernel" == true ]; 
   then
      echo "# Second image: data image" >> $filename
      echo "img2_name=\"kernel\"" >> $filename
      echo "img2_description=\"This is the kernel image\"" >> $filename
      echo "img2_file=\"$filepath/$VarFileNameKernel\"" >> $filename
      echo "img2_addr=\"$VarKernelAddress\"" >> $filename
      echo "" >> $filename
   fi

   if [ "$VarComponentRootfs" == true ];  
   then
      echo "# Third image: data image" >> $filename
      echo "img3_name=\"rootfs\"" >> $filename
      echo "img3_description=\"This is the rootfs image\"" >> $filename
      echo "img3_file=\"$filepath/$VarFileNameRootfs\"" >> $filename
      echo "img3_addr=\"$VarRootfsAddress\"" >> $filename
      echo "# for tc3:" >> $filename
      echo "img3_size=\"$VarRootfsSize\"" >> $filename
      echo "" >> $filename
   fi

   if [ "$VarComponentFdt" == true ]; 
   then
      echo "# 4th image: data image" >> $filename
      echo "img4_name=\"dtb\"" >> $filename
      echo "img4_description=\"This is the device tree blob image\"" >> $filename
      echo "img4_file=\"$filepath/$VarFileNameFdt\"" >> $filename
      echo "img4_addr=\"$VarFdtAddress\"" >> $filename
      echo "" >> $filename
   fi

   if [ "$VarComponentUbootEnvVars" == true ]; 
   then
      echo "# 5th image: uboot script" >> $filename
      echo "img5_name=\"script\"" >> $filename
      echo "img5_description=\"This is a uboot script file\"" >> $filename
      echo "img5_file=\"$filepath/$VarUBootEnvFile\"" >> $filename
      echo "img5_type=\"script\"" >> $filename
   fi

   # wait for process to end   
   wait $! 
   
   # Check if udimage.cfg file exists
   if [ ! -s "$filename" ]; 
   then
      echo -e "Config file for FIT image does not exist!"
      return 1
   fi

   echo -e "Config file for FIT image created.\n"

   # Create FIT image
   echo -e "Creating FIT image..."
   filename="$VarTftpFolder/$VarImageName"
   OUTPUT=`$script_dir/make_FIT/make_udimg`
   
   # wait for process to end   
   wait $!

   OUTPUT=`chmod 777 -R $VarTftpFolder`

   # Check if image.fit file exists
   if [ ! -s "$filename" ]; 
   then
      echo -e "Creating FIT image failed!"
      return 1
   fi

   echo -e "\nFIT image created.\n"
 
return 0
}


