####################################################################
# Configuration section

# PWD = complete pathname of the dir containing this makefile.  If you
# are using csh this should be set automatically by the shell.
# Otherwise, remove the comment char from the next line and append the
# pathname of this dir.
# PWD = 

# CP = copying command to use when installing files ('cp' or 'ln')
CP=ln

# BINDIR = directory in which you want to install the executable files
BINDIR=$(PWD)/bin

# OPTIONS = Compiler options
OPTIONS=-O -pipe

# AUXDIR = complete pathname of directory in which you want to install
# the files that Heisenberg needs at run-time.  Do not move or delete
# these files; if you do, Heisenberg will not work any more.  Do not
# comment out the def of AUXDIR; if you do, Heisenberg will not work
# then either.
AUXDIR = $(PWD)/lib

# BSDPREFIX = prefix for "BSD style" header files sys/types.h,
# sys/socket.h, netinet/in.h, netdb.h, and strings.h.  On Suns and other
# Berkeley Unix systems, leave the def of BSDPREFIX commented out.  On
# IRISes it should be
#   BSDPREFIX = bsd/
# (the last thing on the line should be the '/' --- no trailing blanks!)
# BSDPREFIX =

# BSDLIBS = any library flags which are necessary to load the BSD
# libraries.  On Suns and other Berkeley Unix systems, leave the def
# of BSDLIBS commented out.  On IRISes it should be
#   BSDLIBS = -lbsd
# BSDLIBS =

# SPOOL_COMMAND = command to use for printing PostScript files.  If
# you can't print PostScript files, comment out this def.
SPOOL_COMMAND=lpr

# DEV = the kind of graphics device to compile for; should be
#    DEV = SUNVIEW
# for Suns, or
#    DEV = IRIS4
# for IRISes.
DEV = SUNVIEW

# End of Configuration section
####################################################################

HEISENBERGPATH = $(BINDIR)/heisenberg

ELBOWDPATH = $(BINDIR)/elbowd

INSTALL_BIN = $(PWD)/instl

INSTALL = $(INSTALL_BIN) -cp "$(CP)"

SHELL = /bin/sh

COMMON_MAKE_FLAGS = \
  OPTIONS="$(OPTIONS)" \
  CP="$(CP)" \
  INSTALL_BIN="$(INSTALL_BIN)"

GLG_MAKE_FLAGS = \
  $(COMMON_MAKE_FLAGS)

HEISENBERG_MAKE_FLAGS = \
  $(COMMON_MAKE_FLAGS) \
  HELPDIR="$(AUXDIR)" \
  BINDIR="$(BINDIR)" \
  FSA_OBJ="$(PWD)/fsa/fsa.o" \
  DEVICE="-D$(DEVICE)" \
  GR_DIR="$(PWD)/gr" \
  LGD_DIR="$(PWD)/lgd"

SUNVIEW_EXE_LIBS =	$(PWD)/lgd/liblgd.a $(PWD)/gr/libgr3d.a \
			-lm -lsuntool -lsunwindow -lpixrect

IRIS4_EXE_LIBS = $(PWD)/lgd/liblgd.a -lm -lgl_s

REMOTEPIPE_MAKE_FLAGS = \
  $(COMMON_MAKE_FLAGS) \
  SYS_TYPES_H="<$(BSDPREFIX)sys/types.h>" \
  SYS_SOCKET_H="<$(BSDPREFIX)sys/socket.h>" \
  NETINET_IN_H="<$(BSDPREFIX)netinet/in.h>" \
  NETDB_H="<$(BSDPREFIX)netdb.h>" \
  STRINGS_H="<$(BSDPREFIX)strings.h>" \
  LIBS="$(BSDLIBS)" \
  BOTHPIPEDIR="$(PWD)/bothpipe" \
  BINDIR="$(BINDIR)"

FSA_MAKE_FLAGS = \
  $(COMMON_MAKE_FLAGS)

GR_MAKE_FLAGS = \
  $(COMMON_MAKE_FLAGS) \
  DEFAULT_SPOOL_COMMAND="$(SPOOL_COMMAND)" \
  AUXDIR="$(AUXDIR)"

