#!/bin/csh -f
#
#
cat << _EOF_

             DESIGNERs RADIANCE INSTALLATION 

This script integrates the features mentioned in the corresponding
workshop contribution.

It is assumed that a normal Radiance version ( 3R4, updated with the
official NBNL patches (3R4p1) ) is already installed and no other
patches are applied so far.

!!  It is important to read through the README first before starting 
!!  this script

You must first answer the following questions.


_EOF_

again1:
echo -n "Where do you want the executables [/usr/bin]? "
set idir=$<
(echo $idir) >/dev/null
if ( $status ) goto again1
set idir=$idir
if ( "$idir" == "" ) then
	set idir=/usr/bin
else if ( "$idir" !~ /* ) then
	echo "Directory must be relative to root, please reenter"
	goto again1
endif
if ( ! -d $idir ) then
	mkdir $idir
	if ( $status ) then
		echo "Cannot create directory, please reenter"
		goto again1
	endif
endif

cat << _EOF_

                  DESIGNERs RADIANCE LICENSE AGREEMENT

This is an inofficial modification of Radiance. The Radiance License
Agreement is not affected by this.
In addition, by installing the patches, you further agree to the following terms:

DESIGNERs RADIANCE, i.e. the modifications of/and additional Radiance 
source code modules are copyright of Carsten Bauer (Berlin).
You may compile and use this software on any machines to which you have
personal access and may share its use with others who have access to the 
same machines. You may not sell or redistribute the code to others without 
the prior express written permission of Carsten Bauer. 
It is simply provided "as is", WITHOUT WARRANTY OF ANY KIND, EITHER 
EXPRESS OR IMPLIED. THE COPYRIGHT HOLDER DOES NOT ASSUME ANY LEGAL LIABILITY 
OR RESPONSIBILITY FOR THE ACCURACY AND FITNESS FOR A PARTICULAR PURPOSE
OF THE PROGRAM AND WILL IN NO EVENT BE LIABLE FOR ANY DAMAGES WHICH
MAY RESULT OUT OF THE USE OR THE INABILITY TO USE THE PROGRAM.


_EOF_
echo -n "Do you understand and accept the terms of this agreement [n]? "
set ans="$<"
if ( "$ans" !~ [yY]* ) exit

echo  "Please check/edit your rmake-script. Integer arguments have" 
echo -n "to be activated with -DIARGS ! Press c when ready to continue ?"
set ans="$<"
if ( "$ans" !~ [cC]* ) exit	 

echo  "Installing.. "


cd ../common
echo  "rebuilding librt ..."
set cerrs = 0

foreach i ( color.h color.c colrops.c cone.h cone.c\
            object.h readobj.c readoct.c otypes.h )

  set num = `fgrep -c "29.07.2002 CB" $i`
  if ( $num == 1 ) then
       echo "$i already patched, skipping"
  else if ( $num == 0 ) then
       patch -n -b -z .org $i ../desrad/$i.patch
  else
       echo "Error in patching $i"
  endif
end                       

rmake
@ cerrs += $status

if( $cerrs ) then  
        echo "Error in making librt.a"
	exit
endif
	        
if (! -e ../lib/librt.a.org) then 
     mv -f ../lib/librt.a ../lib/librt.a.org
     mv -f librt.a ../lib/librt.a
else
     mv -f librt.a ../lib/librt.a
endif

cd ../ot
echo  "rebuilding oconv ..."

foreach i ( bbox.c initotypes.c)
  
   set num = `fgrep -c "29.07.2002 CB" $i`
   if ( $num == 1 ) then
       echo "$i already patched, skipping"
   else if ( $num == 0 ) then
       patch -n -b -z .org $i ../desrad/ot$i.patch
   else
       echo "Error in patching $i"
  endif
end

rmake oconv
if ( $status ) then
     echo "Error in making oconv"
     exit
endif
mv oconv $idir/oconv

cd ../px
echo "rebuilding ra_t16 ..."  
if(! -e ra_t16.c.org) then
        mv ra_t16.c ra_t16.c.org
	cp ../desrad/ra_t16.c ra_t16.c
endif
set num = `fgrep -c "29.07.2002 CB" ra_t16.c`
if ( $num == 1 ) then
     rmake ra_t16
else
     echo "Error in making ra_t16"
endif

if ( $status ) then
     echo "Error in making ra_t16"
     exit
endif
mv ra_t16 $idir/ra_t16
            	

cd ../rt
echo "rebuilding rpict, rview and rtrace ..."

set rterrs = 0

foreach i ( ambcomp.c ambient.c aniso.c data.h data.c initotypes.c m_brdf.c\
            normal.c o_cone.c p_data.c ray.h raytrace.c rmain.c rpict.c\
            rtrace.c rview.c rv3.c source.c )

   set num = `fgrep -c "29.07.2002 CB" $i`
   if ( $num == 1 ) then
         echo "$i already patched, skipping"
   else if ( $num == 0 ) then
         patch -n  -b -z .org $i ../desrad/$i.patch
   else
        echo "Error in patching $i"
   endif
end
     cp -f  ../desrad/lite.h lite.h
     cp -f  ../desrad/lite.c lite.c

set num = `fgrep -c "29.07.2002 CB" Rmakefile`
if ( $num == 1 ) then
         echo "Rmakefile already patched, skipping"
   else if ( $num == 0 ) then
         patch -n  -b -z .org Rmakefile ../desrad/Rmakefile.patch
   else
        echo "Error in patching Rmakefile"
endif

rmake rpict
@ rterrs += $status
if( $rterrs ) then 
       	echo "Error in making rpict"
	exit
endif
mv rpict $idir/rpict

rmake rview
@ rterrs += $status
if( $rterrs ) then                                                                                                           
        echo "Error in making rview"                                                                                   
        exit                                                                                                            
endif    	 
mv rview $idir/rview
rmake rtrace
@ rterrs += $status
if( $rterrs ) then
        echo "Error in making rtrace"
	exit
endif	
mv rtrace $idir/rtrace

cd ../gen

set generrs = 0
echo  "rebuilding generators ..."

set num = `fgrep -c "29.07.2002 CB" Rmakefile`
if ( $num == 1 ) then
  echo "Rmakefile already patched, skipping"
else if ($num == 0) then
  patch -n -b -z .org Rmakefile ../desrad/genRmakefile.patch
else
  echo "Error in patching Rmakefile"
endif   
  
foreach i ( genbox.c genprism.c genworm.c gensurf.c genrev.c xform.c )

  set num = `fgrep -c "29.07.2002 CB" $i`
  if ( $num == 1 ) then
       echo "$i already patched, skipping"
  else if ( $num == 0 ) then
       patch -n -b -z .org $i ../desrad/$i.patch
  else
       echo "Error in patching $i"
  endif
  
  rmake $i:r

  @ generrs += $status
end

if (! -e genmaptab.c) cp ../desrad/genmaptab.c genmaptab.c
rmake genmaptab
@ generrs += $status

if ( $generrs ) then
        echo "Error in building generators"
	exit
endif
foreach i ( genbox genprism genworm gensurf genrev xform )
         mv $i $idir/$i
end
mv genmaptab $idir/genmaptab

echo  "Finished"
exit 0
