#!/bin/sh

get_stack () # proc thread
{
	if [ -z "$1" ]
	then
		return
	fi

	thread='main'
	if [ -n "$2" ]
	then 
		thread="$2"
	fi

	TMP=`mktemp /tmp/tmp.XXXXXX`
	TMP2=`mktemp /tmp/tmp.XXXXXX`

	proc=`jps|grep $1|awk '{print $1}'`
	if [ -z "$proc" ]
	then
		echo "$1: No such process."
		rm -f $TMP $TMP2
		return
	fi

	# -F
	jstack $proc >$TMP

	st=`grep -n "^\"$thread\"" $TMP|cut -d: -f-1`
	ed=`cat $TMP|wc -l`

	if [ -z "$st" ]
	then
		echo "$1 - $2: No such thread."
		rm -f $TMP $TMP2
		return
	fi

	sed -n "$st,$ed p" $TMP >$TMP2
	ed=`grep -n '^$' $TMP2|head -n 1|cut -d: -f-1`
	sed -n "1,$ed p" $TMP2

	rm -f $TMP $TMP2
}

#########################################################
get_stack Client
#get_stack Server
get_stack Add
