#!/bin/bash

#-----------------------------------------------------------------------------
#
# File: uni_sign
#
# Description:
#	This script takes input file as argument and is used to sign the
#	ISBC/ESBC Images specified in the input file for Platform specified.
#	For doing this the required CST Tools are invoked with the
#	input file as argument
#
# Copyright (c) 2016 Freescale Semiconductor, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of Freescale Semiconductor nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
#
# THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#-----------------------------------------------------------------------------

# Help Function
print_help() {
	./create_hdr_isbc --help
	echo "-----------------------------------------------------"
	echo "- uni_sign is a wrapper script over the TOOL"
	echo "- Correct Usage (OPtions as specified above):"
	echo "-"
	echo "- $0 [options] <input_file>"
	echo "-"
	echo "-----------------------------------------------------"
	exit
}

############################################################
# Script Starts Here
############################################################
echo ""

# Check if proper arguments are specified

if [ $# -eq 0 ]; then
	# Invalid Arguments
	echo "Error !! Invalid Usage"
	print_help

elif [ $1 == "--help" ]; then
	# Help Message to be printed
	print_help

else 
	input_file=${@:$#}

	# Check if Input File exists
	if [ -s $input_file ]; then
		# Check if image is ESBC or ISBC
		grep "^ESBC=1$" $input_file &> /dev/null

	else
		echo "Input File $input_file does not exist"
		print_help
	fi

	#########################################################
	# IF ESBC = 1, call create_hdr_esbc
	# Else Call create_hdr_isbc
	#########################################################

	if [ $? -eq 0 ]; then
		./create_hdr_esbc $@
	else
		./create_hdr_isbc $@
	fi

fi
