######################################################################
#
# 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
#
# 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_aliases 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.     
#
# See 'Problems' for some possible issues during installation.
#
####################################################################

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

DATE        = $(shell date)
EXPORT_DATE = $(shell cat .export_date)

CURRENT     = $(shell git tag | tail -n 1)
PREVIOUS    = $(shell git tag | tail -n 2 | head -n 1)

INSTALL    = $(TRM_SOFTWARE)/doppler
CURSES     = $(TRM_SOFTWARE)/lib
STAR       = $(STARLINK_DIR)
FC         = g95
FFLAGS     = -Wall -Wno=155 -O -fno-second-underscore -fstatic -I$(STAR)/include
#FC         = gfortran
#FFLAGS     = -Wall -O -fno-second-underscore -fno-automatic -fno-range-check -I$(STAR)/include

# Command search path
PATH      := $(STAR)/bin:${PATH}

# 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): src/%.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 doppler links help doppler_aliases message

.PHONY: all dir doppler links help Doppler

# Rule for creation of directories that are needed

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

# Dependencies on includes

$(LARCH)(comdat.o):  src/doppler.inc
$(LARCH)(memit.o):   src/doppler.inc src/mem.inc
$(LARCH)(optgam.o):  src/doppler.inc
$(LARCH)(optscl.o):  src/doppler.inc
$(LARCH)(optr.o):    src/doppler.inc
$(LARCH)(eclipse.o): src/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: links src/doppler.f $(DOPPLER_OBJ) Makefile
	@which alink ndf_link_adam pgplot_link
	@echo "Linking doppler ..."
	$(F77) $(FFLAGS) -c -o doppler.o src/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`/src/comdat.ifl $(INSTALL)/comdat.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/imsymm
	ln -sf `pwd`/src/imsymm.ifl $(INSTALL)/imsymm.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/makimg
	ln -sf `pwd`/src/makimg.ifl $(INSTALL)/makimg.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/memit
	ln -sf `pwd`/src/memit.ifl $(INSTALL)/memit.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/optgam
	ln -sf `pwd`/src/optgam.ifl $(INSTALL)/optgam.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/optscl
	ln -sf `pwd`/src/optscl.ifl $(INSTALL)/optscl.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/eclipse
	ln -sf `pwd`/src/eclipse.ifl $(INSTALL)/eclipse.ifl
	ln -sf $(INSTALL)/doppler $(INSTALL)/makcirc
	ln -sf `pwd`/src/makcirc.ifl $(INSTALL)/makcirc.ifl
	chmod 755 $(INSTALL)
	chmod 644 src/*.ifl

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

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

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

help:
	@echo ''comdat  `pwd`/src/comdat.f''  >  help_pointers
	@echo ''imsymm  `pwd`/src/imsymm.f''  >> help_pointers
	@echo ''makimg  `pwd`/src/makimg.f''  >> help_pointers
	@echo ''memit   `pwd`/src/memit.f''   >> help_pointers
	@echo ''optgam  `pwd`/src/optgam.f''  >> help_pointers
	@echo ''optscl  `pwd`/src/optscl.f''  >> help_pointers
	@echo ''eclipse `pwd`/src/eclipse.f'' >> help_pointers
	@echo ''makcirc `pwd`/src/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 src/DAT_PAR src/PRM_PAR src/SAE_PAR

changes:
	git log > /tmp/doppler_changes
	git log $(PREVIOUS)..HEAD > /tmp/doppler_recent_changes

tar:
	echo $(DATE) > .export_date
	cd ..; tar -cvf /tmp/doppler.tar --exclude=".git" doppler subrs mem roche pgplus
	\rm -f /tmp/doppler.tar.gz
	gzip /tmp/doppler.tar

export: changes tar
	scp /tmp/doppler_changes $(WEB_SERVE):$(WEB_PATH)/software/.
	scp /tmp/doppler_recent_changes $(WEB_SERVE):$(WEB_PATH)/software/.
	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>$(CURRENT)</td>%' index.html > junk.html; mv junk.html index.html"

message:
	@-\echo " "
	@-\echo " "
	@-\echo "To start doppler, source the file $(INSTALL)/doppler_aliases"
	@-\echo "You may want to create an alias for it in your .cshrc as in:"
	@-\echo " "
	@-\echo "alias doppler 'source $(INSTALL)/doppler_aliases'"
	@-\echo " "
	@-\echo "For further help, point a web browser at $(INSTALL)/html/INDEX.html"
	@-\echo " "
	@-\echo "Good luck!"
	@-\echo " "

