#!/bin/csh -f
#  This can be used for Glauber data upto Fe as well as upto Pb
#    sort and awk must be gnusort and gnuawk
# 

if( $#argv != 2 ) then
	echo Usage: fordpmjetGLB  configfile  mediadir
	exit
endif


rm -f  temp_data
touch temp_data
rm -f  temp_target

set first = 0

#                  skip until first ----------
#                  box etc must not include number (0~9)
#        awk '$1~/^[0-9]+/ && $2 !~/^[0-9]+/  {l=index($3,"*"); \
foreach f(`awk '$1~/^----------+/,$1~/^xyzabcrandomslslkdkwo/' $1 | \
        awk '$1~/^[0-9]+/ && $2 !~/^[0-9]/ && $2 !~/^-[0-9]/ && \
       $3 !~ /^[0-9]/ && $3 !~ /^-[0-9]/ {l=index($3,"*"); \
       if(l>0) print substr($3,1,l-1);else print $3}' | sort   |uniq`)
    set j =  `echo $f | awk '{print tolower($1)}' `
    foreach g($2/*.GLB)
#                 x : Pb.GLB
		set x = $g:t
#                 y : Pb 
		set y = $x:r 
#                 z:  pb
		set z = `echo $y | awk '{print tolower($1)}' `

	 	if ( $z == $j ) then
	   	  if( $first == 0 ) then
#                      extract header; get # of header lines
		    set hlines = `awk '(NR==4 || NR==5) && index($1,":") > 0 {print NR;exit}' $g`
		    awk 'NR<line' line=$hlines $g > temp_header

		    set i = 1
		    set l=""
 		    while ( $i <  $hlines ) 
		       set l=($l ` awk 'NR==line {print $1; exit}' line=$i $g `)
#		       echo "l$i=" $l[$i]
		       @ i++
                    end
         	    awk 'NR==line {for(i=2; i<=NF; ++i){ print  $i} }' line=$hlines $g  > temp_target
	  	    set first = 1
                  else	
		    set i = 1
		    while ( $i <  $hlines )
       		       set k=` awk 'NR==line {print $1; exit}' line=$i $g `
		       if( "$l[$i]" != $k ) then
			  echo  $l[$i] 
   	                  echo  $k of $g
	                  echo  differ
	                  exit  1
  	               endif
		       @ i++
                    end
         	    awk 'NR==line {for(i=2; i<=NF; ++i){ print  $i} }' line=$hlines $g  >> temp_target
              	  endif

                  awk 'BEGIN {nl=0};\
                  NR>line && nl==0 {printf("%10d%10d%10d%10d%10d%15.5f\n", $1,$2,$3,$4,$5,$6);nl++;next}; \
                  NR>line {print; ++nl};nl==10 {nl=0}' line=$hlines $g >> temp_data
		  goto ok
               endif
	end	
	echo "no Glauber data found for component media: " $f
	exit 1
ok:
end
#          bug                if "|" below is  "| ", we have serious problem
#             since dpmjet3 uses punch card image data !!!!
awk 'BEGIN{nl=0}; {++nl;printf("%s %s", $0, "|"); if(nl==10) {print "";nl=0} }' temp_data  > temp_data2

#  uniq -w65   | has a potential dangour due to floating point data accuracy in field 6.
#  so we use dirty method.below

sort -n -k 1,2 -k 6,7 -k 3,4 -k 2,3 temp_data2  | \
awk 'BEGIN{for(i=1;i<=6;++i) prev[i]=-100}; {j =0;  \
 for(i=1;i<=5;++i) { if(prev[i] != $i) j=1; prev[i]=$i};\
  if( (prev[6] -$6) < -0.001 ||  (prev[6] -$6) > 0.001 ) j=1; \
  prev[6]=$6; if(j !=0 ) print}'  \
  | awk 'BEGIN {RS="|"; ORS="\n"}; {print}'  | awk 'NF>0' > temp_data



   sort temp_target | uniq > temp_target2
 
   set ntarget = ` wc -l temp_target2 `
    
   awk '{print}' temp_header
   echo  $ntarget[1] | awk '{printf("%4d: ",$1)}'
   awk	'{printf("%4d",$1)}' temp_target2
   echo ""
   cat temp_data

   rm  -f temp_data temp_data2  temp_target temp_target2 temp_header







