#!/bin/csh -f
# SCCSid "@(#)makeall 2.13 5/27/92 LBL"
#
# Make all the Radiance programs
#
if ( $#argv < 1 ) then
	echo "Usage: makeall install [clean] [make options]"
	echo "   or: makeall clean"
	exit 1
endif
if ( "$1" == install ) then
	cat << _EOF_

		RADIANCE 2.1 INSTALLATION

This script rebuilds all of the Radiance programs and installs
them on your system.  You should read the file README before running
this script.  You can type ^C (followed by return) at any time to abort.

You must first answer the following questions.

_EOF_
if ( ! $?EDITOR ) then
	echo -n "What is your preferred editor [vi]? "
	set ans="$<"
	if ( "$ans" != "" ) then
		setenv EDITOR "$ans"
	else
		setenv EDITOR vi
	endif
endif
again1:
echo -n "Where do you want the executables [/usr/local/bin]? "
set idir=$<
(echo $idir) >/dev/null
if ( $status ) goto again1
set idir=$idir
if ( "$idir" == "" ) then
	set idir=/usr/local/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
if ( ! -d $idir/dev ) then
	mkdir $idir/dev
	if ( $status ) then
		echo "Cannot create subdirectory, please reenter"
		goto again1
	endif
endif
set rmake=$idir/rmake
if ( "`ls -t $rmake $0 |& head -1`" == $rmake ) goto gotrmake
echo -n "Are you a new Radiance user or with a new address? "
set ans="$<"
if ( "$ans" =~ [yY]* ) then
	csh -f newuser
else
	echo Radiance 2R1 compiled by `whoami` | mail GJWard@lbl.gov
endif
cat << _EOF_

Please select your system type from the following list:

	1)	Sun 3 family
	2)	Sun 4 family (including SPARCstation)
	3)	DECstation
	4)	VAXstation running ULTRIX
	5)	HP workstation
	6)	Sun 386i
	7)	Silicon Graphics running IRIX 3.x
	8)	Silicon Graphics running IRIX 4.x
	9)	Mac II running A/UX
	10)	Stardent
	11)	NeXT
	12)	Other

_EOF_
echo -n "Choice? "
set arch="$<"
switch ("$arch")
case 1:			# Sun 3
	set mach="-DBSD -f68881 -fsingle /usr/lib/libm.il -DIEEE"
	set opt="-O -pipe"
	set special="aed sun tiff"
	set compat="bmalloc.o"
	set arch=sun
	breaksw
case 2:			# Sun 4
	set mach="-DBSD -fsingle -DALIGN=double /usr/lib/libm.il"
	set opt="-O -pipe"
	set special="aed sun tiff"
	set compat="bmalloc.o"
	set arch=sun
	breaksw
case 3:			# DECstation
	set mach="-DALIGN=double -DBSD"
	set opt="-O"
	set special="aed tiff"
	set compat="malloc.o erf.o"
	set arch=mips
	breaksw
case 4:			# VAXstation
	set mach="-DBSD"
	set opt="-O"
	set special="aed tiff"
	set compat="malloc.o erf.o"
	set arch=vax
	breaksw
case 5:			# HP workstation
	set mach="-DALIGN=double"
	set opt="-O"
	set special="tiff"
	set compat="malloc.o getpagesize.o"
	set arch=hpux
	breaksw
case 6:			# Sun 386i
	set mach="-DBSD"
	set opt="-O"
	set special="aed sun tiff"
	set compate="malloc.o erf.o"
	set arch=sun
	breaksw
case 7:			# SGI IRIX 3.x
	set mach="-DALIGN=double -noprototypes"
	set opt="-O"
	set special="sgi tiff"
	set compat="bmalloc.o"
	set arch=sgi
	breaksw
case 8:			# SGI IRIX 4.x
	set mach="-DALIGN=double -cckr"
	set opt="-O"
	set special="tiff"
	set compat="bmalloc.o"
	set arch=sgi
	breaksw
case 9:			# Mac II
	set mach=
	set opt="-O"
	set special="tiff"
	set compat="malloc.o getpagesize.o erf.o"
	set arch=mac2
	set mem=
	set sizemod="large models (8,000+ surfaces)"
	breaksw
case 10:		# Stardent
	set mach="-DALIGN=double"
	set opt="-O"
	set special=
	set compat="bmalloc.o erf.o getpagesize.o"
	set arch=stardent
	set mem="-DBIGMEM"
	set sizemod="very large models (24,000+ surfaces)"
	breaksw
