#!/bin/bash
# plot histgram from logscale distribution
# 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>

DISTFILE=
OUTFILE=

ALL_KEYS=1
DONT_RUN_VIEWER=0
USE_ALIAS=0

NAME=$2

trap 'cleanup $?' SIGINT SIGHUP SIGQUIT SIGTERM EXIT

function cleanup()
{
    if [ -f "$PDATA" ]; then
	rm "$PDATA"
    fi
    if [ -f "$SDATA" ]; then
	rm "$SDATA"
    fi
    exit $1
}

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

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

function mkplotdata()
{
    local LINE KEY ALIAS
    local DATA RANGES
    local enc_key i j NR_COLUMNS

    # skip the first line
    read LINE

    read -a RANGES
    NR_COLUMNS=0
    while [ -n "${RANGES[$NR_COLUMNS]}" ]; do
	let NR_COLUMNS++
    done

    i=0
    while read -a DATA; do
	if [ $USE_ALIAS -ne 0 ]; then
	    KEY=${DATA[1]}
	else
	    KEY=${DATA[0]}
	fi

	if [ $ALL_KEYS -eq 0 ]; then
	    enc_key=`encode_key "$KEY"`
	    [ "${KEYS[asc_$enc_key]}" != "$KEY" ] && continue
	fi

	for ((j=2; j<NR_COLUMNS; j++)); do
	    echo ${RANGES[$j]} ${DATA[$j]}
	done
	echo -e "\n\n"
	TITLES[$i]="$KEY"
	let i++
    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 1:2 title \"$k\" with steps"
	comma=','
	let i++
    done
    echo
}

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:no: "$@"`
if [ $? -ne 0 ]; then
    usage
    exit 1
fi

NR_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" | tr ',' ' '`; do
		let NR_KEYS++
		enc_k=`encode_key "$k"`
		eval asc_$enc_k=$NR_KEYS
		KEYS[asc_$enc_k]="$k"
	    done
	    shift 2
	    ;;
	-n)
	    DONT_RUN_VIEWER=1
	    shift
	    ;;
	-o)
	    OUTFILE=$2
	    shift 2
	    ;;
	--)
	    shift
	    break
	    ;;
	*)
	    exit 1
	    ;;
    esac
done

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

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

TITLE=`head -n 1 $DISTFILE | sed s/analyzer/distribution-graph/g`
if [ -z "$OUTFILE" ]; then
    OUTFILE=$DISTFILE.pdf
fi

PDATA=`mktemp /tmp/tmp$$.XXXXXX`
mkplotdata < "$DISTFILE" > $PDATA
if [ $? -eq 0 ]; then
    echo "error: This log doesn't include any specified keys"
    exit 1
fi

gnuplot << EOF > "$DISTFILE".ps
set title "$TITLE"
set logscale x
set xlabel "sec"
set ylabel "count"
set key box
set terminal postscript color
`print_plot_strings ${TITLES[*]}`
EOF
rc=$?
if [ $rc -ne 0 ]; then
    exit $rc
fi 

ps2pdf "$DISTFILE".ps "$OUTFILE"
rm "$DISTFILE".ps

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

exit 0
