############################################
# YARM makefile
############################################

#Uncomment to use pgcc instead of gcc
#CC       = pgcc
CC         = gcc
OPTS       = -O6 -ffast-math
CFLAGS     = -ansi -Wall -Wno-unused
# CFLAGS     = -ansi -Wall 
DEST       = /usr/local

#######################################
# Linux install - f2c compiled lapack
#######################################
# LAPACK_LIBDIR = /usr/lib
# MATHLIBS      =  -lblas $(LAPACK_LIBDIR)/liblapack.a $(LAPACK_LIBDIR)/libf2c.a -lm
#MATHLIBS      =  -lblas -llapack -lf2c -lm

#################################################
# Linux install - PGI compiled lapack
#################################################
#
# Some futzing required for pgi build with pgcc.  Basically, gnu-based
# dynamic libraries make pgcc sick, so try using an exception switch in
# the compilation, unless you have static libraries and then just include 
# the whole damn thing.  The exception switch is "-u MAIN_"  It's a hack
# and I don't care.
#Path to pgi
# PGI = /home/collective/george/pgi
#PGILIB       = /usr/local/lib/pgi
#PII          = /usr/local/lib
# How to use math libraries built solely under pgi and compile with pgcc
#MATHLIBS = $(PII)/lapack-opt.a $(PII)/blas-opt.a \
#	$(PII)/blas-f2c.a -lpgftnrtl -lpgc -lm 
#
# How to use optimized math libraries built solely under pgi and compile 
# with gcc
#MATHLIBS = $(PII)/lapack-opt.a $(PII)/blas-opt.a \
#	$(PII)/blas-f2c.a $(PGILIB)/libpgftnrtl.a $(PGILIB)/libpgc.a -lm 
#
#
# How to use math libraries built under pgi with local optimization
#MATHLIBS = $(PII)/lapack_pII_pgf77_020499.a $(PII)/blas_pII_pgf77_020499.a \
#	$(PGILIB)/libpgftnrtl.a $(PGILIB)/libpgc.a -lm 
#
# How to use math libraries built with gcc and with pgi - not everything works here
# it is impossible to avoid using some of the pgi libraries.  Everything 
# except Hydro will build with this method.
#MATHLIBS = -llapack $(PII)/blas-opt.a $(PII)/blas-f2c.a -lf2c -lm 
#
# How to use math libraries built with gcc and with pgi - use this 
# if somehow you figured out how to separate pgi from its libraries 
# without being separated simultaneously from your $500.
#MATHLIBS = -llapack $(PII)/blas-opt.a $(PII)/blas-f2c.a -lf2c.a -lm \
#	$(PGILIB)/libpgftnrtl.a $(PGILIB)/libpgc.a
#
# pgi optimizations - these could be a lot better
#OPTS  = -O2 -Munroll -Mvect=cachesize:524288 -pc 64 -Kieee -tp p6
# gcc optimizations
# OPTS  = -O6 -ffast-math

#################################################
# SGI install
#################################################
#
# use "-lcomplib.sgimath" and "-lftn" to access the built-in BLAS 
# and LAPACK routines on IRIX v5.1 and higher
#
# MATHLIBS   = -lcomplib.sgimath -lftn -lm
# OPTS       = -O2 -mcpu=r4000 -mips2 -ffast-math
# OPTS       = -O2 -mips2

#################################################
# Do not edit below
#################################################
SHELL    = /bin/sh
VERSION  = 0.33b
MV       = /bin/mv
CP       = /bin/cp
RM       = /bin/rm
TAR      = tar -cvf
COMPRESS = gzip

# Various directories needed
INSTDIR  = $(DEST)/$(YARMDIR)
YARMDIR  = yarm_v$(VERSION)
BINDIR   = bin/
PERLDIR  = perl/
DATADIR  = data/
LIBDIR   = lib/
HTMLDIR  = html/

# Use the .a for statically linked executables
# Use the .so for dynamically linked executables
LIBTYPE  = static
# LIBTYPE  = dynamic

libYARM:
	( cd src_libYARM; make CC="$(CC)" OPTS="$(OPTS)" CFLAGS="$(CFLAGS)" \
		$(LIBTYPE) )

libNRF:
	( cd src_libNRF; make $(LIBTYPE) )

libINSTALL:
	cp src_libNRF/libNRF.* $(LIBDIR)
	cp src_libYARM/libYARM.* $(LIBDIR)
	cp perl/libYarm.pm $(LIBDIR)

progs:
	( cd src_progs; make CC="$(CC)" OPTS="$(OPTS)" CFLAGS="$(CFLAGS)" all )

all:
	make libNRF
	make libYARM
	make libINSTALL
	make progs
	@ echo " "
	@ echo " *** The YARM executable files were successfully built ***"
	@ echo " "

html:
	( extras/generate_html.pl )
	@ echo " "
	@ echo " *** The YARM HTML documents were successfully built ***"
	@ echo " "

