######################################################################
#
# This is the makefile for installation and maintenance of molly
# and associated routines. molly is a program for the analysis of
# of (mainly) time-series spectroscopy.
#
# T.R.Marsh 06/11/2008, updated 07/01/2020
#
# Installation is a matter of editing this file; see below.
#
# Main targets:
#
# all    -- makes molly, pca, rubfix and help. Default if you say 'make'. 
# molly  -- main spectrum analysis program.
# molcon -- conversion of molly files between different UNIX systems.
# clean  -- Removes files created during installation. Useful
#           if you encounter problems during installation and you 
#           want to restart from scratch. Removes entire installation 
#           directory so take care that it does not contain anything other
#           than molly stuff.
# help   -- generates help on molly, both html and a pointer file
#           that molly accesses when you ask for help inside it.
# tar    -- makes a tar file for molly.
#
# Old targets, no longer supported:
#
# pca    -- carries out principal components analysis of molly spectra
# rubfix -- for translation of RUBY to molly files. If you don't know
#           what RUBY is, you don't need rubfix.
#
# It might also be useful to be aware of trm.molly, a little Python
# package that allows you to read & write molly data. Probably mainly
# useful for reviving old molly data without the hassle of installing
# molly. You can get from github at https://github.com/WarwickAstro/trm-molly
#
####################################################################
#
# Installation:
#
# (1) Look at the makefile below from START!!! to STOP!!!
#
# (2) I now recommend using gfortran as the compiler.
#
# (3) You must have starlink installed and have defined the environment
#     variable STARLINK_DIR
#
# (4) Type 'make clean' (deletes all files produced by molly -- but see (4)
#     first), then 'make', wait a bit ...
#
# (5) If it all works, then either put the executable directory
#     in your path (see below) or define suitable aliases.
#
# (6) Type 'make clean' if you want to get rid of everything, but note
#     that this wipes out the installation directory and could be dangerous
#     if you have other stuff there.
#
# Possible/likely problems are:
#
#   (i) You have not installed STARLINK
#
#  (ii) You have not defined the STARLINK environment variable STARLINK_DIR to
#       point to the top-level STARLINK directory as required by the latest 
#       JAC release (as of May 2008)
#
# (iii) You have not supplied a valid installation directory.
#
#  (iv) You don't have 'gfortran' or 'libreadline' (development versions) installed
#
#   (v) You have installed the wrong glibc version of starlink
#
#  (vi) You have a Mac in which case you will have to make some changes. Search for
#       'mac' in this file and have a look at the README file. It has worked on an
#       intel machine running OS X.5 with 'g95' from the starlink installation.
#
# (vii) Take a look at the bottom of README file for more tales of installation woe.
#
########################################################################
#
# START!!!
#
# NB Much the best to install molly in its own directory as it makes
#    the 'make clean' operation safe.
#
# These are the meanings of variables in this script:
#
# INSTALL -- installation dirctory. Make it a separate one just for molly otherwise
#            'make clean' could be a disaster.
# STAR    -- top level of STARLINK distribution. Should have bin, etc inside it.
# FC      -- Fortran77 compiler
# FFLAGS  -- Flags for F77 compiler
# CC      -- C compiler
# CFLAGS  -- Flags for C compiler
# MCFLAGS -- Flag for molcon to indicate Sun (SPARC) byte order or not
# TAR     -- Directory for export tar file
# FGET    -- Version for command line recall stuff. Version 3 for gfortran (& g95), 1 for g77

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

# Warwick version
INSTALL    = $(TRM_SOFTWARE)/molly
STAR       = $(STARLINK_DIR)

PATH      := $(STAR)/bin:${PATH}

# I now use gfortran only and do not recommend any other compiler but have left stuff
# connected to g95 for old times sake.
#
#FC         = g95
#FFLAGS     = -Wall -Wno=104 -O -fno-second-underscore -fstatic -I$(STAR)/include
FC         = gfortran
FFLAGS     = -Wall -O -fno-second-underscore -fno-automatic -fno-range-check -fbackslash -I$(STAR)/include
CC         = gcc
CFLAGS     = -Wall -DPOSIX
MCFLAGS    =

