#! /bin/csh -f
# 
# Build the PCE object files for various architectures.
# 
# Copyright (c) 1991 University of Amsterdam
# All rights reserved.
# 
# Normal commands
# 
#	./make			# creates PCE object files
#	./make XPCE.a		# creates a single file XPCE.a using `ar -q'
#	./make tags		# makes emacs TAG file (needs etags)
#	./make clean		# clean up

set FLAGS=""
set fake=0
set optimise=""
while ( $1 =~ -* )
    switch( $1 )
	case -n:
	    set fake=1
	    shift
	    break
	case -g:
	    set optimise=($optimise "-g")
	    shift
	    break
	case -pg:
	    set optimise=($optimise "-O -finline-functions -pg")
	    shift
	    break
	default
            set FLAGS=($FLAGS $1)
	    shift
    endsw
end
set TARGETS=($* all files)
if ( $optimise != "" ) setenv OPTIMISE "$optimise"

# Find out about context

setenv MACHINE `../machine`

foreach dir ($path)
    if ( -x $dir/etags ) set has_etags
    if ( -x $dir/gcc )   set has_gcc
end

if ( ! -e TAGS && $?has_etags ) then
   set TARGETS=($TARGETS tags)
endif

# Determine type of machine

if ( ! $?PCEXBASE ) setenv PCEXBASE /usr

switch ($TARGETS[1])
    case clean:
	echo -n "Removing ../$MACHINE ... "
	if ( ! $fake ) rm -rf ../$MACHINE
	echo -n "EMACS backup files ... "
	if ( ! $fake ) rm -rf *~
	echo -n "Others ... "
	if ( ! $fake ) rm -f TAGS names.* pce-name.i[ch] files
	echo "done."
	exit 0
endsw

if ( ! -d ../$MACHINE ) then
    set names=(names.all names.found names.new names.prolog names.sorted \
	       names.rebuild pce-name.ic pce-name.ih Names.X.c Names.Gen.c )

    set ids = (`echo md-*.h | sed 's/md-\([^ ]*\).h/\1/g'`)
    foreach id ( $ids )
        if ( $MACHINE =~ $id* ) set ID=$id
    end

    if ( ! $?ID ) then
	echo "Don't know how to make PCE for $MACHINE"
	exit 1
    endif

    echo -n "Creating object directory ../$MACHINE ... "
    mkdir ../$MACHINE
    ( cd ../$MACHINE; ln -s ../src/???-*.[Cchy] . )
    ( cd ../$MACHINE; ln -s ../src/Makefile-$ID Makefile )
    ( cd ../$MACHINE; ln -s ../src/md-$ID.h md.h )
    ( cd ../$MACHINE; ln -s ../src/{pce,syspce}.h . )
    ( cd ../$MACHINE; ln -s ../src/FN . )
    ( cd ../$MACHINE; ln -s ../src/bitmaps . )
    ( cd ../$MACHINE; ln -s ../src/files . )
    foreach name ($names)
	( cd ../$MACHINE; ln -s ../src/$name $name )
    end
    ( cd ../$MACHINE; echo '#define MACHINE "'$MACHINE'"' > pce-arch.h )
    echo "done."
endif

# Change to installation directory

echo cd ../$MACHINE
cd ../$MACHINE

# Detemine the X-version used

setenv XINCLUDES $PCEXBASE/include/X11
if ( ! -f $XINCLUDES/IntrinsicP.h ) then
    echo "[WARNING: Cannot find X header files. Check PCEXBASE]"
    exit 1
endif

egrep '^#[ 	]*define[ 	]+XT_(VERSION|REVISION)' \
      $XINCLUDES/IntrinsicP.h > .xwn-version.h

if ( ! -f xwn-version.h ) then
    mv .xwn-version.h xwn-version.h
else if ( ! { cmp -s .xwn-version.h xwn-version.h } ) then
    echo "WARNING: Different X revision; updating xwn-version.h"
    mv .xwn-version.h xwn-version.h
endif

if ( $XINCLUDES != /usr/include/X11 ) then
    setenv IFLAGS -I`dirname $XINCLUDES`
else
    setenv IFLAGS ""
endif

set makeargs = ""
if ( $?MAKEJOBS ) then
    set makeargs = -j$MAKEJOBS
endif

if ( ! -e ../src/names.all ) then
   echo make names
   make names
endif

   
set makeargs = ($makeargs -e $FLAGS $TARGETS)

echo make $makeargs

exec make $makeargs