test:
	make libNRF
	make libYARM
	make libINSTALL
	( cd src_test; make CC="$(CC)" OPTS="$(OPTS)" CFLAGS="$(CFLAGS)" all )

clean:
	( cd src_libNRF; make clean )
	( cd src_libYARM; make clean )
	( cd src_test; make clean )
	( cd src_progs; make clean )
	$(RM) -f $(BINDIR)*
	$(RM) -f $(LIBDIR)*
	$(RM) -rf $(HTMLDIR)
	$(RM) -f yarm_v*.tar.gz
	@ echo " "
	@ echo "The YARM distribution is now clean"
	@ echo " "

install:
	@ echo " "
	@ echo "Copying the YARM executables to $(INSTDIR)/bin ..."
	@ if [ ! -w $(DEST) ]; then \
		echo "You don't have write permission for directory $(DEST)"; \
		echo "Log in as root and retry"; \
	fi
	@ if [ -r $(INSTDIR) ]; then $(MV) -i $(INSTDIR) $(INSTDIR).bak; fi
	@ mkdir $(INSTDIR); chmod a+rx $(INSTDIR)
	@ $(CP) -r $(BINDIR) $(INSTDIR)
	@ chmod a+rx $(INSTDIR)/bin
	@ chmod a+rx $(INSTDIR)/bin/*
	@ echo "  ... the executable files have been successfully installed"
	@ echo " "
	@ echo "Copying the YARM library files to $(INSTDIR)/$(LIBDIR) ..."
	@ $(CP) -r $(LIBDIR) $(INSTDIR)
	@ echo " ... the library files have been successfully installed"
	@ echo " "
	@ echo "Copying the YARM perl scripts to $(INSTDIR)/$(PERLDIR) ..."
	@ $(CP) -r $(PERLDIR) $(INSTDIR)
	@ echo "  ... the perl scripts have been successfully installed"
	@ echo " "
	@ echo "Copying the YARM data files to $(INSTDIR)/$(DATADIR) ..."
	@ $(CP) -r $(DATADIR) $(INSTDIR)
	@ echo " ... the data files have been successfully installed"
	@ echo " "
	@ echo "Copying the YARM manual to $(INSTDIR)/$(HTMLDIR) ..."
	@ $(CP) -r $(HTMLDIR) $(INSTDIR)
	@ echo "  ... the manual has been successfully installed"
	@ echo " "
	@ echo "Post installation notes:"
	@ echo '1) Make sure the variable $$yarm_home is set to $(INSTDIR)'
	@ echo "   in the file $(INSTDIR)/yarm_lib.pl"
	@ echo "2) Place the statement 'require \"$(INSTDIR)/lib/yarm_lib.pl\"'"
	@ echo "   on the top of any perl script from which you wish to "
	@ echo "   have access to the YARM subroutines"
	@ echo "3) Use any web browser to view the manual web pages found at "
	@ echo "   $(INSTDIR)/html"
	@ echo " "

##################################################
# Creating tarball distributions:
# 'make tarball'      => backup everything
# 'make tarball_src'  => everything BUT 'html' and 'data' sub dirs
# 'make tarball_html' => 'html' sub dir only
# 'make tarball_data' => 'data' sub dir only
tarball:
	( cd .. ; $(TAR) yarm_v$(VERSION).tar yarm_v$(VERSION) )
	$(MV) ../yarm_v$(VERSION).tar .
	$(COMPRESS) yarm_v$(VERSION).tar
	@ echo " "
	@ echo "Created a tarball called yarm_v$(VERSION).tar.gz"
	@ echo "    which contains the entire YARM distribution"
	@ echo " "

tarball_src:
	( cd .. ; $(TAR) yarm_v$(VERSION)_src.tar yarm_v$(VERSION)/src )
	$(MV) ../yarm_v$(VERSION)_src.tar .
	$(COMPRESS) yarm_v$(VERSION)_src.tar
	@ echo " "
	@ echo "Created a tarball called yarm_v$(VERSION)_src.tar.gz"
	@ echo "    which contains only the YARM C source code"
	@ echo " "

tarball_html:
	( cd .. ; $(TAR) yarm_v$(VERSION)_html.tar yarm_v$(VERSION)/html )
	$(MV) ../yarm_v$(VERSION)_html.tar .
	$(COMPRESS) yarm_v$(VERSION)_html.tar
	@ echo " "
	@ echo "Created a tarball called yarm_v$(VERSION)_html.tar.gz"
	@ echo "    which contains only the YARM manual"
	@ echo " "

tarball_data:
	( cd .. ; $(TAR) yarm_v$(VERSION)_data.tar yarm_v$(VERSION)/data )
	$(MV) ../yarm_v$(VERSION)_data.tar .
	$(COMPRESS) yarm_v$(VERSION)_data.tar
	@ echo " "
	@ echo "Created a tarball called yarm_v$(VERSION)_data.tar.gz"
	@ echo "    which contains only the YARM data directory"
	@ echo " "
