#-----------------------------------------------------------------------------
# GNU makefile for building the WCSLIB 4.8 FORTRAN wrappers.
#
# Summary of the main targets
# ---------------------------
#   build:     Build the library
#
#   clean:     Delete intermediate object files.
#
#   cleaner:   clean, and also delete the test executables.
#
#   cleanest (distclean, or realclean): cleaner, and also delete the
#              object library.
#
#   check (or test): Compile and run the test programs.  By default they are
#              executed in batch mode, and non-graphical tests only report
#              "PASS" on success.  Use
#
#                gmake MODE=interactive check
#
#              to run them interactively with full diagnostic output.
#
#   tests:     Compile the test programs (but don't run them).
#
# Notes:
#   1) If you need to make changes then preferably modify ../makedefs.in
#      instead and re-run configure.
#
#   2) This makefile assumes that the WCSLIB 4.8 sources reside in ../C
#      (as in the distribution kit).
#
#   3) twcstab assumes that ../C/wcstab.fits has already been generated by
#      the corresponding C test program.
#
# Author: Mark Calabretta, Australia Telescope National Facility
# http://www.atnf.csiro.au/~mcalabre/index.html
# $Id: GNUmakefile,v 4.8.1.1 2011/08/15 08:07:07 cal103 Exp cal103 $
#-----------------------------------------------------------------------------
# Get configure settings.
include ../makedefs

MODULES := $(patsubst %.c,%.o,$(filter-out getwcstab_f.c,$(wildcard *.c)))
MODULES += $(patsubst %.f,%.o,$(wildcard *.f))

ifdef GETWCSTAB
  FGETWCSTAB := ../C/getwcstab.o
endif

WCSLIB := ../C/$(WCSLIB)

# Build the sharable library?
ifneq "$(SHRLIB)" ""
  PICLIB := ../C/libwcs-PIC.a
  SHRLIB := ../C/$(SHRLIB)
endif

CPPFLAGS += -I.. -I../C

vpath %.h  ..:../C
vpath %.in ..


# For building and exercising the test suite
# ------------------------------------------
# Test programs that don't require PGPLOT.
TEST_N := tlin tlog tprj1 tsph tspx ttab1 twcs twcssub tpih1 tfitshdr tunits \
          twcsfix

# Test programs that require CFITSIO (they don't need PGPLOT).
TEST_C := twcstab

# Test programs that require PGPLOT but not PGSBOX.
TEST_P := tspc tprj2 tcel1 ttab2 ttab3 twcsmix

# Test programs that require PGPLOT and PGSBOX.
TEST_B := tpih2

TESTS  := $(TEST_N)

# Add test programs that require CFITSIO if we have it.
ifneq "$(CFITSIOINC)" ""
ifneq "$(CFITSIOLIB)" ""
  TESTS  += $(TEST_C)
endif
endif

# Add test programs that require PGPLOT if we have it.
ifneq "$(PGPLOTINC)" ""
ifneq "$(PGPLOTLIB)" ""
  TESTS   += $(TEST_P) $(TEST_B)
endif
endif

PGSBOXLIB := ../pgsbox/libpgsbox-$(LIBVER).a
PGPLOTLIB := $(PGPLOTLIB)


# Pattern rules
#--------------

$(WCSLIB)(%.o) : %.c
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFLAGS) -c $<
	   $(AR) r $(WCSLIB) $%
	-@ $(RM) $%

$(WCSLIB)(%.o) : %.f
	-@ echo ''
	   $(FC) $(FFLAGS) -c $<
	   $(AR) r $(WCSLIB) $%
	-@ $(RM) $%

$(PICLIB)(%.o) : %.c
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(SHRFLAGS) -c $<
	   $(AR) r $(PICLIB) $%
	-@ $(RM) $%

$(PICLIB)(%.o) : %.f
	-@ echo ''
	   $(FC) $(FFLAGS) $(SHRFLAGS) -c $<
	   $(AR) r $(PICLIB) $%
	-@ $(RM) $%

%.i : %.c
	-@ echo ''
	-@ $(RM) $@
	   $(CPP) $(CPPFLAGS) $(CFLAGS) $< > $@

