# make and install Rochester Connectionist Simulator
# Copyright (C) 1989 University of Rochester
#
#########################################################################
#									#
#	BEGIN site-specific macro definitions ...			#
#									#
#########################################################################

#####
# Root directory of distribution when compiling.
SRCDIR=/u/bukys/rcs_v4.2

#####
# Directory that will hold simulator installation.
# subdirectories will be created under here: bin, include, lib
SIMDIR=/u/bukys/rcs_installed

#####
# where to put the manual page.
MANDIR=/u/bukys/man/man1

#####
# where to put the user-accessible executable "makesim" (shell script).
BINDIR=/u/bukys/bin

#####
# C compiler to use for installation
# WARNING: READ THE "README" FILE REGARDING gcc ON SUN-4 machines.
##### DECstations:
#CC=cc -G 0
##### MIPS workstations:
#CC=/bsd43/bin/cc -G 0 -systype bsd43
##### iff you prefer the GNU C compiler
#CC=gcc -traditional -fwritable-strings
##### most machines
CC=cc

#####
# linker to use for installation
##### if you want to use gnu linker throughout (not recommended!):
#LD=$(SRCDIR)/lib/ld
##### MIPS workstations:
#LD=/bsd43/bin/ld
##### generally preferred:
LD=ld

#####
# linker to use in makesim
##### if you want to use gnu linker throughout (not recommended!):
#MLD=$(SIMDIR)/lib/ld
##### generally preferred:
MLD=$(LD)

#####
# command which extracts namelists from object files
##### MIPS workstations:
#NM=nm -B
##### generally preferred:
NM=nm

#####
# extra flags and libraries to add to final $CC loading step in makesim
##### DECstations:
#CCLDFLAGS=-N
#CCLDLIBS=-lc
##### MIPS workstations:
#CCLDFLAGS=-N
#CCLDLIBS=-lmld -lc
##### SUN/gcc: -g causes -Bstatic to be passed to final ld run.
#CCLDFLAGS=-g
#CCLDLIBS=
##### SUN/cc: -Bstatic allows RCS's dynamic loading to work
CCLDFLAGS=-Bstatic
CCLDLIBS=
##### most other machines:
#CCLDFLAGS=
#CCLDLIBS=

#####
# C compiler for simulator to use to compile user code for runtime compiling
# (it's up to you)
SIMCC=$(CC)

#####
# Linker for simulator to use to load in user code dynamically (at runtime).
##### Gnu ld, only seems to work perfectly on Vax.  Not recommended!
#SIMLD=$(SIMDIR)/lib/ld
##### preferred:
SIMLD=$(LD)

#####
# was the GNU linker used by any of the above configuration settings?
##### if not, then set LDCOMPILE=no:
LDCOMPILE=no
##### if it was used, then I'd better compile it:
#LDCOMPILE=yes
##### You may add -Dwhatever flags to LDCOMFLAGS as appropriate.
LDCOMFLAGS=

#####
# Default graphics (X11 or SUN or none)
DEFAULT_GRAPHICS=X11

#####
# X11 interface wanted; where it's installed; file to check for existence
WANT_X11=yes
##### at the University of Rochester...
XUSRLIBDIR=/usr/staff/lib
XINCROOT=/usr/staff/include
##### MIPS workstations:
#XUSRLIBDIR=/bsd43/usr/lib
#XINCROOT=/usr/include/bsd43
##### typically:
#XUSRLIBDIR=/usr/lib
#XINCROOT=/usr/include

WANT_X11_FILE=$(XINCROOT)/X11/X.h

#####
# suntools interface wanted; and a file which will exist only on suns
# (it's OK to leave this "yes" on non-Suns; it'll disable itself automatically)
WANT_SUN=yes
WANT_SUN_FILE=/usr/include/suntool/sunview.h

#####
# utilities
##### MIPS workstations:
#MAKE=/bsd43/bin/make
#RM=/bsd43/bin/rm -f
#ECHO=/bsd43/bin/echo
#CP=/bsd43/bin/cp
#INSTALL=/bsd43/bin/cp
#MKDIR=mkdir
##### typically:
MAKE=make
RM=/bin/rm -f
ECHO=/bin/echo
CP=cp
INSTALL=install -c
#MKDIR=mkdir
MKDIR=mkdir -p

