#!/bin/csh -f
#
#  compile-all -- script to compile everything
#
# $Header: compile-all,v 1.9 92/03/04 09:55:21 wlott Exp $

set features = ()
set misfeatures = ()
set target = "@sys"
set subdir = alpha
set core = ""
set clean = 0
set update = 1
set bootstrap = "target:bootstrap"

set systems = ""
set nosystems = ""

while ($#argv > 0)
	if ("$argv[1]" !~ -*) then
		set features = ($features $argv[1])
	else
		switch ($argv[1])

# Select what system to compile, and how:
			case "-target":
				set target = $argv[2]
				shift
				breaksw
			case "-release":
				set subdir = $argv[2]
				shift
				breaksw
			case "-lisp":
				set lispdir = $argv[2]
				shift
				breaksw
			case "-core":
				set core = " -core $argv[2]"
				shift
				breaksw
			case "-bootstrap":
				set bootstrap = $argv[2]
				shift
				breaksw
			case "-misfeature":
			        set misfeatures = ($misfeatures $argv[2])
				shift
				breaksw

# Source tree management:
			case "-clean":
				set clean = 1
				breaksw
			case "-noupdate":
			        set update = 0
				breaksw

# Select what to compile:
			case "-compile":
			        set systems = $argv[2]
				shift
				breaksw
			case "-nocompile":
			        set nosystems = $argv[2]
				shift
				breaksw
			default:
				echo "Bogus switch: $argv[1]"
				cat <<END_HELP
Try these:
  -target	[@sys]	
	The machine to compile for: pmax_mach, sun4c_41 ...

  -release	[alpha]
	Which source tree to compile: alpha, exp/foo...

  -lisp		[/afs/cs/misc/cmucl/@sys/<release> or /usr/misc/.cmucl/]
	The directory to run Lisp out of.

  -core		[<lisp>/lib/lisp.core]
	The core file to run.

  -bootstrap	[target:bootstrap]
	File to load into lisp before compiling.

  -misfeature <feature>
	Remove <feature> from the features when compiling.  May be used more
	than once.

  -clean <no arg>
	Delete all *.*f, *.assem, *.log and *.log.OLD in the destination.

  -noupdate <no arg>
	If specified, inhibits rcsupdate of the source tree.

  -compile	[All systems]
	A comma-separated list of system names, e.g. "code,compiler".  Order is
	not significant.  All systems are compiled by default.
  
  -nocompile	[No systems]
	A comma-separated list of systems *not* to compile.  Only meaningful
	when -compile is not specified.

END_HELP

				exit
		endsw
	endif
	shift
end

if (! $?lispdir) then
	set lispdir = /afs/cs/misc/cmucl/@sys/$subdir
	if (! -e $lispdir) then
		echo "Release $subdir not installed; using /usr/misc/.cmucl"
		set lispdir = /usr/misc/.cmucl
	endif
endif
setenv CMUCLLIB "$lispdir/lib"
set lisp = "$lispdir/bin/lisp$core"

if ($systems == "") then
    if ($nosystems == "") then
        echo "Will compile all systems ..."
    else
        echo "Will compile all systems except for: $nosystems ..."
    endif
else
    echo "Will compile these systems: $systems ..."
endif

set src = ()
set thissrc = /afs/cs/project/clisp/src/$subdir

nother_source:

set src = ($src \"$thissrc/\")

if (-e $thissrc/FEATURES) then
        set tmp = (`cat $thissrc/FEATURES`)
        echo "Features from $thissrc/FEATURES file:" $tmp
	set features = ($features $tmp)
endif

if (-e $thissrc/MISFEATURES) then
        set tmp = (`cat $thissrc/MISFEATURES`)
        echo "Misfeatures from $thissrc/MISFEATURES file:" $tmp
	set misfeatures = ($misfeatures $tmp)
endif

if $update then
    echo "Updating source directory $thissrc ..."
    (cd $thissrc; rcsupdate -q)
endif

if (-e $thissrc/SHADOW) then
    set thissrc = `cat $thissrc/SHADOW`
    goto nother_source
endif

echo "Source directory(ies): $src"

set dest = /afs/cs/project/clisp/build/$target/$subdir
echo "Target directory: $dest"

if $clean then
    echo "Cleaning up binaries and logs in $dest ..."
    (cd  $dest;\
     find . \( -name '*.*f' -o -name '*.assem' \) -print -exec rm {} \; ;\
     rm *.log *.log.OLD)
else
    if ({(echo $dest/*.log>/dev/null)}) then
	echo "Preserving log files in $dest as .OLD ..."
	foreach foo ( $dest/*.log )
	    set old = "${foo}.OLD"
	    if (-e $old) then
	        echo "" >>$old
		date >>$old
		echo "_________________________________________">>$old
		cat $foo >>$old
		rm $foo
	    else
		mv $foo $old
	    endif
        end
    endif
endif

if ($?LISP) then
	echo "LISP environment variable override: $LISP"
	set lisp = "$LISP"
endif

echo "Compiling setup and bootstrap ..."
$lisp -noinit -eval '(eval (read))' << EOF
(progn
  (setf *features*
	(set-difference (list* $features *features*) '($misfeatures)))
  (setf (search-list "target:") '("$dest/" $src))
  (setq *compile-verbose* nil *compile-print* nil) 
  (load "target:tools/setup" :if-source-newer :load-source)
  (comf "target:tools/setup" :load t)
  (when (probe-file "${bootstrap}.lisp") (comf "$bootstrap"))
  (quit))
EOF

set sysinfo = ("code worldcom"\
	       "compiler comcom"\
	       "clx clxcom"\
	       "hemlock hemcom"\
	       "pcl pclcom"\
	       "genesis worldbuild")

while ($#sysinfo > 0)
    set system_vec = ($sysinfo[1]:x)
    set this_system = $system_vec[1]
    set this_comfile = $system_vec[2]
    shift sysinfo
    if ($systems =~ *${this_system}* || \
        ($systems == "" && $nosystems !~ *${this_system}*)) then
	echo "Compiling $this_system ..."
	$lisp -noinit -eval '(eval (read))' << EOF
(progn
  (setf *features*
	(set-difference (list* $features *features*) '($misfeatures)))
  (setf (search-list "target:") '("$dest/" $src))
  (setq *compile-verbose* nil *compile-print* nil) 
  (load "target:tools/setup")
  (load "$bootstrap" :if-does-not-exist nil)
  (setf *interactive* nil *gc-verbose* nil)
  (load "target:tools/$this_comfile")
  (quit))
EOF
   endif
end

echo "Done..."
