#	A guide to setting machine dependent variables:
# SYSLIB  The system libraries that S uses.
#	Most systems will need the Fortran library(ies) and math library -lm
# UNDEF	Some systems don't use an initial underscore in mapping C function
#	names to loader names.  The value of UNDEF will have to be
#	edited accordingly.
# Compilation options:
#	The compilation options are set by setting CFLAGS.
#	Here is a description:
#	-DVAX	for a vax
# X11  If you have X11 installed on your system, this should point to some
#	file of x11 stuff.  The readability of that file is used to make
#	or not make the X11 device function
# X11LIB Should be the list of libraries that need to be searched to
#	load the X11 device

H = $$SHOME
DEVICE_FILES = Spost.o Shpgl.o Stty5620.o Shp2623.o Stek4014.o Ssun.o Sprinter.o \
	Stek4105.o Spic.o Sx11.o
MAIN = c_support.o comp_util.o debug.o deparse.o device.o do_x.o \
	eval.o fft.o get_data.o graphics.o lang.o lex.o pars_util.o \
	pratom.o put_data.o random.o str_utils.o subset.o sys_funs.o \
	window.o malloc.o $(DEVICE_FILES)
SCODE = main.o S_ld_init.o interface.o ftn_utils.o linear.o stable.o S_utils.o
SLIBS = $$L/Sqpe.a $$L/graph.a $$L/lang.a $$L/grz.a $$L/psl.a
SYSLIB = /usr/lib/libF77.a /usr/lib/libI77.a -lm
UNDEF = -u _ld_init -u _Sfun
X11 = /usr/lib/libX11.a
X11LIB = -lX11

# IF defined as -if for Berkeley where if returns funny status
IF = -if
# If you don't want .f,.c files left from .r, .y, .l makes, define:
#RM = -/bin/rm
RM = @echo >/dev/null
M4 = /usr/bin/m4
F77 = f77
CC = cc
LD = ld
RATFOR = ratfor
EFL = efl
DCC = /usr/jerq/bin/3cc

# For VAX architecture, define:
# CFLAGS = -g $(CCFLAGS) -DVAX
# For UTS architecture, define:
#CFLAGS = $(CCFLAGS) -DUTS
# For other architectures, define:
CFLAGS = $(CCFLAGS)
FFLAGS =
LDFLAGS = -x
LDRFLAGS =

.SUFFIXES:
.SUFFIXES: .o .n .r .f .e .c .l .y
.f.o:
	$(F77) $(FFLAGS) -c $*.f
.r.o:
	$(RATFOR) "-6&" $*.r >.$*.f
	$(F77) $(FFLAGS) -c .$*.f
	mv .$*.o $*.o
	$(RM) .$*.f
.c.o:
	$(CC) -c $(CFLAGS) $*.c
.y.o:
	yacc -d $*.y
	mv y.tab.c $*.c
	$(CC) -c $(CFLAGS) $*.c
	$(RM) $*.c
.l.o:
	lex $*.l
	mv lex.yy.c $*.c
	$(CC) -c $(CFLAGS) $*.c
	$(RM) $*.c
.c.n:
	$(DCC) -c $*.c
	mv $*.o $*.n

default: S

all: restart S functions audit reverse

restart:
	-rm -f $$C/qpe main.a $$L/S_makefile
	cp S_makefile $$L
S: main.a S.o
	date | sed 's/.*/char load_time[] ="S &";/' > S_load_time.c
	$(CC) -o nnSqpe $(LDFLAGS) S_load_time.c $(UNDEF) \
		$$L/S.o $$L/main.a $(SLIBS) $(SYSLIB)
	mv nnSqpe $$C/nSqpe
	touch S
nofortran: main.a main.o no_S.o ld_init.o
	date | sed 's/.*/char load_time[] ="No_F &";/' > load_time.c
	$(CC) -o qpe $(LDFLAGS) load_time.c ld_init.o main.o no_S.o \
		$$L/main.a -lm
main.a: $(MAIN) runit
	$$A/LIBRARY $$L/main.a $(MAIN)
	touch main.a
S.o: $(SCODE)
	$$A/LIBRARY $$L/S.o $(SCODE)
	touch S.o
y.tab.h: lang.o
window.o: window.c
	$(IF) test -d /usr/jerq/include; then $(CC) -c -DJERQ $(CFLAGS) window.c; \
		else $(CC) -c $(CFLAGS) window.c; fi
Ssun.o:	ok_sun.c
	$(IF) test -r /bin/sun && /bin/sun; then make Sun; \
		else make no_sun.o; mv no_sun.o Ssun.o; fi
Sun: ok_sun.o
	-$(LD) -r -o Ssun.o $(LDRFLAGS) ok_sun.o -lcore -lsunwindow -lpixrect
Sx11.o : ok_x11.c
	$(IF) test -r $(X11); then make x11; \
		else make no_x11.o ; mv no_x11.o Sx11.o; fi
x11 : ok_x11.o
	-$(LD) -r -o Sx11.o $(LDRFLAGS) ok_x11.o $(X11LIB)
