#!/bin/sh
###########################################################################
# @(#)docommand	1.2 11/05/28 Written 2011 by J. Schilling
###########################################################################
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# See the file CDDL.Schily.txt in this distribution for details.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file CDDL.Schily.txt from this distribution.
###########################################################################
#
# docommand()	the base test function
#
# Call: docommand [options] parameters
#
# Options:
# -silent	do not print command label nor "passed"
#
# Parameters:
# $1 a command label to be printed
# $2 a command to be executed
# $3 the expected exit code			or IGNORE
# $4 the expected stdout			or IGNORE
# $5 the expected stderr			or IGNORE
#
# Example:
# docommand "echo test" 0 "test\n" ""
#
###########################################################################
docommand() {
	remove exp.stdout exp.stderr got.stdout got.stderr cmd.last
	silent=false
	cmd_label="$1"
	case "$1" in
	-silent | --silent)
		silent=true
		shift;
		cmd_label="$1"
		;;
	esac

	$silent || echo_nonl "$cmd_label"...

	echo "$2" >  cmd.last			# Remember last command
	echo "$2" >> cmd.log			# Add last command to history
	eval "$2" > got.stdout 2> got.stderr	# run current command
	cmd_exit=$?				# Save exit code

	if test "$3" != "IGNORE"		# Verify exit code?
	then
		if test "$3" -eq "$cmd_exit"
		then
			:
		else
			if test -s got.stderr
			then
				stderr=`cat got.stderr`
				msg="Error message: $stderr"
			else
				msg="No error message was printed on stderr"
			fi
			fail "$cmd_label: \"$2\" Expected exit code $3, got $cmd_exit
$msg"
		fi
	fi
	if test "$4" != "IGNORE"		# Verify stdout?
	then
		echo_nonl "$4" > exp.stdout	# Save stdout sample to file
		echo		>> exp.stdout	# Make sample end in newline
		echo		>> got.stdout	# Make output end in newline
		diff exp.stdout got.stdout || fail "$cmd_label: stdout format error with $2"
	fi
	if test "$5" != "IGNORE"		# Verify stderr?
	then
		echo_nonl "$5" > exp.stderr	# Save stderr sample to file
		echo		>> exp.stderr	# Make sample end in newline
		echo		>> got.stderr	# Make output end in newline
		diff exp.stderr got.stderr || fail "$cmd_label: stderr format error with $2"
	fi
	
	remove exp.stdout exp.stderr got.stdout got.stderr cmd.last
	$silent || echo "passed"
	true
}

###########################################################################
#
# do_output()	alternate test function
#
# Call: do_output [options] parameters
#
# Options:
# -silent	do not print command label nor "passed"
#
# Parameters:
# $1 a command label to be printed
# $2 a command to be executed
# $3 the expected exit code			or IGNORE
# $4 the expected stdout contained in a file	or IGNORE
# $5 the expected stderr			or IGNORE
#
# Example:
# echo test > out_file
# do_output "echo test" 0 out_file ""
#
###########################################################################
do_output() {
	remove exp.stdout exp.stderr got.stdout got.stderr cmd.last
	silent=false
	cmd_label="$1"
	case "$1" in
	-silent | --silent)
		silent=true
		shift;
		cmd_label="$1"
		;;
	esac

	$silent || echo_nonl "$cmd_label"...

	echo "$2" >  cmd.last			# Remember last command
	echo "$2" >> cmd.log			# Add last command to history
	eval "$2" > got.stdout 2> got.stderr	# run current command
	cmd_exit=$?				# Save exit code

	if test "$3" != "IGNORE"		# Verify exit code?
	then
		if test "$3" -eq "$cmd_exit"
		then
			:
		else
			if test -s got.stderr
			then
				stderr=`cat got.stderr`
				msg="Error message: $stderr"
			else
				msg="No error message was printed on stderr"
			fi
			fail "$cmd_label: \"$2\" Expected exit code $3, got $cmd_exit
$msg"
		fi
	fi
	if test "$4" != "IGNORE"		# Verify stdout?
	then
		diff "$4" got.stdout || fail "$cmd_label: stdout format error with $2"
	fi
	if test "$5" != "IGNORE"		# Verify stderr?
	then
		echo_nonl "$5" > exp.stderr	# Save stderr sample to file
		echo		>> exp.stderr	# Make sample end in newline
		echo		>> got.stderr	# Make output end in newline
		diff exp.stdout got.stdout || fail "$cmd_label: stderr format error with $2"
	fi
	
	remove exp.stdout exp.stderr got.stdout got.stderr cmd.last
	$silent || echo "passed"
	true
}