LGD_MAKE_FLAGS = \
  $(COMMON_MAKE_FLAGS) \
  DEV="$(DEV)" \
  AUXDIR="$(AUXDIR)" \
  GR_HEADER="$(PWD)/gr/gr.h" \
  GLGHEADER="$(PWD)/glg/glg.h" \
  GLGLIB="$(PWD)/glg/libglg.a" \
  DEFAULTSDIR=""

all:	compile

compile:
	@if test "$(DEV)" = "SUNVIEW" ; then \
	    $(MAKE) -e\
	      DEVICE=DEVICE_SUNVIEW \
	      DEVDIR=sunview \
	      HEISENBERG_EXE_LIBS="$(SUNVIEW_EXE_LIBS)" \
	      compile_sun ; \
	  elif test "$(DEV)" = "IRIS4" ; then \
	    $(MAKE) -e \
	      DEVICE=DEVICE_IRIS4 \
	      DEVDIR=iris4 \
	      HEISENBERG_EXE_LIBS="$(IRIS4_EXE_LIBS)" \
	      compile_iris ; \
	  fi

install:	install_quietly
	@echo ''
	@echo 'Installation of the graphics part of Heisenberg is now'
	@echo 'complete. The executable files for the graphics host'
	@echo 'are in the directory'
	@echo '     $(BINDIR)'
	@echo ''

install_quietly:	$(AUXDIR) $(BINDIR)
	@if test "$(DEV)" = "SUNVIEW" ; then \
	    $(MAKE) -e\
	      DEVICE=DEVICE_SUNVIEW \
	      DEVDIR=sunview \
	      HEISENBERG_EXE_LIBS="$(SUNVIEW_EXE_LIBS)" \
	      install_sun ; \
	  elif test "$(DEV)" = "IRIS4" ; then \
	    $(MAKE) -e \
	      DEVICE=DEVICE_IRIS4 \
	      DEVDIR=iris4 \
	      HEISENBERG_EXE_LIBS="$(IRIS4_EXE_LIBS)" \
	      install_iris ; \
	  fi

startheis:	rm-startheis startheis.tpl
	sed \
	  -e 's|HEISENBERGPATH|$(HEISENBERGPATH)|' \
	  -e 's|ELBOWDPATH|$(ELBOWDPATH)|' \
	  -e 's|#TPLWARNING|# DO NOT EDIT THIS FILE DIRECTLY!  EDIT ONLY THE CORRESPONDING .tpl FILE.|' \
	  -e '/#TPL.*$$/d' \
	  < startheis.tpl > startheis
	chmod -w,+x startheis

rm-startheis:
	/bin/rm -f startheis ;

install_startheis:	startheis
	$(INSTALL) '$(BINDIR)' startheis

$(AUXDIR):
	mkdir $(AUXDIR)

$(BINDIR):
	mkdir $(BINDIR)

heisgraphics.tar.Z:	heisgraphics.tar
	compress heisgraphics.tar

heisgraphics.tar:	rm-heisgraphics.tar
	tar chbf 512 /tmp/heisgraphics.tar *
	/bin/mv /tmp/heisgraphics.tar .

rm-heisgraphics.tar:
	/bin/rm -f heisgraphics.tar ;

localclean:
	/bin/rm -f *~

clean:
	@if test "$(DEV)" = "SUNVIEW" ; then \
	  $(MAKE) -e DEVDIR=sunview clean2 ; \
	elif test "$(DEV)" = "IRIS4" ; then \
	  $(MAKE) -e DEVDIR=iris4 clean2 ; \
	fi

veryclean:
	@if test "$(DEV)" = "SUNVIEW" ; then \
	  $(MAKE) -e DEVDIR=sunview veryclean2 ; \
	elif test "$(DEV)" = "IRIS4" ; then \
	  $(MAKE) -e DEVDIR=iris4 veryclean2 ; \
	fi

distclean:
	@if test "$(DEV)" = "SUNVIEW" ; then \
	  $(MAKE) -e DEVDIR=sunview distclean2 ; \
	elif test "$(DEV)" = "IRIS4" ; then \
	  $(MAKE) -e DEVDIR=iris4 distclean2 ; \
	fi