case 11:		# NeXT
	set mach="-DBSD -Dstrcmp=Zstrcmp"
	set opt="-O"
	set special=aed
	set arch=next
	set compat="bmalloc.o erf.o"
	set canmalloc
	breaksw
case 12:		# Other
	set mach=
	set opt="-O"
	set special=
	set compat="bmalloc.o erf.o"
	echo -n "Is the UNIX a Berkeley derivative [n]? "
	if ( "$<" =~ [yY]* ) then
		set mach="$mach -DBSD"
		set special="$special aed"
	else
		set compat="$compat getpagesize.o"
	endif
	echo -n "Is the machine a RISC architecture [n]? "
	if ( "$<" =~ [yY]* ) then
		set mach="$mach -DALIGN=double"
	endif
	set arch=
	breaksw
default:
	echo "Illegal choice\!"
	echo "Installation aborted."
	exit 1
	breaksw
endsw
if ( $?mem ) then
	set mach="$mach $mem"
else
	echo -n "Does the target machine have more than 8 Mbytes of RAM? "
	set ans="$<"
	if ( "$ans" =~ [yY]* ) then
		echo -n "More than 24 Mbytes? "
		set ans="$<"
		if ( "$ans" =~ [yY]* ) then
			set mach="$mach -DBIGMEM"
			set sizemod="huge models (60,000+ surfaces)"
		else
			set sizemod="very large models (16,000+ surfaces)"
		endif
	else
		set sizemod="large models (8,000+ surfaces)"
	endif
endif
echo "Do you expect to be rendering $sizemod?"
echo -n '(Answering "yes" may adversly affect rendering time and geometric accuracy) '
set ans="$<"
if ( "$ans" =~ [yY]* ) then
	set mach="$mach -DSMLFLT"
endif
#echo -n "Do you have X10 support [n]? "
#if ( "$<" =~ [yY]* ) then
#	set special="$special x10"
#endif
if ( $?canmalloc ) then
	chmod 644 src/{ot,rt}/Rmakefile
	ed - src/ot/Rmakefile <<'_EOF_'
1,$s/malloc\.o//
w
e src/rt/Rmakefile
1,$s/malloc\.o//
w
q
'_EOF_'
endif
again2:
echo -n "Where do you want the library files [/usr/local/lib/ray]? "
set ldir=$<
(echo $ldir) >/dev/null
if ( $status ) goto again2
set ldir=$ldir
if ( "$ldir" == "" ) then
	set ldir=/usr/local/lib/ray
else if ( "$ldir" !~ /* ) then
	echo "Directory must be relative to root, please reenter"
	goto again2
endif
if ( $ldir != $cwd/lib ) then
	if ( ! -d $ldir ) then
		mkdir $ldir
		if ( $status ) then
			echo "Cannot create directory, please reenter"
			goto again2
		endif
	endif
	cmp -s lib $ldir
	if ($status) then
		echo -n "Install library files now [n]? "
		if ( "$<" =~ [yY]* ) then
			echo -n "Copying library files to $ldir... "
			(cd lib ; tar cf - *) | (cd $ldir ; tar xf -)
			echo "Done."
		endif
	endif
endif
cat > $rmake << _EOF_
#!/bin/sh
exec make "SPECIAL=$special" \
	"OPT=$opt" \
	"MACH=$mach" \
	ARCH=$arch "COMPAT=$compat" \
	INSTDIR=$idir \
	LIBDIR=$ldir "\$@" -f Rmakefile
_EOF_
chmod 755 $rmake
chmod 644 src/*/Rmakefile src/rt/devtable.c
gotrmake:
echo "Current rmake command is:"
cat $rmake
echo -n "Do you want to change it? "
set ans="$<"
if ( "$ans" =~ [yY]* ) then
	$EDITOR $rmake
endif
cd src
if ( ! -d lib ) then
	mkdir lib
endif
echo "Making programs..."
set errs=0
foreach i ( common cv cv/dxfcvt gen ot rt px util cal/{ev,calc,rcalc,util} )
	pushd $i
	echo "In directory $i..."
	$rmake -k $*
	@ errs += $status
	popd
end
if ( $errs ) then
	echo "There were some errors."
else
	echo "Done."
endif
cd ..
else
cd src
foreach i ( common cv cv/dxfcvt gen ot px rt util cal/{ev,calc,rcalc,util} )
	pushd $i
	echo "In directory $i..."
	make -f Rmakefile $*
	popd
end
echo "Done."
cd ..
endif
exit 0