# Different versions of fgetlin.c are needed for different compilers.
#
# .v1 for f77/Solaris, g77/RedHat Linux 6.1, 7.2
# .v2 for g77/RedHat Linux 5.2
# .v3 for gfortran (and g95 and poss also g77). Uses readline
#
# Note that owing to a change to get_input.f to enable filename completion
# for character string input, .v1 and .v2 will definitely not work now.

FGET       = fgetlin.c.v3
EXTRA_LIBS = -lreadline -lncurses

# Starlink link scripts
FITS    = -L$(STAR)/lib `cfitsio_link`
PDA     = -L$(STAR)/lib `pda_link`
NDF     = -L$(STAR)/lib `ndf_link`
PGPLOT  = -L$(STAR)/lib `pgplot_link`
SLA     = -L$(STAR)/lib `sla_link`

# Run time path for shareable libraries to avoid the need for setting
# LD_LIBRARY_PATH.  This is very likely to be system-dependent!! To
# see what it should be for your system you might want to have a look
# at the link statement used when compiling pamela which uses 'alink'
# which I think uses GNU's 'libtool' to cope with platform
# dependencies.  Note: you may experience difficulties if you do set
# LD_LIBRARY_PATH as it overrides those set inside the exceutable,
# including ones that point at the g95 dynamic libraries

# MAC users comment the next line out, then uncomment the succeeding
# line This works at least on OS X.5, intel chips, but rpath
# apparently works on MacBook Pro (Intel Leopard), see the README for
# various bits of info, primarily from Peter Jonker. It seems
# relatively painless.
RPATH = -Wl,-rpath -Wl,$(STAR)/lib
#RPATH =

# STOP!!!

# From now on, everything should be pretty much constant.
#
# EXCEPT 07/01/2020: Elme Breedt had to re-order some libraries below
# when installing under Ubuntu. See the file README for what she had
# to do; not difficult; it concerns the main "molly" target linking line.

# Archive. All object files are stored in this archive for
# speed of later re-compilation.

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

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

$(LARCH)(%.o): src/%.c
	$(CC) $(CFLAGS) -c $< -o $%
	ar cr $@ $%
	rm -f $%

# Default target

all: dir molly molcon help

.PHONY: dir molly pca rubfix molcon help

# Special rule for fgetlin.c as it depends on the OS

src/fgetlin.c: src/$(FGET)
	\ln -f src/$(FGET) src/fgetlin.c

# Rule for creation of directories that are needed

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

# Now the lists of objects for each program

MOLLY_OBJ  = $(LARCH)(addsub.o arc_fit.o arcal.o ascout.o asctom.o \
             ascwr.o axtype.o bckfil.o bcoff.o cadd.o calplot.o ccal.o \
             clip.o comdef.o comsubs.o contfit.o control.o cos_rej.o \
             cset.o cursor.o dered.o ecopy.o eduser.o extinc.o ffilt.o \
             fitspec.o fixerr.o fixvel.o flare.o flcal.o flx_fit.o \
             get_input.o getconst.o getdat.o gethelp.o getlis.o \
             getlun.o getset.o gofit.o group.o gsm.o hedave.o hedchg.o \
             hedfix.o hedhist.o hedlis.o hedplt.o hedtrn.o hstasc.o \
	         label.o lasc.o leasq.o lfor.o lgcurv.o ligred.o \
             lruby.o masker.o maths.o mgasfit.o mlims.o mmrqmin.o \
             molwr.o mtoasc.o optsub.o patch.o pergm.o \
             phsbin.o planck.o polmol.o powell3.o putlab.o qplot.o \
             radvel.o relmag.o repet.o replace.o rfits.o rotb.o \
             sclear.o select.o set_aspect.o set_axes.o set_clobber.o \
             set_close.o set_col.o set_confrm.o set_csize.o set_dev.o \
             set_eplot.o set_font.o set_head.o set_id.o set_lcolour.o \
             set_lim.o set_lstyle.o set_lwidth.o set_molex.o set_mute.o \
             set_offset.o set_pix.o set_plabel.o set_sel.o set_stype.o \
             set_xyr.o set_zero.o sinadd.o sincfun.o sindec.o skipper.o \
             sl_inf.o spcave.o spcbin.o spcdmp.o spcfak.o spcmsk.o \
             spcplt.o spcred.o spctrl.o splmol.o tabred.o temin.o \
             tyspc.o tfilt.o vpset.o wcomb.o wrfits.o xcor.o molfrd.o \
             ndfred.o doppdmp.o pl_close.o devopcl.o bckpro.o ndfdmp.o \
             boxfilt.o calendar.o chsubs.o cgs.o covsrt.o \
             fruit.o gauss.o heapsort.o ifind.o isred.o linfit.o \
             lineposn.o lsquar.o median.o medfilt.o medval.o \
             nist.o polint.o poly.o qromb.o ran.o realft.o \
             realft.o sealis.o shellsort.o sigfake.o siteq.o trapzd.o \
             rebin.o reject.o pgwedge.o pglimit.o fgetlin.o fisatty.o \
             extrapol.o rmsspec.o circpol.o)

