#!/bin/sh
#
#    File:  gnu_det.sh
#
# Purpose:  To format a "DET Curve" plot file that can be read by
#           gnuplot.
#
#   Usage:  gnu_det.sh  "title"  inputfile
#
#           where: "title"  --> The title is in quotes
#                  inputfile -> list of files each followed by
#                               a distinct key name
#
#                         EXAMPLE:
#                              
#                          DET_bbn1_2h bbn1
#                          DET_mit1_2h mit1
#                          DET_sri1_2h sri
#
#  USAGE STATEMENT
   if [ "$#" -ne 2 ] ;then
      echo 
      echo "Usage:   gnu_det.sh \"DET_title\" inputfile"
      echo
      exit
   fi
#
#  SELECT ECHO WITH NO NEW LINE CAPABILITY
   if [ -f /usr/ucb/echo ] ;then
      ECHO=/usr/ucb/echo
   else
      ECHO=echo
   fi

#
#  READ THE COMMAND LINE ARG's
   TITLE=${1}
   INFILE=${2}


#
# 
  echo "## GNUPLOT command file" 
  echo "set terminal postscript color"
  echo "set data style lines"
  echo "set key .5,0"
  echo "set size 0.78, 1.0"
  echo "set noxtics" 
  echo "set noytics"
  echo "set title "\'${TITLE}\'
  echo "set ylabel \"Miss probability (in %)\""
  echo "set xlabel \"False Alarms probability (in %)\""
  echo "set grid"
  ${ECHO} -n "set ytics (\"0.1\" -3.08, \"0.5\" -2.57, \"2\" -2.05, \"5\" -1.64,"
  echo  " \"10\" -1.28, \"20\" -0.84, \"30\" -0.52, \"40\" -0.25, \"50\" 0.0)"
#
  ${ECHO} -n "set xtics (\"0.1\" -3.08, \"0.5\" -2.57, \"2\" -2.05, \"5\" -1.64,"
  echo  " \"10\" -1.28, \"20\" -0.84, \"30\" -0.52, \"40\" -0.25, \"50\" 0.0)"
#
  ${ECHO} -n "plot [-3.290527:0.0] [-3.290527:0.0] "

#
# PLOT EACH DATA FILE
#
  flag=0
  color=1
  while read tline ;do
     
	tfile=`echo ${tline} | awk '{print $1}'`
	tcolor=`echo ${tline} | awk '{print $2}'`
        tlinetype=`echo ${tline} | awk '{print $3}'`
        tkey=`echo ${tline} | awk '{print $4}'`

	if [ "${tcolor}" = "green" ] ;then
	   color=1
 	elif [ "${tcolor}" = "blue" ] ;then
	   color=2
 	elif [ "${tcolor}" = "red" ] ;then
	   color=3
 	elif [ "${tcolor}" = "magenta" ] ;then
	   color=4
 	elif [ "${tcolor}" = "cyan" ] ;then
	   color=5
 	elif [ "${tcolor}" = "yellow" ] ;then
	   color=6
 	elif [ "${tcolor}" = "black" ] ;then
	   color=7
 	elif [ "${tcolor}" = "brown" ] ;then
	   color=8
 	elif [ "${tcolor}" = "grey" ] ;then
	   color=9
	else
	   echo "I do not know about color: "${tcolor}
	   echo "Choose From:"
	   echo "             green   blue   red    magenta  cyan"
    	   echo "             yellow  black  brown  grey"
	   exit
	fi

	if [ ${tlinetype} = "line" ] ;then
	   linetype="lines ${color} 1"
	elif [ ${tlinetype} = "circle" ] ;then
	   linetype="points ${color} 1"
        elif [ ${tlinetype} = "star" ] ;then
	   linetype="points ${color} 6"
	else
	   echo "I do not know about line type: "${tlinetype}
	   echo "Choose From:"
	   echo "             line  circle  star"
           exit
	fi

     if [ ${flag} -eq "0" ] ;then
        flag=1
        ${ECHO} -n "\"${tfile}\" using 2:1 title \"${tkey}\" with ${linetype}"
     else
        echo ", \\"
        ${ECHO} -n "\"${tfile}\" using 2:1 title \"${tkey}\" with ${linetype}"
     fi

  done <${INFILE}

  echo ""
exit