######################################################################
#
# This is the makefile for the installation and maintenance of the 
# "doppler" set of ndf routines for MEM Doppler tomography.
#
# Written and maintained by T.R.Marsh; last updated: 16/04/2008
#
# Installation of doppler:
#
# (1) Edit the INSTALL variable below to point to wherever you want
#     to install Doppler
#
# (2) Make sure that the directory containing the STARLINK scripts
#     'alink', 'ndf_link_adam', 'pgplot_link' is in your path. This 
#     is /star/bin for a standard installation of STARLINK software.
#
# (3) Type 'make clean' followed by 'make'. 
#
# (4) Source the file Doppler in the installation directory to define aliases,
#     and off you go. Point to html/OVERVIEW.html in the installation directory
#     to see web-based help.     
#
# Likely/possible problems:
#
# (1) Wrong libraries for PGPLOT
#
# (2) You don't have STARLINK installed or you have not installed
#     all necessary libraries -- lots are needed -- or you have failed
#     to set STARLINK_DIR or you have not run the STARLINK cshrc and login
#     scripts.
#
# (3) You are using wrong version of make. Try 'make -v'
#     I use GNU make.
#
# (4) You have not got 'g95'
#
#
####################################################################

# TRM_SOFTWARE is an environment variable to specify the
# top-level directory

DATE        = $(shell date)
VERSION     = 1.0.1

INSTALL    = $(TRM_SOFTWARE)/doppler
CURSES     = $(TRM_SOFTWARE)/lib
STAR       = $(STARLINK_DIR)
FC         = g95
FFLAGS     = -Wall -Wno=155 -O -fno-second-underscore -fstatic

# PGPLOT, ADAM/NDF libraries

ADAM_NDF   = -L$(STAR)/lib `ndf_link_adam`
PGPLOT     = -L$(STAR)/lib  `pgplot_link`

# Archive
# Directory search path

ARCH    = doppler
LARCH   = $(INSTALL)/lib$(ARCH).a

$(LARCH)(%.o): source/%.f
	$(FC) $(FFLAGS) -c $< -o $%
	ar cr $@ $%
	rm -f $%

$(LARCH)(%.o): ../subrs/%.f
	$(FC) $(FFLAGS) -c $< -o $%
	ar cr $@ $%
	rm -f $%

$(LARCH)(%.o): ../roche/%.f
	$(FC) $(FFLAGS) -c $< -o $%
	ar cr $@ $%
	rm -f $%

$(LARCH)(%.o): ../mem/%.f
	$(FC) $(FFLAGS) -c $< -o $%
	ar cr $@ $%
	rm -f $%

$(LARCH)(%.o): ../pgplus/%.f
	$(FC) $(FFLAGS) -c $< -o $%
	ar cr $@ $%
	rm -f $%

# Default target -- makes everything one needs.

all: dir slinks doppler links help Doppler
	@echo ' '
	@echo 'Finished compilation of doppler'
	@echo 'To run it, source the file: '
	@echo $(INSTALL)'/Doppler to define aliases.'
	@echo ' '

.PHONY: all dir slinks doppler links help Doppler

# Rule for creation of directories that are needed

dir:
	-\mkdir $(INSTALL)
	-\mkdir $(INSTALL)/html

# Soft links which must be present for compilation

slinks: source/DAT_PAR source/PRM_PAR source/SAE_PAR source/CNF_PAR

# and rules for making them

source/CNF_PAR:
	ln -sf $(STAR)/include/CNF_PAR source/CNF_PAR

source/DAT_PAR:
	ln -sf $(STAR)/include/DAT_PAR source/DAT_PAR

source/PRM_PAR:
	ln -sf $(STAR)/include/PRM_PAR source/PRM_PAR

source/SAE_PAR:	
	ln -sf $(STAR)/include/SAE_PAR source/SAE_PAR

# Dependencies on includes

$(LARCH)(comdat.o):  source/doppler.inc
$(LARCH)(memit.o):   source/doppler.inc source/mem.inc
$(LARCH)(optgam.o):  source/doppler.inc
$(LARCH)(optscl.o):  source/doppler.inc
$(LARCH)(optr.o):    source/doppler.inc
$(LARCH)(eclipse.o): source/doppler.inc

# List of routines needed

DOPPLER_OBJ = $(LARCH)(makimg.o profadd.o comdat.o chisqrd.o \
              phase_comp.o optr.o optgam.o memit.o mover.o profmed.o \
              optscl.o imsymm.o gauss.o ran.o cgs.o gblurr.o bilin.o \
              ludcmp.o realft.o medval.o chsubs.o heapsort.o memcore.o \
              memsys.o eclipse.o blink.o inlag.o lag.o poly.o makcirc.o)

# doppler executable. Check that ndf_link_adam and alink are recognised
# to avoid vast number of incomprehensible errors. This executable
# is never run directly, but through a series of soft links, one
# for each command.
 
doppler: slinks source/doppler.f $(DOPPLER_OBJ) Makefile
	@which alink ndf_link_adam pgplot_link
	@echo "Linking doppler ..."
	$(F77) $(FFLAGS) -c -o doppler.o source/doppler.f
	alink doppler.o -o $(INSTALL)/doppler \
	-L$(INSTALL) -l$(ARCH) $(PGPLOT) $(ADAM_NDF) -L$(CURSES)
	chmod a+x $(INSTALL)/doppler
	\rm doppler.o