# extracted as it failed under g95 and I can't be bothered to
# resuscitate it.
# molfrd.o

PCA_OBJ    = $(LARCH)(molwr.o getdat.o eigvec.o)

RUBFIX_OBJ = $(LARCH)(molwr.o)

# Dependencies on include files

$(LARCH)(mgasfit.o):	src/gsfit.inc src/molly.coms
$(LARCH)(mmrqmin.o):	src/gsfit.inc src/molly.coms
$(LARCH)(set_col.o):	src/setcol.inc
$(LARCH)(putlab.o):	src/device.inc
$(LARCH)(sincfun.o):	src/maxsinc.inc
$(LARCH)(rotb.o):	src/maxsinc.inc src/molly.coms
$(LARCH)(orbfit.o):	src/molly.coms
$(LARCH)(bcoff.o):	src/maxsinc.inc src/molly.coms
$(LARCH)(devopcl.o):	src/device.inc
src/molly.coms:	src/molly.pars
 
# molly executable. 4 steps
#
# 1) Check for recognition of library link scripts
# 2) Generate molly.f from molly.f.template
# 3) Compile and link
# 4) Clear up a bit, set some permissions.

molly:  $(MOLLY_OBJ) src/molly.f.template
	@which cfitsio_link pda_link ndf_link
	perl modmolly.pl > src/molly.f	
	$(FC) $(FFLAGS) -c -o molly.o src/molly.f
	$(FC) $(FFLAGS) -o $(INSTALL)/molly molly.o $(EXTRA_LIBS) \
	-L$(INSTALL) -l$(ARCH) $(NDF) $(PDA) $(FITS) $(PGPLOT) $(SLA) $(RPATH)
	chmod a+xr $(INSTALL)/molly
	-\rm molly.o
	-\rm src/molly.f

# principal components program
#
# call to IARGC not supported under gfortran. Probably easy to fix but
# I have not used this for years so, its just commented out.

#pca:  src/pca.f $(PCA_OBJ) 
#	@which $(STAR)/bin/pda_link
#	$(FC) $(FFLAGS) -c -o pca.o src/pca.f
#	$(FC) $(FFLAGS) pca.o -o $(INSTALL)/pca -L$(INSTALL) -l$(ARCH) $(PDA) $(RPATH)
#	chmod a+xr $(INSTALL)/pca
#	-\rm pca.o

# Ruby fixing routine
#
# call to IARGC not supported under gfortran. Probably easy to fix but
# I have not used this for years so, its just commented out.

#rubfix:  src/rubfix.f $(RUBFIX_OBJ)
#	$(FC) $(FFLAGS) -c -o rubfix.o src/rubfix.f
#	$(FC) $(FFLAGS) rubfix.o -o $(INSTALL)/rubfix -L$(INSTALL) -l$(ARCH) $(RPATH)
#	chmod a+xr $(INSTALL)/rubfix 
#	-\rm rubfix.o

# Foreign molly data conversion

molcon:  src/molcon.c
	$(CC) $(MCFLAGS) -o $(INSTALL)/molcon src/molcon.c
	chmod a+xr $(INSTALL)/molcon

