#!/bin/bash

# Copyright (c) 2015 Wind River Systems, Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1) Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2) 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.
#
# 3) Neither the name of Wind River Systems 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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.
#

# Wrapper script to perform all operating system sanity tests

# Import common sanity check definitions
#
if [ -z ${ZEPHYR_BASE} ]; then
	echo "shell variables required to build Zephyr Kernel are not set"
	exit 1
fi
if [ ! -d ${ZEPHYR_BASE} ] ; then
	echo "directory ${ZEPHYR_BASE} not found"
	exit 1
fi
source ${ZEPHYR_BASE}/scripts/sanity_chk/common.defs

# Symbols used in this script only
#
SCRIPT_PATH=${ZEPHYR_BASE}/scripts/sanity_chk

# print script usage
#
help() {
cat << EOF

Usage : ${SCRIPT_NAME} [-hHcqbl] [-A <arch>] [-P <platform>]
       -h display this help message
       -H display more detailed documentation
       -c just clean projects and delete execution logs
       -q just do quick sanity check
       -b just build projects
       -l keep successful project execution logs
       -A build projects for the specified architecture
       -P build projects for the specified platform

EOF
}

# print long help
#
long_help() {
cat << EOF

Script to sanitize Zephyr Kernel.

The full sanity check consists of the following phases:

    - Run "footprint_chk" to characterize kernel footprint.
    - Run "regression_chk" to regression test kernel operation.
    - Run "out-of-tree_chk" to verify if projects can reside outside the normal tree

The full sanity check is run by default. If desired, some steps can be omitted
by using the available command options.

*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=*-=

Before running the script, ensure Zephyr Kernel environment variables are set.

The script returns 0 on success. If an error is encountered at any point
the script terminates with an error message and returns the exit value
of the command that failed.

EOF
}

# main routine
#
main() {
	# set up default behaviors
	clean_only=0

	args_passed_on=""

	# process command options
	while getopts ":hHcqblA:B:P:" arg $*
	do
		case $arg in
		h)
			help
			exit 0
			;;
		H)
			long_help
			exit 0
			;;
		c)
			clean_only=1
			;;
		q)
			args_passed_on="${args_passed_on} -q"
			;;
		b)
			args_passed_on="${args_passed_on} -b"
			;;
		l)
			args_passed_on="${args_passed_on} -l"
			;;
		A)
			args_passed_on="${args_passed_on} -A $OPTARG"
			;;
		B)
			# Unlisted option.  Exists only until migration to
			# -P <platform> is complete
			args_passed_on="${args_passed_on} -B $OPTARG"
			;;
		P)
			args_passed_on="${args_passed_on} -P $OPTARG"
			;;
		\?)
			${ECHO} "invalid option -$OPTARG"
			help
			exit 1
			;;
		:)
			${ECHO} "missing argument for option -$OPTARG"
			help
			exit 1
			;;
		esac
	done

	# ensure no unexpected arguments have been provided
	shift $((OPTIND-1))
	if [ x$1 != x ] ; then
		${ECHO} "unexpected argument $1"
		exit 1
	fi

	# handle case where we're just cleaning up
	if [ ${clean_only} = 1 ] ; then
		${SCRIPT_PATH}/footprint_chk -c
		[ $? -eq 0 ] || exit $?
		${SCRIPT_PATH}/regression_chk -c
		[ $? -eq 0 ] || exit $?
		${SCRIPT_PATH}/out-of-tree_chk -c
		exit $?
	fi

	# run the footprint characterization check
	print_header
	${ECHO} "Running Footprint Characterization Check"
	${ECHO}
	${SCRIPT_PATH}/footprint_chk ${args_passed_on}
	[ $? -eq 0 ] || exit $?

	# run the standard regression check
	print_header
	${ECHO} "Running Standard Regression Check"
	${ECHO}
	${SCRIPT_PATH}/regression_chk ${args_passed_on}
	[ $? -eq 0 ] || exit $?

	# run the out of tree regression check
	print_header
	${ECHO} "Running Out of tree Regression Check"
	${ECHO}
	${SCRIPT_PATH}/out-of-tree_chk ${args_passed_on}
	[ $? -eq 0 ] || exit $?

	${ECHO}
	print_header
	${ECHO} "${SCRIPT_NAME} completed successfully"
}

# invoke main routine passing all parameters passed to the script
#
main $*

# exit with success
exit 0
