#!/bin/bash
#kstrax execution helper script
#COPYRIGHT (C) HITACHI,LTD. 2006
#Created by S.Moriya<s-moriya@sdl.hitachi.co.jp>

function usage() {
    echo "Usage : trace <start/stop> <option(tracing time)>"
}

function start_trace() {
    kstrax-rec start
    if [ $# = "0" ]; then
	kstrax -b &
    else
	kstrax -b -T $1
    fi
}

function stop_trace() {
    local KSTRAX=`ps aux | grep "kstrax -b" | grep -v grep | cut -c 5-14`

    if [ $# = "0" ]; then
	kill -INT $KSTRAX
	while [ $KSTRAX ];
	do
	  sleep 1
	  KSTRAX=`ps aux | grep "kstrax -b" | grep -v grep | cut -c 5-14`
	done
    fi
    kstrax-rec stop
}

if [ $# = "0" ]; then
    usage
elif [ $# = "1" ]; then
    if [ $1 = "start" ]; then
	start_trace
    elif [ $1 = "stop" ]; then
	stop_trace
    else
	usage
    fi
else
    start_trace $2
    stop_trace $2
fi
