#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/cdat/types/trace/trace.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

phase_check()
{
    # Only LPAR and VIOS are supported for the trace collect
    [ "$CDAT_TYPE" = 'LPAR' -o "$CDAT_TYPE" = 'VIOS' ] || {
        mlog -1 "trace collect type is not supported on $CDAT_TYPE"
        exit 1	# IGNORE
    }
}

phase_execute()
{
    remote_cmd "LANG=C /usr/sbin/trace -a -o /tmp/cdat.trc $opts"
    status=$?
    [ $status -eq 0 ] || {
	mlog -1 "Cannot start trace (error=$status)"
	exit 128
    }
    # Sleep for the specified duration if trace option -x is not specified
    [ -z "$xopt" ] && sleep $dval
}

phase_terminate()
{
    # Stop the trace if trace option -x is not specified, otherwise the
    # trace will be stopped automatically at the end of the command.
    [ -z "$xopt" ] && {
	remote_cmd "LANG=C /usr/bin/trcstop"
	status=$?
	[ $status -eq 0 ] || {
	    mlog -1 "Cannot stop trace (error=$status)"
	    exit 128
	}
    }
}

phase_grab()
{
    get_file /tmp/cdat.trc "$CDAT_DEST_DIR/trcfile"
    status=$?
    [ $status -eq 0 ] || {
	mlog -1 "Cannot retrieve trace file (error=$status)"
	exit 128
    }
    get_file /etc/trcfmt "$CDAT_DEST_DIR/"
    # NB: we do not fail if we can't retrieve the trcfmt (not fatal)
}

phase_clean()
{
    remote_cmd "rm -f /tmp/cdat.trc"
    [ $? -eq 0 ] || exit 128
}

PATH=$CDAT_SRVC_DIR:${PATH}

while getopts d: opt
do
    case $opt in
    d)
	# duration
	dval="$OPTARG"
	;;
    ?)
	mlog -1 "Usage: trace [-d duration] -- [trace options]"
	exit 128
	;;
    esac
done

shift $(($OPTIND - 1))
opts=$*

# Check trace options to make sure they do not conflict with the
# options that are forced.

# List of trace options with optargs:
traceoptargs="CokjJKrmTLF@AtPxXe"
# This list is necessary to distinguish options from optargs, for
# example in trace -ofilex, "filex" is an optarg, not the list of
# options "-f -i -l -e -x".

while [ $# -gt 0 ]
do
    # Check if trace option -d is specified
    echo $1 | grep -q "^-[^$traceoptargs]*d"
    [ $? -eq 0 ] && {
	mlog -1 "trace option -d is not supported in this environment"
	exit 128
    }
    # Check if trace option -a is specified
    echo $1 | grep -q "^-[^$traceoptargs]*a"
    [ $? -eq 0 ] && {
	mlog -1 "trace option -a is not supported in this environment"
	exit 128
    }
    # Check if trace option -o is specified
    echo $1 | grep -q "^-[^$traceoptargs]*o"
    [ $? -eq 0 ] && {
	mlog -1 "trace option -o is not supported in this environment"
	exit 128
    }
    # Check if trace option -x is specified
    echo $1 | grep -q "^-[^$traceoptargs]*x"
    [ $? -eq 0 ] && {
	# -d and -x are not compatible
	[ -z "$dval" ] || {
	    mlog -1 "trace option -x cannot be used with a duration"
	    exit 128
	}
	xopt=1
    }
    shift
done

# default to 30 seconds
[ -z "$dval" ] && dval=30

case $CDAT_PHASE in
check)
    phase_check
    ;;
execute)
    phase_execute
    ;;
terminate)
    phase_terminate
    ;;
grab)
    phase_grab
    ;;
clean)
    phase_clean
    ;;
esac

exit 0
