#!/bin/bash
#  this is to convert config data created by drawconfig for
#  Geomview display
#

colormap=./colormap


if [ $# -gt 0 ] && [ "$1" == "-h" ]; then
    cat <<EOF
	Usage: convconfig [-e|-a|-h][-s][-i] [outdir config1 config2 ...]
    where
     -h to display this message as you see now.
     -e to display config  by each component base.
    
       If neither  -e nor -a is given, only level 0~1 components are displayed
     -a to display whole detector as one element
     -s skip drawing off-file surface which may be drawn incorrectly by
        geomview (typically fpolygon case)
     -i to  display color index (correspondence betw media vs color.
   
     outdir is the output directory (default /tmp/$USER/Work)
     config are config file such as config.Pb 
        made by drawconfig in Util. Default is /tmp/$USER/Work/config.*
        xx of config.xx must be a media name.
EOF
 exit 0;
fi

if [ $#  -gt 0 ] && [ "$1" == "-e" ]; then
    base="comp"
    shift
elif [[ $# -gt 0 && "$1" == "-a" ]]; then
    base="all"
    shift
else  
    base="lvl0"
fi
#######
echo "base is " $base
######
concave="ok"
if [[ $# -gt 0 && "$1" == "-s" ]]; then
    concave="ng"
    shift
fi

if [[ $# -gt 0 && "$1" == "-i" ]]; then
       dispcolormap="yes"
       shift
else
    dispcolormap="no";
fi
    

if [ $# -gt  0 ]; then
    outdir=$1
    shift
else	
    outdir=/tmp/$USER/Work
fi
    
exist=` ls * | awk '$0 ~ /^temp/' | wc -l`
if [ $exist -gt  0 ]; then
    rm -f temp
fi

exist=` ls $outdir/ | awk '$0 ~ /\.mesh$/' | wc -l`

if [ ${exist} -gt 0 ]; then
    echo "There are $exist .mesh files in " $outdir
    echo "They are being removed "
    find $outdir -name '*.mesh' -print | xargs rm -f
fi

exist=`ls $outdir/ | awk '$0 ~ /\.off$/' | wc -l`
if [ $exist -gt 0 ]; then
    echo "There are $exist .off files in " $outdir
    echo "They are being removed "
    find $outdir -name '*.off' -print | xargs rm -f
fi

exist=` ls $outdir/ | awk '$0 ~ /\.list$/' | wc -l`

if [ $exist -gt 0 ]; then
    echo "There are $exist .list files in " $outdir
    echo "They are being removed"
    find $outdir -name '*.list' -print | xargs rm -f
fi
#    rm -f $outdir/allobj.list  # no need for below


echo "LIST" > $outdir/allobj.list

#  for each config.xx; do next; store all objects in allobj.list
echo "processing each config.XX file"
let nm=0

if [ $# -gt 0 ]; then
    while [ $# -gt 0 ];do
	  let n++;
	  ./dispconfigbygeomv2 $concave $outdir $1 $colormap
	        ./showmediacolor2 $outdir $1 $colormap  $n
		shift
    done
else

    for f in `awk 'NF>4 {gsub(/\"/,"");print $2}' /tmp/$USER/Work/gnu`;do
	     nl=`wc -l $f`
	     if [ `echo ${nl} |  awk '{print $1}'` -gt 0 ]; then
		 ./dispconfigbygeomv2 $concave $outdir $f $colormap
		 let n++;
		 ./showmediacolor2 $outdir $f $colormap $n
	     fi
    done
fi


if [ $base == "comp" ]; then

	echo "decomposing objects into each component file"
	gawk -f ./geomviewconfig2.awk  outdir=$outdir $outdir/allobj.list
	rm -f $outdir/allobj.list

elif [ $base == "lvl0" ]; then

    echo "decomposing objects into level 0~1 subdetectors and components files"
    #      next awk must be gawk (why ?) 
        gawk -f ./geomviewconfig3.awk  outdir=$outdir $outdir/allobj.list
	rm -f $outdir/allobj.list
fi