# Makes soft links to executable for each command

links:
	ln -sf $(INSTALL)/doppler $(INSTALL)/comdat
	ln -sf `pwd`/source/comdat.ifl $(INSTALL)/comdat.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/imsymm
	ln -sf `pwd`/source/imsymm.ifl $(INSTALL)/imsymm.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/makimg
	ln -sf `pwd`/source/makimg.ifl $(INSTALL)/makimg.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/memit
	ln -sf `pwd`/source/memit.ifl $(INSTALL)/memit.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/optgam
	ln -sf `pwd`/source/optgam.ifl $(INSTALL)/optgam.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/optscl
	ln -sf `pwd`/source/optscl.ifl $(INSTALL)/optscl.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/eclipse
	ln -sf `pwd`/source/eclipse.ifl $(INSTALL)/eclipse.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/makcirc
	ln -sf `pwd`/source/makcirc.ifl $(INSTALL)/makcirc.ifl
	chmod 755 $(INSTALL)
	chmod 644 source/*.ifl

# Makes a file called Doppler which can be used to set up command aliases

Doppler:
	echo '#' > $(INSTALL)/Doppler
	echo '# This file was generated by make Doppler' >> $(INSTALL)/Doppler
	echo '# It defines aliases for the doppler package' >> $(INSTALL)/Doppler
	echo '# source it to define them.' >> $(INSTALL)/Doppler
	echo '#' >> $(INSTALL)/Doppler
	echo ''alias comdat  $(INSTALL)/comdat''  >> $(INSTALL)/Doppler
	echo ''alias imsymm  $(INSTALL)/imsymm''  >> $(INSTALL)/Doppler
	echo ''alias makimg  $(INSTALL)/makimg''  >> $(INSTALL)/Doppler
	echo ''alias memit   $(INSTALL)/memit''   >> $(INSTALL)/Doppler
	echo ''alias optgam  $(INSTALL)/optgam''  >> $(INSTALL)/Doppler
	echo ''alias optscl  $(INSTALL)/optscl''  >> $(INSTALL)/Doppler
	echo ''alias eclipse $(INSTALL)/eclipse'' >> $(INSTALL)/Doppler
	echo ''alias makcirc $(INSTALL)/makcirc'' >> $(INSTALL)/Doppler
	chmod a+r $(INSTALL)/Doppler

# Generate html help. help_pointers file is read by
# perl script

help:
	@echo ''comdat  `pwd`/source/comdat.f''  >  help_pointers
	@echo ''imsymm  `pwd`/source/imsymm.f''  >> help_pointers
	@echo ''makimg  `pwd`/source/makimg.f''  >> help_pointers
	@echo ''memit   `pwd`/source/memit.f''   >> help_pointers
	@echo ''optgam  `pwd`/source/optgam.f''  >> help_pointers
	@echo ''optscl  `pwd`/source/optscl.f''  >> help_pointers
	@echo ''eclipse `pwd`/source/eclipse.f'' >> help_pointers
	@echo ''makcirc `pwd`/source/makcirc.f'' >> help_pointers
	perl genhtml.pl $(INSTALL)/html
	@-\rm help_pointers 	
	-ln -sf `pwd`/html/CLASSES.html $(INSTALL)/html/CLASSES.html
	-ln -sf `pwd`/html/COMMANDS.html $(INSTALL)/html/COMMANDS.html
	-ln -sf `pwd`/html/OVERVIEW.html $(INSTALL)/html/OVERVIEW.html
	-ln -sf `pwd`/html/SETUP.html $(INSTALL)/html/SETUP.html
	-ln -sf `pwd`/html/doppler.css $(INSTALL)/html/doppler.css
	chmod 755 . html $(INSTALL) $(INSTALL)/html
	chmod 644 html/* $(INSTALL)/html/*

# Removes some generated files to save a bit of space

clean:
	@-\rm -fr $(INSTALL)
	@-\rm source/DAT_PAR source/PRM_PAR source/SAE_PAR


tar:
	cd ../..; tar -cvf /tmp/doppler.tar F77/doppler F77/subrs F77/mem F77/pgplus F77/roche
	\rm -f /tmp/doppler.tar.gz
	gzip /tmp/doppler.tar
	scp /tmp/doppler.tar.gz $(WEB_SERVE):$(WEB_PATH)/software/.
	scp -r $(INSTALL)/html $(WEB_SERVE):$(WEB_PATH)/software/doppler/.
	ssh $(WEB_SERVE) "cd $(WEB_PATH)/software; sed -e 's%<\!-- doppler date --><td>.*</td>%<\!-- doppler date --><td>$(DATE)</td>%' index.html > junk.html; mv junk.html index.html"
	ssh $(WEB_SERVE) "cd $(WEB_PATH)/software; sed -e 's%<\!-- doppler version --><td>.*</td>%<\!-- doppler version --><td>$(VERSION)</td>%' index.html > junk.html; mv junk.html index.html"


 











