#!/bin/bash
# -----------------------------------------------------------------------------
#   file	make_udimg
#   brief       compile a FIT image for the STW uboot updater (for TC3/EB07)
#   usage	make_udimg
#
#   Generate a uboot FIT image source file (its file) and compile the FIT image.
#   The FIT image can contain several images (kernel, rootfs,...) to be updated 
#   by the STW uboot updater.
#   The configuration is read from file config.cfg.
#
#   return	0: no error
#         	1: error
#
#   created     17.01.2013  STW/G.Waibel (georg.waibel@sensor-technik.de)
# -----------------------------------------------------------------------------



its_file="tmp.its"
script_name=`basename $0`
script_dir=`dirname $0`
config_file=$script_dir/udimage.cfg

# pull in user configuration (for $result_file)
if [ ! -e $config_file ]; then
	printf "Error: Configuration file does not exist\n\n"
	exit 1
fi
source $config_file


# check if result file is specified in config file
if [ ! $result_file ] || [ $result_file == "" ]; then
	printf "Error: Result file not specified\n\n"
	exit 1
fi

# cleanup old result
rm -f $result_file

# generate its file
$script_dir/gen_its $config_file > $its_file
if [ $? != 0 ]; then
	printf "Error: Could not generate its file\n\n"
	exit 1
fi

# generate fit image
# overwrite serach path with local directory to make sure we use our own 
# mkimage and dtc tools (dtc is called by mkimage)
PATH_OLD=$PATH
PATH=$script_dir
mkimage -f $its_file $result_file
if [ $? != 0 ]; then
	printf "Error: Could not make fit image\n\n"
	exit 1
fi
PATH=$PATH_OLD

# cleanup temporary files
rm -f pswd.tmp
rm -f script.img
rm -f $its_file

exit 0