# clean -- removes files created by 'make' or 'make all'
# and should be run if you have problems during make and want
# start from a clean slate. The html help files generated all
# start with a lower case letter which is the basis for the
# last line.

clean:
	-@\rm -rf $(INSTALL)
	-@\rm -f ../../C/getline/fgetlin.c

# Run perl script to generate html. 

help: 
	perl genhtml.pl $(INSTALL)/html $(INSTALL)/molly
	-cp -f `pwd`/html/CHANGES.html    $(INSTALL)/html/CHANGES.html
	-cp -f `pwd`/html/DATA.html       $(INSTALL)/html/DATA.html
	-cp -f `pwd`/html/COMCALL.html    $(INSTALL)/html/COMCALL.html
	-cp -f `pwd`/html/HELP.html       $(INSTALL)/html/HELP.html
	-cp -f `pwd`/html/MEMORY.html     $(INSTALL)/html/MEMORY.html
	-cp -f `pwd`/html/OWN.html        $(INSTALL)/html/OWN.html
	-cp -f `pwd`/perl/test.html       $(INSTALL)/html/PERL.html
	-cp -f `pwd`/html/SELECTION.html  $(INSTALL)/html/SELECTION.html
	-cp -f `pwd`/html/SETUP.html      $(INSTALL)/html/SETUP.html
	-cp -f `pwd`/html/STANHEAD.html   $(INSTALL)/html/STANHEAD.html
	-cp -f `pwd`/html/STARTUP.html    $(INSTALL)/html/STARTUP.html
	-cp -f `pwd`/html/SUPPORT.html    $(INSTALL)/html/SUPPORT.html
	-cp -f `pwd`/html/STORAGE.html    $(INSTALL)/html/STORAGE.html
	-cp -f `pwd`/html/USER_GUIDE.html $(INSTALL)/html/USER_GUIDE.html
	-cp -f `pwd`/html/molly.css       $(INSTALL)/html/molly.css
	-git log > $(INSTALL)/html/git_change_log || echo "git not available; no change log created"
	@chmod 755 $(INSTALL)/html 
	@chmod 644 $(INSTALL)/html/* 

changes:
	git log > /tmp/molly_changes
	git log $(PREVIOUS)..HEAD > /tmp/molly_recent_changes

tar: changes
	cd ../..; tar cvfz /tmp/molly.tar.gz --exclude=".git" F77/molly F77/subrs F77/pgplus C/getline
#	cp $(PGPLOT_DIR)/grfont.dat .; cp $(PGPLOT_DIR)/rgb.txt .; cp $(PGPLOT_DIR)/pgxwin_server .; cp $(INSTALL)/molly .
#	cd ..; tar -cvf /tmp/molly_exe.tar molly/README molly/startup molly/molly molly/command_defs molly/help_pointers molly/grfont.dat molly/rgb.txt molly/pgxwin_server molly/src
#	\rm -f grfont.dat rgb.txt pgxwin_server molly 
#	\rm -f /tmp/molly.tar.gz /tmp/molly_exe.tar.gz
#	gzip /tmp/molly.tar /tmp/molly_exe.tar
#	gzip /tmp/molly.tar
	scp /tmp/molly_changes    $(WEB_SERVE):$(WEB_PATH)/software/.
	scp /tmp/molly_recent_changes $(WEB_SERVE):$(WEB_PATH)/software/.
	scp /tmp/molly.tar.gz     $(WEB_SERVE):$(WEB_PATH)/software/.
#	scp /tmp/molly_exe.tar.gz $(WEB_SERVE):$(WEB_PATH)/software/.
	scp -r $(INSTALL)/html    $(WEB_SERVE):$(WEB_PATH)/software/molly/.
	ssh $(WEB_SERVE) "cd $(WEB_PATH)/software; sed -e 's%<\!-- molly date --><td>.*</td>%<\!-- molly date --><td>$(DATE)</td>%' index.html > junk.html; mv junk.html index.html"
	ssh $(WEB_SERVE) "cd $(WEB_PATH)/software; sed -e 's%<\!-- molly version --><td>.*</td>%<\!-- molly version --><td>$(CURRENT)</td>%' index.html > junk.html; mv junk.html index.html"















