#!/bin/csh -f
#
#  compile-all -- script to compile everything
#
# $Header: compile-all,v 1.16 94/10/30 21:04:29 ram Exp $

set features = ()
set misfeatures = ()
set target = "@sys"
set subdir = alpha
set core = ""
set interactive = "nil"
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
			case "-interactive":
				set interactive = "t"
				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.

  -interactive <no arg>
	Print compiler output to terminal instead of log files.

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

  -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 (-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 ({(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


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

echo "Compiling setup and bootstrap ..."
sed -e 's/\\//g' << EOF | $lisp -noinit -eval '(eval (read))'
(progn
  (when (find-package "INTERFACE")
    (set (intern "*INTERFACE-STYLE*" "INTERFACE") :tty))
  (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)
  (setf *interactive* $interactive *gc-verbose* nil)
  (comf "target:tools/setup" :load t)
  (when (probe-file "${bootstrap}.lisp") (comf "$bootstrap"))
  (quit))
EOF

set sysinfo = ("code worldcom"\
	       "compiler comcom"\
	       "pcl pclcom"\
	       "clx clxcom"\
	       "hemlock hemcom"\
               "clm clmcom"\
	       "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 ..."
	sed -e 's/\\//g' << EOF | $lisp -noinit -eval '(eval (read))'
(progn
  (when (find-package "INTERFACE")
    (set (intern "*INTERFACE-STYLE*" "INTERFACE") :tty))
  (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* $interactive *gc-verbose* nil)
  (load "target:tools/$this_comfile")
  (quit))
EOF
   endif
end

echo "Done..."
