#!/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
      if [ "$VarPrepareNandFlash" == true -o "$VarMachinesCloudService" == true ]; then
         if [ "$VarPrepareNandFlash" == true ]; then
            echo -e "Prepare NAND flash"
         fi
         if [ "$VarMachinesCloudService" == true ]; then
            echo -e "Activate machines.cloud service"
         fi
            VarPrepareNandFlashOnly=true
         return 0
      else
         echo -e "No component for update selected."
         echo -e "\033[33mPlease select components in config.ttl and restart update procedure.\033[37m"
      fi
      return 1
   fi

   # create tftpboot folder if necessary
   if [ ! -d "$VarTftpFolder" ];
   then
      OUTPUT=`echo -e "$PASSW\n" | sudo -S mkdir $VarTftpFolder`
      # wait for process to end
      wait $!
   fi

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

   # Make sure uboot env variable for overlay is off
   sed -i 's/overlay_on/overlay_off/g' "$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 $VarTargetName:" >> $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 was not created!"
      return 1
   fi

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

   # Create FIT image
   echo -e "Creating FIT image..."
   filename="$VarTftpFolder/$VarImageName"
   #OUTPUT=`echo -e "$PASSW\n" | sudo -S $script_dir/make_FIT/make_udimg`  # => hide output messages
   echo -e "$PASSW\n" | sudo -S $script_dir/make_FIT/make_udimg
   # wait for process to end
   wait $!

   OUTPUT=`echo -e "$PASSW\n" | sudo -S 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
}


