
# Define variable GMT_SCRIPTS (assuming GMT scripts carry the suffix .gmt)
#	-use make's 'shell' function to call 'ls'
#	-the shell function actually executes 
#	-for Makefiles the function syntax is: '$(functionname arguments)'
# 	-redirect 'ls' errors to /dev/null using '2>' (we don't want these listed as our scripts)
#	 note that stdin, stderr have file descriptors 1, and 2 respectively
#       -GMT_SCRIPTS is then a list of GMT files
GMT_SCRIPTS := $(shell ls ./*.gmt 2>/dev/null)

# The maps are supposed to have the same name as the GMT scripts, but carry 
# the burden of being a PDF. $(GMT_SCRIPTS:%.gmt=%.pdf) does the renaming for
# ALL files in the array GMT_SCRIPTS. The $(notdir) function returns only 
# those elements of GMT_SCRIPTS that are actual files (no directories, so to speak)
MAPS        := $(notdir $(GMT_SCRIPTS:%.gmt=%.pdf))

# Our maps, of course, depend on the data that's being plotted. We should
# therefore know all of our data files. Again, I call make's 'shell' function
# this time invoking 'find' which looks in the directory '../data' and returns
# everything of type 'f' (file). Errors are being redirected again.
# The reason for using 'find' instead of 'ls' is that find includes the 
# pathname in the result. Make needs to know not only what you call the file,
# but also where it lives, to check for any changes. 
#
# DATAFILES will contain something like '../data/test.dat'
DATAFILES   := $(shell find ../data -type f 2>/dev/null)

# A pattern rule: Create a file that has the suffix .pdf from files with the 
# same name (%) and all the datafiles. Hence, if there's any change been made to 
# either the maps GMT file or any of the data files the pdf will be created.
# Note that this is slightly inefficient since a change to one data file will lead
# to all maps being recreated, independent of them being used for a specific map.
%.pdf : %.gmt $(DATAFILES)
# 	Tell the user what's going on
	@echo 'calling ./$<'
# 	create ps file from GMT script, $< represents the name of the first prerequisite 
# 	in our case that's the GMT (shell) script. 
# 	(Note that this changes if you switch the prerequisites 
	@./$<
# 	GMT creates postscript, but we want pdf. Replace the suffix as described for 
#	MAPS variable only from .gmt to .ps (assuming the GMT script creates output that
# 	carries this name). The variable that's being changed is '$<'. Then invoke epstopdf
# 	with this ps-file name as an argument to get the PDF
	@ps2pdf $(<:%.gmt=%.ps)

# ALL is a standard target, traditionally this contains whatever is necessary to 
# get the whole job done. Our prerequisites to get everything done are all the 
# maps in PDF format. Make will now invoke the rule
#		%.pdf : %.gmt $(DATAFILES) 
# for each element in $(MAPS) since the rule describes how to create the target
# PDF-file in our context. Make won't do a thing when neither gmt scripts nor data
# files have changed.
# Once all the pdf's are created it will execute the commands in it's body, here
# telling the user that it did it's job.
all: $(MAPS) 
	@echo
	@echo "   Maps in '"$(shell pwd)"' up to date."
	@echo

clean: 
	rm .gmt* *.pdf *.ps
