#!/bin/csh -f
# This is a front end to gcc (Gnu c++ compiler) which
# applies some changes to source files before compilation. 
# These changes are applied only when a file is on the list
# below (criticalFiles). List of changes is stored in the array "changes" 
# (format of arguments to sed)

echo ============ THIS IS A PATCHED G++ ===============

					# here is the list of files that
					# should be preprocessed 

set criticalFiles = ( plcm.C pattern.C layMap.C imageMap.C cri.C compact.C )

					# here are modyfications (in sed format) 
					# which should be applied to the source
					# file
 
set changes = ( 's/(Item&)\([0-9A-Za-z+]*\)/\1.get()/g' )


if ( ! $?MACHINE ) then
   echo set your MACHINE environment first ...
   exit 1
endif

switch ("$MACHINE")
case "*Linux*":
     set CC=/usr/bin/g++
     breaksw
case "PA-RISC*":
case "HP*":
case "hppa*":
     set CC=/usr/local/bin/g++
     breaksw
default:
     set CC=/usr/local/bin/g++
     breaksw
endsw

set tmpFile = /tmp/gcc$$tmp.C
set tmpFile2 = /tmp/gcc$$tmp2.C
set exitstatus=0

# if a ^C comes in, clean-up the mess ...:
onintr cleanup

                                        # first let's find the name of the 
					# file to compile

set  name=""                            # file name to compile		       
set  outputNameGiven = 0
set noS = 1

foreach token ($argv)
	set match = `expr $token  : '.*\.[Cc]'`
	if($match > 0) then
		set name = $token
		set noS = 0
	endif
	if ( "$token" == "-o") then
		set outputNameGiven = 1
	endif

end


					# check if this is one of the 
					# critical files
set  isCritical = 0
foreach file ($criticalFiles)
	set cName = `basename $file`
	if ("$name" == "$cName") then
	   set isCritical = 1
	   goto jumpHere  
	endif
end

jumpHere:

					# first special case : program thedate
if("$name" == "thedate.C") then
  set today = `date | awk '{OFS = "_"} {print $1,$2,$3,$6}'`
  set myname = `whoami`
  set myhost = `hostname`
  $CC -I. -I/usr1/ocean/include -I../partitioner -I../phil  -DTHEDATE=\"$today\"  -DTHEHOST=\"$myhost\" -DTHEMAN=\"$myname\" -c thedate.C -o objects/$MACHINE/thedate.o
  exit $status
endif
					# no C source on the command line
					# might be a call to a linker
					# start compiler directly
					# or it's just an ordinary file


if ( $noS == 1 || ! $isCritical ) then
   $CC $* ; set exitstatus=$status
else
	#first we have to change some command line arguments ..
   touch $tmpFile
   foreach token ($argv)
	   if ( "$token" != "$name") then
	   	echo "$token " >> $tmpFile 	   
	   else
	   	echo "$tmpFile " >> $tmpFile
	   endif
   end
   set givenCommand = `cat $tmpFile`

			   		# now apply all the changes

   cat $name > $tmpFile2
   set toApply = $#changes
   set cnt = 1
   while ($cnt <= $toApply)
	    echo ==== PREPROCESS: sed -e "'$changes[$cnt]'" ====
	    sed -e "$changes[$cnt]" $tmpFile2 > $tmpFile 	    
	    cp $tmpFile $tmpFile2
	    @ cnt ++
   end
   set extraCommand = ""
   if ($outputNameGiven != 1) then
      set outputName = `echo $name | sed -e "s/[.][Cc]/.o/g"` 
      set extraCommand = `echo "-o $outputName"`
   endif
   $CC $givenCommand $extraCommand ; set exitstat=$status
endif

cleanup:
# set env NO_REMOVE if you want to look at the results of the sed:
if ( ! $?NO_REMOVE ) then
   rm -f $tmpFile $tmpFile2
endif

exit $exitstatus
