#!/bin/bash
# plot logdata from logfile
# Copyright (C) HITACHI,LTD. 2005
# WRITTEN BY HITACHI SYSTEMS DEVELOPMENT LABORATORY,
# Created by M.Hiramatsu <hiramatu@sdl.hitachi.co.jp>
# Updated by H.Kawai <h-kawai@sdl.hitachi.co.jp>

LOGFILE=
OUTFILE=
IKNAME=
LSTYLE="steps lw 1"
TMP=
PDATA=
USE_ALIAS=0
DONT_RUN_VIEWER=0

trap 'cleanup $?' SIGINT SIGHUP SIGQUIT SIGTERM EXIT

function cleanup() {
    if [ -f "$TMP" ]; then
	rm "$TMP"
    fi
    if [ -f "$PDATA" ]; then
	rm "$PDATA"
    fi

    exit $1
}

function usage() {
    echo "lkst_plot_log [OPTION] <log_file>"
    echo "OPTION"
    echo "  -a                 use alias insted of infokey"
    echo "  -k infokey[,...]]  plot a graph about data indicated by infokeys"
    echo "  -l linestyle       gnuplot line style"
    echo "  -n                 don't run pdf viewer after plotting a graph"
    echo "  -o outfile         output to outfile as pdf format"
    echo "                     (default: <log_file>.pdf)"
}

function encode_key() {
    echo "$1" | tr '+-/*.()' '_____\0\0'
}

function mkplotdata()
{
    local IK ALN OTHERS
    local i=0
    local prev_key=
    local enc_key=
    local key_changed=0

    while read -s IK ALN OTHERS ; do
	if [ $USE_ALIAS -ne 0 ]; then
	    IK="$ALN"
	fi

	if [ "$IK" != "$prev_key" ]; then
	    enc_key=`encode_key "$IK"`
	    prev_key="$IK"
	    key_changed=1
	fi

	if [ $ALL_KEYS -ne 0 -o -n "${KEYS[asc_$enc_key]}" ]; then
	    if [ $key_changed -ne 0 ]; then
		echo -e "\n\n"
		TITLES[$i]="$IK"
		let i++
		key_changed=0
	    fi
	    echo $IK $ALN $OTHERS
	fi
    done

    return $i
}

function print_plot_strings() {
    local i=0
    local comma=
    local k

    echo -n "plot "
    for k in $*; do
	echo -n "$comma \"$PDATA\" index $i using 3:4 title \"$k\" with $LSTYLE"
	comma=','
	let i++
    done
}

if [ "$PDF_VIEWER" ] ; then
    VPDF=$PDF_VIEWER
else
    VPDF=`which acroread 2> /dev/null`;
    if [ -z "$VPDF" ]; then
	VPDF=`which xpdf 2> /dev/null`;
    fi
    if [ -z "$VPDF" ]; then
	VPDF=`which gpdf 2> /dev/null`;
    fi
    if [ -z "$VPDF" ]; then
	VPDF=echo
    fi
fi

OPTS=`getopt -- ak:l:no: "$@"`
if [ $? -ne 0 ]; then
    usage
    exit 1
fi

ALL_KEYS=1
NR_KEYS=0
KEYS[0]=
eval set -- $OPTS
while [ $# -ne 0 ]; do
    case $1 in
	-a)
	    USE_ALIAS=1
	    shift
	    ;;
	-k)
	    ALL_KEYS=0
	    for k in `echo "$2" | sed -e 's/,/ /g'`; do
		let NR_KEYS++
		enc_k=`encode_key "$k"`
		eval asc_$enc_k=$NR_KEYS
		KEYS[asc_$enc_k]="$k"
	    done
	    shift 2
	    ;;
	-l)
	    LSTYLE="$2"
	    shift 2
	    ;;
	-n)
	    DONT_RUN_VIEWER=1
	    shift
	    ;;
	-o)
	    OUTFILE=$2
	    shift 2
	    ;;
	--)
	    shift
	    break
	    ;;
	*)
	    usage
	    exit 1
	    ;;
    esac
done

if [ $# -lt 1 ] ;then 
    usage
    exit 1;
fi

LOGFILE=$1
if [ -z "$OUTFILE" ]; then
    OUTFILE=${LOGFILE}.pdf
fi

if [ ! -f $LOGFILE ] ;then
    echo "$LOGFILE is not found"
    exit 1;
fi

TITLE=`head -n 1 $LOGFILE | sed s/analyzer/graph/g`
DTITLE=`head -n 2 $LOGFILE | tail -n 1 | xargs | cut -d\  -f 4 `

PDATA=`mktemp /tmp/tmp$$.XXXXXX`
TMP=`mktemp /tmp/tmp$$.XXXXXX`

NAME=`head -1 $LOGFILE`

if [ $USE_ALIAS -eq 0 ]; then
    tail -n+3 "$LOGFILE" | sort -n -k 3 | sort -k 1 > $TMP
else
    tail -n+3 "$LOGFILE" | sort -n -k 3 | sort -k 2 > $TMP
fi

mkplotdata "$IKNAME" <$TMP >$PDATA
if [ $? -eq 0 ]; then
    echo "error: This log doesn't include any specified keys"
    exit 1
fi

gnuplot << EOF | sed -e "s/^currentpoint gsave translate 90 rotate 0 0 M/\
currentpoint gsave translate 45 rotate 0 0 M/g" \
-e s@"^($DTITLE) Cshow"@\
"currentpoint gsave translate 45 rotate 0 0 M ($DTITLE) Cshow grestore"@g \
>  $LOGFILE.ps
set title "$TITLE"
set xlabel "sec"
set ylabel "$DTITLE"
set key box
#set key top outside
set format x "%f"
set xtics axis rotate
set yrange [] nowriteback
set terminal postscript color
`print_plot_strings ${TITLES[*]}`
EOF
rc=$?
if [ $rc -ne 0 ]; then
    exit $rc
fi 

ps2pdf $LOGFILE.ps $OUTFILE 
rm $LOGFILE.ps

if [ $DONT_RUN_VIEWER -eq 0 ]; then
    $VPDF $OUTFILE
fi

exit 0