#########################################################################
#									#
#	... END of site-specific macro definitions			#
#									#
#########################################################################

DEFNS=	"SIMDIR=$(SIMDIR)" "CC=$(CC)" "LD=$(LD)" "MAKE=$(MAKE)" \
	"DTYPE=$(DTYPE)" "SIMLD=$(SIMLD)" "SIMCC=$(SIMCC)" "BINDIR=$(BINDIR)" \
	"SRCDIR=$(SRCDIR)" "XUSRLIBDIR=$(XUSRLIBDIR)" "XINCROOT=$(XINCROOT)" \
	"INSTALL=$(INSTALL)" "NM=$(NM)"

all: show
	$(MAKE) makesim $(DEFNS)
	$(MAKE) manpage $(DEFNS)
	-sh -c 'if test "$(LDCOMPILE)" = "yes"; \
	then cd src/ld; $(MAKE) CFLAGS="$(LDCOMFLAGS)" SIMDIR="$(SIMDIR)" CC="$(CC)" linker; fi'
	cd src; $(MAKE) $(MFLAGS) $(DEFNS) library simtools bp simulator;
	-sh -c 'if test "$(WANT_SUN)" = "yes" -a -f $(WANT_SUN_FILE);\
	then cd src; $(MAKE) $(MFLAGS) $(DEFNS) sgraphics ; \
	fi'
	-sh -c 'if test "$(WANT_X11)" = "yes" -a -f $(WANT_X11_FILE);\
	then cd src; $(MAKE) $(MFLAGS) $(DEFNS) xgraphics ;\
	fi'
	cd example; $(MAKE) permission
	$(MAKE) tidy;

makesim:
	@$(ECHO) "s+RELEASE_DIR=xxx+RELEASE_DIR=$(SIMDIR)+" > sedscript.tmp
	@$(ECHO) "s+DEBUG_DIR=xxx+DEBUG_DIR=$(SRCDIR)+" >> sedscript.tmp
	@$(ECHO) "s+compile=xxx+compile='$(CC)'+" >> sedscript.tmp
	@$(ECHO) "s+ccldflags=xxx+ccldflags='$(CCLDFLAGS)'+" >> sedscript.tmp
	@$(ECHO) "s+ccldlibs=xxx+ccldlibs='$(CCLDLIBS)'+" >> sedscript.tmp
	@$(ECHO) "s+load=xxx+load='$(MLD)'+" >> sedscript.tmp
	@$(ECHO) "s+namelist=xxx+namelist='$(NM)'+" >> sedscript.tmp
	@$(ECHO) "s+xusrlibdir=xxx+xusrlibdir=$(XUSRLIBDIR)+" >> sedscript.tmp
	@$(ECHO) "s+xincroot=xxx+xincroot=$(XINCROOT)+" >> sedscript.tmp
	@-sh -c 'if test "$(WANT_SUN)" = "yes"; \
	then \
		if test -f $(WANT_SUN_FILE); \
		then \
		  if test "$(DEFAULT_GRAPHICS)" = "SUN"; \
		  then \
		    : "# sun graphics by default:" ; \
		    $(ECHO) "s+graphics=xxx+graphics=yes+" >> sedscript.tmp; \
		    $(ECHO) "s+swindows=xxx+swindows=yes+" >> sedscript.tmp; \
		    $(ECHO) "s+xwindows=xxx+xwindows=+" >> sedscript.tmp; \
		    $(ECHO) "s+gidebug=xxx+gidebug=gi+" >> sedscript.tmp; \
		  fi; \
		else \
		    $(ECHO) "WARNING: SUNTOOLS INTERFACE DISABLED (`basename $(WANT_SUN_FILE)` does not exist)"; \
		fi; \
	fi'
	@-sh -c 'if test "$(WANT_X11)" = "yes"; \
	then \
		if test -f $(WANT_X11_FILE); \
		then \
		  if test "$(DEFAULT_GRAPHICS)" = "X11"; \
		  then \
		    : "# x11 graphics by default:"; \
		    $(ECHO) "s+graphics=xxx+graphics=yes+" >> sedscript.tmp; \
		    $(ECHO) "s+swindows=xxx+swindows=+" >> sedscript.tmp; \
		    $(ECHO) "s+xwindows=xxx+xwindows=yes+" >> sedscript.tmp; \
		    $(ECHO) "s+gidebug=xxx+gidebug=xgi+" >> sedscript.tmp; \
		  fi; \
		else \
		    $(ECHO) "WARNING: X11 INTERFACE DISABLED (`basename $(WANT_X11_FILE)` does not exist)"; \
		fi; \
	fi'
	@$(ECHO) "s+graphics=xxx+graphics=+" >> sedscript.tmp
	@$(ECHO) "s+swindows=xxx+swindows=+" >> sedscript.tmp;
	@$(ECHO) "s+xwindows=xxx+xwindows=+" >> sedscript.tmp;
	@$(ECHO) "s+gidebug=xxx+gidebug=+" >> sedscript.tmp;
	sed -f sedscript.tmp < $(SRCDIR)/bin/makesim-dist > $(SRCDIR)/bin/makesim
	chmod a+rx $(SRCDIR)/bin/makesim
	$(RM) sedscript.tmp

