# file: Makefile		G. Moody		20 February 2004
#
# Top-level 'make' description file for the cardiovascular simulator (cvsim)
#
# To compile and install cvsim, check the values for BINDIR, CVLIB, TMPDIR,
# XLIBDIR, and CFLAGS (below) and change them if necessary, then type 'make'.
# Using the default values for BINDIR and CVLIB, you will need root permissions
# in order to do this successfully.

# The shell scripts 'cvsim' and 'xdpr', and the compiled 'cvsbin', will be
# installed in BINDIR.
BINDIR=/usr/local/bin

# Miscellaneous files needed by cvsim at run time will be installed in CVLIB.
CVLIB=/usr/local/lib/cvsim

# Set TMPDIR to the name of a directory in which temporary files can be
# written at run time by any user of cvsim.
TMPDIR=/tmp

# Set XLIBDIR to the name of the directory that contains the X11 libraries
# (Xaw, Xt, Xmu, Xext, and X11) needed by cvsim.
XLIBDIR=/usr/X11R6/lib

# Uncomment one of the next two lines.
# CFLAGS="-O -DCVLIB=$(CVLIB) -L$(XLIBDIR)"
CFLAGS="-O -DCVLIB=$(CVLIB) -DNOCRYPT -L$(XLIBDIR)"
# Use the second definition (recommended) to disable encryption and decryption
# of the case study files.  The code for encrypting and decrypting has not been
# checked recently.

# 'make' or 'make install': compile the sources and install cvsim in BINDIR.
install: cvsbin cvsim.sh
	test -d $(BINDIR) || ( mkdir -p $(BINDIR); chmod a+x $(BINDIR) )
	test -d $(CVLIB)  || ( mkdir -p $(CVLIB);  chmod a+x $(CVLIB)  )
	test -d $(TMPDIR) || ( mkdir -p $(TMPDIR); chmod a+x $(TMPDIR) )
	rm -f $(BINDIR)/cvsim $(BINDIR)/cvsbin $(BINDIR)/xdpr
	cp -p cvsbin $(BINDIR)
	cp -p cvsim.sh $(BINDIR)/cvsim
	cp -p xdpr $(BINDIR)
	chmod a+x $(BINDIR)/cvsim $(BINDIR)/cvsbin $(BINDIR)/xdpr
	cp -pr cvlib/* $(CVLIB)
	make clean

# 'make uninstall': remove (almost) everything installed by 'make install'.
uninstall:
	rm -f $(BINDIR)/cvsim $(BINDIR)/cvsbin $(CVLIB)
	@echo $(BINDIR)/xdpr, $(BINDIR), and $(TMPDIR) not removed.

tarballs:	bin-tarball src-tarball

bin-tarball:	install
	cd /; tar cfvz $(TMPDIR)/cvsim-bin.tar.gz \
	 $(BINDIR)/cvsim $(BINDIR)/cvsbin $(BINDIR)/xdpr $(CVLIB)
	mv $(TMPDIR)/cvsim-bin.tar.gz ..
	@( cd ..; echo "Binary tarball is `pwd`/cvsim-bin.tar.gz" )

src-tarball:	clean
	cd ..; tar cfvz cvsim-src.tar.gz cvsim-src
	@( cd ..; echo "Source tarball is `pwd`/cvsim-src.tar.gz" )

# 'make cvsbin': compile the executable binary of cvsim.
cvsbin:
	cd data;    $(MAKE) CFLAGS=$(CFLAGS)
	cd sim;     $(MAKE) CFLAGS=$(CFLAGS)
	cd event;   $(MAKE) CFLAGS=$(CFLAGS)
	cd circuit; $(MAKE) CFLAGS=$(CFLAGS)
	cd plot;    $(MAKE) CFLAGS=$(CFLAGS)
	cd nl;      $(MAKE) CFLAGS=$(CFLAGS)
	cd form;    $(MAKE) CFLAGS=$(CFLAGS)
	cd main;    $(MAKE) CFLAGS=$(CFLAGS)
	mv main/cvsbin .

# 'make cvsim.sh': create the 'cvsim' shell script.
cvsim.sh:
	sed s+XBINDIR+$(BINDIR)+g <cvsim.template | \
	 sed s+XCVLIB+$(CVLIB)+g | \
	 sed s+XTMPDIR+$(TMPDIR)+g >cvsim.sh

# 'make clean': remove temporary files.
clean:
	rm -f */*.o sim/tblc sim/tbldcdt */*.out cvsbin cvsim.sh */*~ *~
