#!/bin/sh
# bcheck.sh 1.31 96/06/17
#
# Copyright 06/17/96 Sun Microsystems, Inc. All Rights Reserved
#

# Print usage message and exit
usage()
{
	echo "usage: $self [-all | -access | -leaks | -memuse] [-o logfile] [-q] [-s script] command"
	exit 1
}

self=`basename $0`
run_dir=`dirname $0`
arch=`uname -p`

quiet=off

# Set default checking mode
access=off leaks=on
memuse=off

# Handle checking mode flags
while [ $# -gt 0 ]
do
	case $1 in
		-all)	access=on; leaks=on;  shift;;
		-access) access=on; leaks=off; shift;;
		-leaks)	leaks=on; access=off; shift;;
		-memuse) leaks=off; access=off; memuse=on; shift;;
		-o)	shift; 
			if [ $# -gt 0 ]
			then errlogfile=$1
			else usage
			fi
			shift;;
		-q)	quiet=on; shift;;
		-s)	shift; 
			if [ $# -gt 0 ]
			then script=$1
			else usage
			fi
			if [ ! \( -r "$script" -a -f "$script" \) ]
			then echo "${self}: Cannot read script file $script"
			     exit 1
			fi
			shift;;
		-*)	usage;;
		*)	break;;
	esac
done

# Check for insufficient args
[ $# = 0 ] && usage

# Set program name and arguments
program="$1"; shift

# Prevent a second round of shell substitution on the arguments:
# - Insert a backslash (\) in front of each shell meta-character
#   contained in the arguments (except newline).
# - Surround embedded newlines with single quotes (')

SCRIPT=/tmp/.sed.$$
while [ $# -gt 0 ]
do
	cat > $SCRIPT << \!
s/\([][;&()|^<>         "$*'\]\)/\\\1/g
s/^/'/
s/$/'/
1s/^'//
$s/'$//
!
	arg=`echo "$1" | sed -f $SCRIPT`
	args="$args $arg"
	shift
done
rm -f $SCRIPT

if [ "$arch" != "sparc" ]
then
	if [ "$access" = "on" -a "$leaks" = "off" -a "$memuse" = "off" ]
	then
		echo "${self}: Access checking is not supported on this architecture."
		exit 1
	fi
		
fi

case ${access}-${leaks} in
	on-on)		checks=-all;;
	on-off)		checks=-access;;
	off-on)		checks=-leaks;;
	off-off)	if [ "$memuse" = "on" ] 
		        then
			    checks=-memuse
			else
			    checks=""	# can't happen
			fi
			;;
esac

# Check for existence of program
if [ ! \( -x "$program" -a -f "$program" \) ]
then
	echo "${self}: '"$program"' does not exist, or is not executable."
	exit 1;
fi

# Find dbx
dbx=${run_dir}/dbx

if [ ! \( -x "$dbx" -a -f "$dbx" \) ]
then echo "${self}: Cannot find dbx in `dirname $dbx`"
     echo "Run ${self} from the standard installed location."
     exit 1;
fi

# Set error log file and initialize it
if [ "$errlogfile" = "" ]
then
	errlogfile=`basename $program`
	errlogfile=${errlogfile}.errs
fi

# Setup command file
cmdfile=/tmp/.bcheck.$$

trap "rm -f $cmdfile" 0 1 2 3 15 

cat > $cmdfile <<!
set -o errexit
dbxenv rtc_auto_continue on
dbxenv rtc_auto_suppress on
dbxenv rtc_error_log_file_name $errlogfile
dbxenv rtc_mel_at_exit verbose
check $checks
!

# Insert command to source script if -s option was given
[ "$script" != "" ] && echo ". $script" >> $cmdfile

cat >> $cmdfile << !
run $args
exit
!

# Initiate checking
if [ "$quiet" = "on" ]
then
	$dbx -s /dev/null -C -B -c ". $cmdfile" $program 
else
	$dbx -s /dev/null -C -c ". $cmdfile" $program
fi

#pragma	@(#)RELEASE VERSION Workshop 4.0 10/15/96 [dbx 4.0]