S_utils.o: S_utils.r	# special rules for S routines
	$$C/MAKE S_utils.o
audit: audit.o
	$(CC) -o $$C/AUDIT $(LDFLAGS) audit.o
reverse: reverse.o
	$(CC) -o $$C/reverse $(LDFLAGS) reverse.o
runit: runit.o
	$(CC) $(LDFLAGS) -o $$C/runit runit.o
	touch runit

functions:
	-mv $H/cmd/nSqpe $H/cmd/Sqpe
	mv $H/s/.Functions $H/s/.Data
	-mkdir $H/s/.Functions
	chmod 775 $H/s/.Data
	./functions.sh $H/s/.Data $H/data/S || \
		(rm -fr $H/s/.Functions; mv $H/s/.Data $H/s/.Functions; exit 1)
	rmdir $H/s/.Functions
	mv $H/s/.Data $H/s/.Functions
	mv $H/s/.Datasets $H/s/.Data
	-mkdir $H/s/.Datasets
	./functions.sh $H/s/.Data $H/data/machine.so || \
		(rm -fr $H/s/.Datasets; mv $H/s/.Data $H/s/.Datasets; exit 1)
	(cd $H/s; echo "data.restore('$H/data/DATA.data.dump')" | $H/cmd/Sqpe) || \
		(rm -fr $H/s/.Datasets; mv $H/s/.Data $H/s/.Datasets; exit 1)
	rmdir $H/s/.Datasets
	mv $H/s/.Data $H/s/.Datasets
library:
	./functions.sh $H/library/examples/.Data $H/data/EXAMPLES
	cd $H/library/examples; make
	./functions.sh $H/library/semantics/.Data $H/data/SEMANTICS

tar:
	tar cf source.tar `cat SOURCE_FILES`
	sh tar.funs.sh
cpio:
	cat SOURCE_FILES | cpio -oc > source.cpio
	h=`pwd`; cd ../functions; find . -cpio $$h/funs.cpio
clean: 
	rm -f *.o y.tab.h main.a .linear.f .stable.f S runit \
		lex.c lang.c S_load_time.c load_time.c
machine.h:
	cc -DDP machar.c
	a.out | tr '[a-z]' '[A-Z]'| sed -e 1d -e s/=// -e 's/^/#define /' -e 's/.$$//' -e 's/\([A-Z]\)\./\1_/g' >machine.h
	(echo ".Machine <-"; a.out) | sed -e 's/)/,/' >$H/data/machine.so
	cc -DSP machar.c
	a.out | tr '[a-z]' '[A-Z]'| sed -e 1d -e s/=// -e 's/^/#define /' -e 's/.$$//' -e 's/\([A-Z]\)\./\1_/g' >>machine.h
	a.out | sed -e 1d -e 's/)/,/' >>$H/data/machine.so
	cc machint.c
	a.out | tr '[a-z]' '[A-Z]'| sed -e 1d -e s/=// -e 's/^/#define /' -e 's/.$$//' -e 's/\([A-Z]\)\./\1_/g' >>machine.h
	a.out | sed -e 1d >>$H/data/machine.so
	rm a.out

S.h: machine.h system.h

# Dependencies on .h files: must be updated explicitly when they change

Shp2623.o: S.h device.h
Shpgl.o: S.h device.h
Spic.o: S.h device.h
Spost.o: S.h device.h
Sprinter.o: S.h device.h
Stek4014.o: S.h device.h
Stek4105.o: S.h device.h
Stty5620.o: S.h device.h
audit.o: S.h audit.h
c_support.o: S.h eval.h options.h y.tab.h
comp_util.o: S.h eval.h infix.h options.h sys_codes.h y.tab.h
debug.o: S.h eval.h options.h y.tab.h
deparse.o: S.h eval.h options.h y.tab.h
device.o: S.h device.h
do_x.o: S.h
eval.o: S.h eval.h options.h sys_codes.h y.tab.h
fft.o: S.h
ftn_utils.o: S.h
get_data.o: S.h eval.h options.h y.tab.h
graphics.o: S.h device.h eval.h graphics.h options.h y.tab.h
interface.o: S.h eval.h interface.h options.h y.tab.h
lang.o: S.h
ld_init.o: S.h
lex.o: S.h options.h y.tab.h
main.o: S.h eval.h options.h y.tab.h
malloc.o: S.h
no_S.o: S.h interface.h
no_sun.o: S.h
no_x11.o: S.h
ok_sun.o: S.h device.h
ok_x11.o: S.h device.h disp_list.h x11texture.h
pars_util.o: S.h options.h y.tab.h
pratom.o: S.h options.h y.tab.h
put_data.o: S.h options.h y.tab.h
random.o: S.h dist.h y.tab.h
reverse.o: S.h
runit.o: S.h
skeleton.o: S.h device.h
skeleton2.o: S.h device.h
str_utils.o: S.h eval.h options.h y.tab.h
subset.o: S.h eval.h options.h y.tab.h
sys_funs.o: S.h device.h dist.h eval.h infix.h options.h sys_codes.h y.tab.h
window.o: S.h options.h
