#!/bin/csh -f
# SCCSid "@(#)makeall 2.34 11/15/93 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_

		`cat src/rt/VERSION` 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
set newrmake
echo -n "Are you a new Radiance user or with a new address? "
set ans="$<"
if ( "$ans" =~ [yY]* ) then
	csh -f newuser
else
	echo `cat src/rt/VERSION` compiled by `whoami` | mail GJWard@lbl.gov
endif
set special=
set arch=
set opt=
set mach=
set compat=
set extras=
cat << _EOF_

Please select your system type from the following list:

	1)	Sun 3 family (Motorola)
	2)	Sun 4 family (SPARC) running 4.x
	3)	Sun 4 family running 5.x (Solaris 2.x)
	4)	DECstation
	5)	VAXstation running ULTRIX
	6)	DEC Alpha running OSF
	7)	HP workstation
	8)	Sun 386i
	9)	Silicon Graphics running IRIX 3.x
	10)	Silicon Graphics running IRIX 4.x
	11)	Silicon Graphics running IRIX 5.x
	12)	Apple Mac II or Quadra running A/UX
	13)	NeXT
	14)	BSDI BSD/386
	15)	Linux
	16)	Other

_EOF_
echo -n "Choice? "
set arch="$<"
switch ("$arch")
case 1:			# Sun 3
	set mach="-DBSD -f68881 -fsingle /usr/lib/libm.il -DIEEE -DSPEED=3 -DDCL_ATOF"
	set opt="-O -pipe"
	set special="aed sun tiff"
	set compat="bmalloc.o strcmp.o"
	set arch=sun
	breaksw
case 2:			# SunOS 4.x
	set mach="-DBSD -fsingle -DALIGN=double /usr/lib/libm.il -I/usr/openwin/include -L/usr/openwin/lib -DSPEED=12 -DDCL_ATOF"
	set opt="-O -pipe"
	set special="aed sun tiff"
	set compat="bmalloc.o strcmp.o"
	set arch=sun
	breaksw
case 3:			# SunOS 5.x (Solaris 2.x)
	set mach="-DALIGN=double -I/usr/openwin/include -L/usr/openwin/lib -DSPEED=36 -DDCL_ATOF"
	set opt=-O
	set special="aed sun tiff"
	set compat="bmalloc.o strcmp.o getpagesize.o"
	set arch=sun
	breaksw
case 4:			# DECstation
	set mach="-DALIGN=double -DBSD -DSPEED=12"
	set opt="-O"
	set special="aed tiff"
	set compat="malloc.o erf.o strcmp.o"
	set arch=mips
	breaksw
case 5:			# VAXstation
	set mach="-DBSD -DWFLUSH=30 -DSPEED=2"
	set opt="-O"
	set special="aed tiff"
	set compat="malloc.o erf.o strcmp.o"
	set arch=vax
	breaksw
case 6:			# DEC Alpha running OSF
	set mach="-DALIGN=double -DSPEED=64 -Dvoid=char -DDCL_ATOF"
	set opt="-O"
	set compat="bmalloc.o strcmp.o"
	set arch=alpha
	breaksw
case 7:			# HP workstation
	set mach="-DALIGN=double -DSPEED=36"
	set opt="-O"
	set special="tiff"
	set compat="malloc.o getpagesize.o strcmp.o"
	set arch=hpux
	breaksw
case 8:			# Sun 386i
	set mach="-DBSD -DSPEED=3 -DDCL_ATOF"
	set opt="-O"
	set special="aed sun tiff"
	set compate="malloc.o erf.o strcmp.o"
	set arch=sun
	breaksw
case 9:			# SGI IRIX 3.x
	set mach="-DALIGN=double -noprototypes -DSPEED=36"
	set opt="-O"
	set special="sgi tiff"
	set compat="malloc.o strcmp.o"
	set arch=sgi
	breaksw
case 10:		# SGI IRIX 4.x
	set mach="-DALIGN=double -cckr -DSPEED=36"
	set opt="-O2"
	set special="tiff"
	set compat="malloc.o strcmp.o"
	set arch=sgi
	breaksw
case 11:		# SGI IRIX 5.x
	set mach="-DALIGN=double -cckr -DSPEED=36"
	set opt="-O2 -Wl -ignore_unresolved"
	set special="tiff"
	set compat="malloc.o strcmp.o"
	set arch=sgi
	breaksw
case 12:		# Mac II
	set mach="-DSPEED=8 -DDCL_ATOF"
	set opt="-O"
	set special=tiff
	set compat="malloc.o getpagesize.o erf.o strcmp.o"
	set arch=mac2
	breaksw
case 13:		# NeXT
	set mach="-DBSD -DSPEED=6 -DDCL_ATOF"
	set opt="-O"
	set special=aed
	set arch=next
	set compat="bmalloc.o erf.o"
	set canmalloc
	breaksw
case 14:		# BSDI BSD/386
	set mach="-DBSD -DSPEED=25 -D_POSIX_SOURCE -DDCL_ATOF -L/usr/X11/lib"
	set opt="-O"
	set compat="malloc.o erf.o strcmp.o"
	breaksw
case 15:		# Linux
	set opt="-O2"
	set mach="-DBSD -DSPEED=25 -DDCL_ATOF"
	set compat="bmalloc.o erf.o getpagesize.o frexp.o"
	set extras="CC=gcc"
case 16:		# Other
	set opt="-O"
	set compat="bmalloc.o erf.o strcmp.o"
	echo -n "Is the UNIX a Berkeley derivative [n]? "
	if ( "$<" =~ [yY]* ) then
		set mach="$mach -DBSD"
		set special="$special aed"
	else
		echo -n "Is this OSF UNIX [n]? "
		if ( "$<" =~ [yY]* ) then
			set mach="$mach -Dvoid=char -DDCL_ATOF"
		else
			set compat="$compat getpagesize.o"
		endif
	endif
	echo -n "Is the machine a RISC architecture [n]? "
	if ( "$<" =~ [yY]* ) then
		set mach="$mach -DALIGN=double"
	endif
	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 opt="$opt -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 \
	$extras "\$@" -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
	cp $rmake /tmp/rmake$$
	$EDITOR $rmake
	if ( `cat $rmake /tmp/rmake$$ | grep OPT= | uniq | wc -l` == 2 ) set newrmake
	rm -f /tmp/rmake$$
endif
if ( ! -d src/lib ) then
	mkdir src/lib
endif
if ( $?newrmake ) then
	echo 'New rmake command -- running "makeall clean"...'
	csh -f $0 clean
endif
cd src
echo "Making programs..."
set errs=0
foreach i ( common cv cv/dxfcvt gen ot rt px util cal/{ev,calc,rcalc,util} meta )
	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} meta )
	pushd $i
	echo "In directory $i..."
	make -f Rmakefile $*
	popd
end
echo "Done."
cd ..
endif
foreach i ( $* )
	if ( $i == clean ) then
		echo "Removing library archives..."
		rm -f src/lib/*.a
	endif
end
exit 0