%.d : %.c
	-@ echo ''
	-@ $(CPP) $(CPPFLAGS) $(CFLAGS) $< | \
	   sed -n -e 's|^# 1 "\([^/].*\.h\)".*|\1|p' | \
	   sort -u

%.o : %.f
	-@ echo ''
	   $(FC) $(FFLAGS) -c $<

run_% : %
	-@ echo ''
	-@ $(TIMER)
	 @ if [ '$(MODE)' = interactive -o '$(VALGRIND)' ] ; then \
	     printf 'Press <CR> to run $<: ' ; \
	     read DUMMY ; \
	     if [ '$<' = tunits ] ; then \
	       $(VALGRIND) ./$< < test/units_test ; \
	     else \
	       $(VALGRIND) ./$< ; \
	     fi ; \
	   else \
	     if [ '$(filter $<, $(TEST_N) $(TEST_C))' ] ; then \
	       if [ '$<' = tunits ] ; then \
	         ./$< < ../C/test/units_test > $<.out 2>&1 ; \
	       else \
	         ./$< < /dev/null > $<.out 2>&1 ; \
	       fi ; \
	       if grep 'PASS:' $<.out > /dev/null ; then \
	         head -2 $<.out ; \
	         grep 'PASS:' $<.out ; \
	       elif [ -f 'test/$<.out' ] ; then \
	         trap 'rm -f run_test.tmp' 0 1 2 3 15 ; \
	         sed -e 's/0x[0-9a-f][0-9a-f][0-9a-f]*/0x<address>/g' $<.out > \
	           run_test.tmp ; \
	         mv -f run_test.tmp $<.out ; \
	         if cmp -s $<.out test/$<.out ; then \
	           head -2 $<.out ; \
	           echo 'PASS: Output agrees with Fortran/test/$<.out' ; \
	         else \
	           cat $<.out ; \
	           echo '' ; \
	           echo 'FAIL: Output disagrees with Fortran/test/$<.out' ; \
	         fi ; \
	       else \
	         cat $<.out ; \
	       fi ; \
	     else \
	       ./$< < /dev/null 2>&1 ; \
	     fi ; \
	   fi
	-@ echo ''

# Static and static pattern rules
#--------------------------------

.PHONY : build check clean cleaner cleanest distclean install lib realclean \
         test tests

build : lib

lib : $(WCSLIB) $(SHRLIB)

$(WCSLIB) ::
	-@ echo ''
	   $(MAKE) -C ../C lib

$(WCSLIB) :: $(MODULES:%=$(WCSLIB)(%))
	-@ echo ''
	   $(RANLIB) $(WCSLIB)

$(SHRLIB) : $(PICLIB)
	-@ echo ''
	-@ $(RM) -r tmp
	   mkdir tmp && \
	     cd tmp && \
	     trap 'cd .. ; $(RM) -r tmp' 0 1 2 3 15 ; \
	     $(AR) x ../$(PICLIB) && \
	     $(SHRLD) $(LDFLAGS) -o $(@F) *.o && \
	     mv $(@F) ../../C

$(PICLIB) : $(MODULES:%.o=$(PICLIB)(%.o)) ;

install : build
	$(MAKE) -C ../C install
	$(INSTALL) -m 444 *.inc $(INCDIR)

clean :
	-  $(RM) -r *.o *.i a.out t*.out core fort.* *.dSYM $(EXTRA_CLEAN)

cleaner : clean
	-  $(RM) .gdb_history
	-  $(RM) $(TEST_N)
	-  $(RM) $(TEST_P) tpih2 twcstab
	-  $(RM) tofits pih.fits

cleanest distclean realclean : cleaner
	-  $(RM) $(WCSLIB) $(PICLIB) $(SHRLIB)

check test : tests $(TESTS:%=run_%)

tests : $(TESTS)

$(TEST_N) : % : test/%.f $(WCSLIB)
	-@ echo ''
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(WCSLIB) $(LIBS)
	-@ $(RM) $@.o

$(TEST_P) : % : test/%.f $(WCSLIB)
	-@ echo ''
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(WCSLIB) $(PGPLOTLIB) $(LIBS)
	-@ $(RM) $@.o

