#!/bin/ksh

xmin=-1
xmax=-1
ymin=-1
ymax=-1
title=""
xlabel=""

################################## Check usage #############################3
display_help()
{
    echo "Usage:  plotit [-a <xmin> -b <xmax> -c <ymin> -d <ymax> ] [-t <title>] [-x <label on the x axis> -y <label on the y axis> <data file>"
    echo "   -a,-b,-c,-d: To zoom into a specific part of your graph";
    echo "   -t: To specify a title for your graph"; 
    echo "   -x: To specify a label on the x axis for your graph";
    echo "   -y: To specify a label on the y axis for your graph";
    exit; 
}

while getopts a:b:c:d:t:x:y: option; do
    case ${option} in
        a) xmin=${OPTARG};;
        b) xmax=${OPTARG};;
        c) ymin=${OPTARG};;
        d) ymax=${OPTARG};;
        t) title=${OPTARG};;
        x) xlabel=${OPTARG};;
        y) ylabel=${OPTARG};;
        ?) echo "bad option '${option}'"; display_help;;
    esac
done

(( SHIFT = OPTIND - 1 ))
shift ${SHIFT}

infile="${1}" 

if [ ! -f "${infile}" ]; then
    echo "Error: input file '${infile}' does not exist"
    display_help;
fi

if [ ${xmin} -ne -1 -a ${xmax} -ne -1 -a ${ymax} -ne -1 -a ${ymin} -ne -1 ]; then
    RANGE="[$xmin:$xmax] [$ymin:$ymax]"
else 
    RANGE=""
fi

################################## Set up gnuplot ##############################

gnuplot > ${infile}.ps <<+
set title '${title}'
show title
set xlabel '${xlabel}'
show xlabel
set ylabel '${ylabel}'
show ylabel
set terminal postscript 'eps'
set logscale x
plot ${RANGE} '${infile}' using 1:2 title 'total' with lines, '${infile}' using 1:3 title 'overhead' \
     with lines, '${infile}' using 1:4 title 'computation' with lines
exit
+ 