###
### WARNING: Do not explicity make any of the targets below here
### without specifying values for the macros
###
###  	DEVICE
###	DEVDIR
###	HEISENBERG_EXE_LIBS
###
###  These macros are not set in the configuration section, but rather
###  are set by the above @if commands depending on the value of the
###  DEV macro, which is set in the configuration section.
###

compile_sun:	compile_gr compile_lgd compile_fsa compile_heisenberg \
		compile_remotepipe startheis
  
install_sun:	install_gr install_lgd install_fsa install_heisenberg \
		install_remotepipe install_startheis
  
compile_iris:	compile_glg compile_lgd compile_fsa compile_heisenberg \
		compile_remotepipe startheis
  
install_iris:	install_glg install_lgd install_fsa install_heisenberg \
		install_remotepipe install_startheis
  
compile_fsa:
	cd fsa ; \
	  $(MAKE) \
	    PWD=$(PWD)/fsa \
	    $(FSA_MAKE_FLAGS)

compile_glg:
	cd glg ; \
	  $(MAKE) \
	    PWD=$(PWD)/glg \
	    $(GLG_MAKE_FLAGS)

compile_gr:
	cd gr ; \
	  $(MAKE) \
	    PWD=$(PWD)/gr \
	    $(GR_MAKE_FLAGS) \
	    libgr3d.a

compile_heisenberg:
	cd heisenberg ; \
	  $(MAKE) \
	    PWD=$(PWD)/heisenberg \
	    LIBS="$(HEISENBERG_EXE_LIBS)" \
	    $(HEISENBERG_MAKE_FLAGS)

compile_lgd:
	cd lgd ; \
	  $(MAKE) \
	    PWD=$(PWD)/lgd \
	    DEVDIR="$(DEVDIR)" \
	    DEVICE="$(DEVICE)" \
	    $(LGD_MAKE_FLAGS) \
	    liblgd.a

compile_remotepipe:
	cd remotepipe ; \
	  $(MAKE) \
	    PWD=$(PWD)/remotepipe \
	    $(REMOTEPIPE_MAKE_FLAGS)

install_fsa:	compile_fsa

install_glg:	compile_glg

install_gr:
	cd gr ; \
	  $(MAKE) \
	    PWD=$(PWD)/gr \
	    $(GR_MAKE_FLAGS) \
	    libgr3d.a install_aux

install_heisenberg:
	cd heisenberg ; \
	  $(MAKE) \
	    PWD=$(PWD)/heisenberg \
	    LIBS="$(HEISENBERG_EXE_LIBS)" \
	    $(HEISENBERG_MAKE_FLAGS) \
	    install

install_lgd:
	cd lgd ; \
	    $(MAKE) \
	      PWD=$(PWD)/lgd \
	      DEVDIR="$(DEVDIR)" \
	      DEVICE="$(DEVICE)" \
	      $(LGD_MAKE_FLAGS) \
	      liblgd.a install_dev

install_remotepipe:
	cd remotepipe ; \
	  $(MAKE) \
	    PWD=$(PWD)/remotepipe \
	    $(REMOTEPIPE_MAKE_FLAGS) \
	    install

clean2:	localclean
	cd fsa ; $(MAKE) clean
	cd glg ; $(MAKE) clean
	cd gr ; $(MAKE) clean
	cd heisenberg ; $(MAKE) clean
	cd remotepipe ; $(MAKE) clean
	cd lgd ; $(MAKE) DEVDIR="$(DEVDIR)" clean

veryclean2:	localclean
	/bin/rm -f startheis
	cd fsa ; $(MAKE) veryclean
	cd glg ; $(MAKE) veryclean
	cd gr ; $(MAKE) veryclean
	cd heisenberg ; $(MAKE) veryclean
	cd remotepipe ; $(MAKE) veryclean
	cd lgd ; $(MAKE) DEVDIR="$(DEVDIR)" veryclean

distclean2:	localclean
	/bin/rm -r -f ./bin ./lib
	cd fsa ; $(MAKE) distclean
	cd glg ; $(MAKE) distclean
	cd gr ; $(MAKE) distclean
	cd heisenberg ; $(MAKE) distclean
	cd remotepipe ; $(MAKE) distclean
	cd lgd ; $(MAKE) DEVDIR="$(DEVDIR)" distclean