manpage:
	@$(ECHO) "s+BINDIR+$(BINDIR)+g" > sedscript.tmp;
	@$(ECHO) "s+MANDIR+$(MANDIR)+g" >> sedscript.tmp;
	@$(ECHO) -n "s+MAKESIM.X+" >>sedscript.tmp;
	@$(ECHO) "s@/.*/man/man@makesim.@"  > sedscript2.tmp;
	@$(ECHO) "$(MANDIR)+g" | sed -f sedscript2.tmp  >> sedscript.tmp;
	@$(ECHO) "s+SIMDIR+$(SIMDIR)+g" >> sedscript.tmp;
	sed -f sedscript.tmp < $(SRCDIR)/man/man1/makesim.1-dist > $(SRCDIR)/man/man1/`$(ECHO) "$(MANDIR)" | sed -f sedscript2.tmp`
	$(RM) sedscript*.tmp

install:
	-$(MKDIR) $(SIMDIR);
	@-sh -c 'if test "$(SRCDIR)" = "$(SIMDIR)"; \
	then \
	  $(ECHO) "Already installed in $(SIMDIR)"; \
	else \
	  $(ECHO) "Installing in $(SIMDIR), please wait..."; \
	  $(ECHO) ""; \
	  set -e; \
	  $(CP) -r $(SRCDIR)/include $(SIMDIR); \
	  $(CP) -r $(SRCDIR)/bin $(SIMDIR); \
	  $(CP) -r $(SRCDIR)/lib $(SIMDIR); \
	  ranlib $(SIMDIR)/lib/*.a; \
	  $(RM) $(SIMDIR)/*/Makefile* $(SIMDIR)/*/*/Makefile* $(SIMDIR)/*/*/*/Makefile* $(SIMDIR)/bin/makesim-dist*; \
	  $(RM) $(SIMDIR)/*~ $(SIMDIR)/*/*~ $(SIMDIR)/*/*/*~; \
	fi'
	-$(MKDIR) $(BINDIR);
	@-sh -c 'if test "$(SIMDIR)/bin" != "$(BINDIR)"; \
	then \
	  $(ECHO) "Installing makesim in $(BINDIR)"; \
	  set -e; \
	  $(RM) $(BINDIR)/makesim; \
	  ln -s $(SIMDIR)/bin/makesim $(BINDIR)/makesim; \
	else \
	  $(ECHO) "Makesim installed in $(BINDIR)"; \
	fi'
	-$(MKDIR) $(MANDIR);
	@$(ECHO) "s@/.*/man/man@makesim.@"  > sedscript2.tmp;
	cp $(SRCDIR)/man/man1/`$(ECHO) "$(MANDIR)" | sed -f sedscript2.tmp` $(MANDIR)/`$(ECHO) "$(MANDIR)" | sed -f sedscript2.tmp`
	$(RM) sedscript*.tmp

