# from example on page 279
# A shell line profiler	
TMPFILE=${TMP-/tmp}/shprof$$
trap 'rm -f $TMPFILE' EXIT
function err_exit
{
	print -u2 "$@"
	exit 1
}
#create equivalent script with profiling enabled
cat > $TMPFILE <<- \EOF
	integer _line
	if	( _line[4095]=0 ) 2> /dev/null
	then	_MAXARRAY=4095
	else	_MAXARRAY=1023
	fi
	readonly _MAXARRAY
	function _profend
	{
		integer i=1
		typeset file
		typeset -R7 count
		file=$(whence "$1")
		if	[[ $file = '' ]]
		then	file=$1
		fi
		print "*** line profile for $1 ***"
		while	read -r -u4 && (( i < _MAXARRAY ))
		do	if	((count=_line[i])) 
			then	print  "$count $i: $REPLY"
			fi
			i=i+1
		done 4< $file
	}
	_command=$1
	shift
	integer i=1
	while (( i < _MAXARRAY ))
	do	_line[i]=0 i=i+1 
	done
	unset i
	trap "_profend $_command" EXIT
	_line[1]=-1
	LINENO=0
	trap "((_line[LINENO]+=1))" DEBUG
EOF
file=$(whence "$1")
cat  "${file-$1}" >> $TMPFILE || err_exit "$1 cannot open"
chmod +x $TMPFILE
if	( exec -a foo /bin/echo ) > /dev/null 2>&1
then	dollar0="-a $1" 	# set $0 if possible
fi
exec $dollar0 $TMPFILE "$@"
