#!/bin/sh
# bcheck.ver.sh 1.5 00/11/23
#
# Copyright 11/23/00 Sun Microsystems, Inc. All Rights Reserved
#

# This string is here for the 'version' command:
# It gets sed'ed in at build-time:
# @(#)RELEASE VERSION Sun Dbx Debugger 7.4 117844-05 2006/03/18

mktmpdir() {
        mytmpdir=/tmp/bcheck.$$
        trap '/usr/bin/rm -rf $mytmpdir' 0 1 2 3 15
        /usr/bin/mkdir -m 700 $mytmpdir || exit 1
}

# Print usage message and exit
usage()
{
	echo "usage: $self [-V] [-all | -access | -leaks | -memuse] "
	echo "       [-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
	        -V) echo $self : "Sun Dbx Debugger 7.4 117844-05 2006/03/18"; exit 0;;
		-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 (')

mktmpdir

SCRIPT=$mytmpdir/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=$mytmpdir/bcheck

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
# prepend PWD if it's not an absolute path.
# because the '.' command in the dbx script refers to
# the directory that the first script is in, not the
# PWD (as is commonly mistaken)

if [ "$script" != "" ]
then
   firstchar=`echo $script | cut -c1`
   if [ "$firstchar" != "/" ]
   then
     script=`/bin/pwd`/$script
   fi
   echo ". $script" >> $cmdfile
fi

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