remote-install:
	@sh -c 'if test "z$(REMOTE)" = "z"; then echo "No remote given"; exit 1; fi'
	-$(MKDIR) $(SIMDIR)";
	@$(ECHO) "Installing in $(SIMDIR) from $(REMOTE), please wait...";
	@$(ECHO) "";
	rcp -r $(REMOTE):$(SRCDIR)/include $(SIMDIR); \
	rcp -r $(REMOTE):$(SRCDIR)/bin $(SIMDIR); \
	rcp -r $(REMOTE):$(SRCDIR)/lib $(SIMDIR); \
	ranlib $(SIMDIR)/lib/*.a;
	@$(RM) $(SIMDIR)/*/Makefile* $(SIMDIR)/*/*/Makefile* $(SIMDIR)/*/*/*/Makefile* $(SIMDIR)/bin/makesim-dist*;
	@$(RM) $(SIMDIR)/*~ $(SIMDIR)/*/*~ $(SIMDIR)/*/*/*~
	-$(MKDIR) $(BINDIR);
	@$(ECHO) "Installing makesim in $(BINDIR)";
	$(RM) -f $(BINDIR)/makesim;
	ln -s $(SIMDIR)/bin/makesim $(BINDIR)/makesim;
	-$(MKDIR) $(MANDIR);
	@$(ECHO) "s@/.*/man/man@makesim.@"  > sedscript2.tmp;
	rcp $(REMOTE):$(SRCDIR)/man/man1/`$(ECHO) "$(MANDIR)" | sed -f sedscript2.tmp` $(MANDIR)/`$(ECHO) "$(MANDIR)" | sed -f sedscript2.tmp`
	$(RM) sedscript*.tmp

show:
	@$(ECHO) "The simulator is configured as follows:"
	@$(ECHO) ""
	@$(ECHO) "working directory = `pwd`"
	@$(ECHO) "source directory = ${SRCDIR}"
	@$(ECHO) "simulator installed in = ${SIMDIR}"
	@$(ECHO) "manual page directory = ${MANDIR}"
	@$(ECHO) "binary directory = ${BINDIR}"
	@$(ECHO) "installation compiler = ${CC}"
	@$(ECHO) "installation loader = ${LD}"
	@$(ECHO) "makesim loader = ${MLD}"
	@$(ECHO) "namelist command = ${NM}"
	@$(ECHO) "final loading extra flags = ${CCLDFLAGS}"
	@$(ECHO) "final loading extra libraries = ${CCLDLIBS}"
	@$(ECHO) "runtime compiler = ${SIMCC}"
	@$(ECHO) "runtime loader = ${SIMLD}"
	@$(ECHO) "compile gnu loader? ${LDCOMPILE}, flags ${LDCOMFLAGS}"
	@$(ECHO) "default graphics = ${DEFAULT_GRAPHICS}"
	@$(ECHO) "want X11 graphics? ${WANT_X11}"
	@$(ECHO) "  X11 libraries in = ${XUSRLIBDIR}"
	@$(ECHO) "  X11 include files in = ${XINCROOT}/X11"
	@$(ECHO) "want SunView graphics? ${WANT_SUN}"
	@$(ECHO) "Version:"
	@grep 'RCS_' include/version.h

xdebug:
	@$(ECHO) "Making debug X11 interface with the following settings:"
	$(MAKE) show $(DEFNS)
	cd src; \
	$(MAKE) $(MFLAGS) $(DEFNS) xdebug

sdebug:
	@$(ECHO) "Making debug Suntools interface with the following settings:"
	$(MAKE) show $(DEFNS)
	cd src; \
	$(MAKE) $(MFLAGS) $(DEFNS) sdebug;

simdebug:
	@$(ECHO) "Making debug simulator with the following settings:"
	$(MAKE) show $(DEFNS)
	cd src; \
	$(MAKE) $(MFLAGS) $(DEFNS) simdebug

clean:
	cd src; $(MAKE) clean
	cd bin; $(MAKE) clean
	cd lib; $(MAKE) clean
	cd example; $(MAKE) clean
	cd test;  $(MAKE) clean
	$(RM) \#* *~ sedscript*.tmp

tidy:
	cd src; $(MAKE) tidy
	cd bin; $(MAKE) tidy
	cd lib; $(MAKE) tidy
	cd example; $(MAKE) tidy
	cd test;  $(MAKE) tidy
	$(RM) \#* *~