tpih2 : % : test/%.f $(PGSBOXLIB) $(WCSLIB)
	-@ echo ''
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< $(PGSBOXLIB) $(WCSLIB) \
	     $(PGPLOTLIB) $(LIBS)
	-@ $(RM) $@.o

twcstab : test/twcstab.f getwcstab_f.o getwcstab.inc $(FGETWCSTAB) \
    $(WCSLIB)
	-@ echo ''
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< getwcstab_f.o \
	     $(FGETWCSTAB) $(CFITSIOLIB) $(WCSLIB) $(LIBS)
	-@ $(RM) $@.o

getwcstab_f.o : getwcstab_f.c getwcstab.h
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(CFLAGS) $(CFITSIOINC) -c $<

../C/getwcstab.o ::
	-@ echo ''
	   $(MAKE) -C ../C getwcstab.o

$(PGSBOXLIB) :
	-@ echo ''
	   $(MAKE) -C ../pgsbox lib

tofits : ../C/test/tofits.c
	   $(CC) $(CFLAGS) -o $@ $<

pih.fits : ../C/test/pih.keyrec tofits
	   sed '/^BADKEYREC/q' $< | ./tofits > $@

GNUmakefile : ../makedefs ;

../makedefs ../wcsconfig.h ../wcsconfig_f77.h : makedefs.in wcsconfig.h.in \
    wcsconfig_f77.h.in ../config.status
	-@ $(RM) ../wcsconfig.h ../wcsconfig_f77.h
	   cd .. && ./config.status

show ::
	-@ echo '  MODULES     := $(MODULES)'

# Dependencies (use the %.d pattern rule to list them)
#-----------------------------------------------------

$(WCSLIB)(cel_f.o)      : cel.h prj.h wcserr.h wcsconfig_f77.h
$(WCSLIB)(fitshdr_f.o)  : fitshdr.h wcsconfig.h wcsconfig_f77.h wcsutil.h
$(WCSLIB)(getwcstab_f.o): getwcstab.h wcsconfig_f77.h
$(WCSLIB)(lin_f.o)      : lin.h wcsconfig_f77.h wcserr.h
$(WCSLIB)(log_f.o)      : log.h wcsconfig_f77.h
$(WCSLIB)(prj_f.o)      : prj.h wcsconfig_f77.h wcserr.h
$(WCSLIB)(spc_f.o)      : spc.h spx.h wcsconfig_f77.h wcserr.h wcsutil.h
$(WCSLIB)(sph_f.o)      : sph.h wcsconfig_f77.h
$(WCSLIB)(spx_f.o)      : spx.h wcsconfig_f77.h wcserr.h
$(WCSLIB)(tab_f.o)      : tab.h wcsconfig_f77.h wcserr.h
$(WCSLIB)(wcs_f.o)      : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h \
                          wcsconfig_f77.h wcserr.h wcsutil.h
$(WCSLIB)(wcsfix_f.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h \
                          wcsconfig_f77.h wcserr.h wcsfix.h
$(WCSLIB)(wcshdr_f.o)   : cel.h lin.h prj.h spc.h spx.h tab.h wcs.h \
                          wcsconfig_f77.h wcserr.h wcshdr.h
$(WCSLIB)(wcsunits_f.o) : wcsconfig_f77.h wcserr.h wcsunits.h wcsutil.h

tcel1   : cel.inc prj.inc
tfitshdr: fitshdr.inc wcshdr.inc
tlin    : lin.inc
tlog    : log.inc
tpih1   : wcs.inc wcsfix.inc wcshdr.inc
tpih2   : wcs.inc wcshdr.inc
tprj1   : prj.inc
tprj2   : prj.inc
tspc    : spc.inc spx.inc
tspx    : spx.inc
ttab1   : tab.inc
ttab2   : tab.inc
ttab3   : prj.inc tab.inc
tunits  : wcsunits.inc
twcs    : cel.inc prj.inc wcs.inc wcserr.inc wcsmath.inc
twcsfix : wcs.inc wcsfix.inc wcsunits.inc
twcsmix : cel.inc lin.inc prj.inc wcs.inc
twcssub : wcs.inc
twcstab : getwcstab.inc wcs.inc wcsfix.inc wcshdr.inc

run_tfitshdr run_tpih1 run_tpih2: pih.fits
run_twcstab: ../C/wcstab.fits
