#!/bin/bash
#  convert trace data and display it by  Geomview.
work=/tmp/$USER/Work
mkdir -p $work


#****************** fix below for your pupose
vdir=$work
# ptcl code to be displayed. (charged/neutral/both can be
#                             specfied from command line)
ptcls="-1 1 2 3 4 5 6 9"
#***********************************


# --------------------
# Display type.
# 1)  only trace data. 
#
#     Usage:  ./disptracebygeomv [-{c|z|b}]  tracedata 
#
# 2)  Trace data + detector
#
#     Usage: ./disptracebygeomv  [-{c|z|b}] tracedata  det [-e|-a][-s][-i] [outdir]
#
# where
#   [-{c|z|b}] specifies display of charged particles only
#           or neutral particles only, or both. partilce codes
#           to be displayed are given in $ptcls. You must be carful
#           to use -z or -b; huge number of gamma rays must be
#           displayed.  The default is -c.
#   det:    should be given literally.  detector is also displayed
#   -e:     detector display is by each component base
#   -a:     //                  all-in-one 
#   -s:     skip drawing a kind of planer surfaces. (typically 
#           in the case of fpolygon; if it is concave, 
#           geomview may fail to draw it correctly. 
#           In Geomview, making transparecy <1 will save the problem
#           but anther problem may arise as to which of the object
#           is far.
#   -i:     detector color and material correspondence is displayed
#  outdir;  working directory to contain files for Geomview.
#           default is /tmp/$USER/Work.
#
#  To fix the color for each particle code, colortab is used.
#  ===============
#

function showerror() {
    if [ $# -lt 1 ] || [ $# -gt 7 ]; then
	echo "You gave "  $# " arguments"
	echo " Usage: "
	echo "./disptracebygeomv [-{c|z|b}] tracedata  [det] ..."
	echo " "
	awk ' $2 ~ /---------/, $2 ~ /===========/' $0 | less
	exit 1
    fi
}

chg="-c"
if [ "$1" == "-c" ] || [ "$1" == "-z" ] || [ "$1" == "-b" ]; then
    chg=$1
    shift
else
    chg="-c" 
#elif [ "$1" ~ "-"* ]; then
#    echo $1 " is not supported "
#    showerror;
fi


if [ $# -ge 2 ]; then
    if [ $2 == "det" ]; then
        case="2"
    else 
 	showerror;
    fi
elif  [ $# -eq 1 ]; then
    case="1"
else
    showerror;
fi


#  rm -f  $vdir/ptcl*.vect

find $vdir -name 'ptcl*.vect' -print | xargs rm -f


for  code in  ${ptcls};  do
    for charge in `echo "n z p"`; do
	if [ "$code" == "1" ] && [ "$charge" != "z" ]; then
	    continue
	fi

	if [ "$code" == "2" ] && [ "$charge" == "z" ]; then
	    continue
	fi

	if [ "$code" == "3" ] && [ "$charge" == "z" ]; then
	    continue
	fi

#            neglect pi0 tracks
	if [ "$code" == 4 ] && [ "$charge" == "z" ]; then
	    continue
	fi

	if [ "$chg" == "-c" ]  && [ "$charge" == "z" ]; then
	    continue
	fi
	
	if [[ ("$chg" == "-c" && "$charge" != "z" ) || \
	      ("$chg" == "-z" && "$charge" == "z" ) || \
   	      ("$chg" == "-b") ]];  then
    	       awk -f ./first.awk chg=$charge \
		   code=$code  $1 > $vdir/vertexdata
               x=`ls -l $vdir/vertexdata` 
	       if [ `echo ${x} | awk '{print $5}'` -gt 2 ]; then
		   echo "VECT" > $vdir/ptcl${code}${charge}.vect
		   awk -f ./second.awk $vdir/vertexdata >> $vdir/ptcl${code}${charge}.vect
      		   cat $vdir/vertexdata >> $vdir/ptcl${code}${charge}.vect
		   if [ $code -le  6 ]; then
	   	       awk '$2==code && $3==chg {print $4, $5, $6, $7}' \
			   code=$code chg=$charge colortab >> \
			   $vdir/ptcl${code}${charge}.vect
		   elif [ $code -eq 9 ]; then
	   	       awk '$2 == code  {print $4, $5, $6, $7}' \
			   code=$code chg=$charge colortab >> \
			   $vdir/ptcl${code}${charge}.vect
		   else
	   	       awk '$2 == "any" {print $4, $5, $6, $7}' \
			   colortab >> $vdir/ptcl${code}${charge}.vect
		   fi

      		   ./splitiftoolarge $vdir/ptcl${code}${charge}.vect
               fi
	fi
    done
done

echo "case is " $case
echo "GEOMVIEW is " $GEOMVIEW

case $case  in
     "1")
	 $GEOMVIEW  $vdir/ptcl???*.vect ;;
     "2")
         shift; shift ;
         source ./convconfig $argv;

         if [ $dispcolormap == "yes" ]; then
        	$GEOMVIEW  $vdir/mediatext ;
		sleep 1;
         fi

         $GEOMVIEW `find $outdir -name '*.list' -print` $vdir/ptcl???*.vect ;;
esac
