#-----------------------------------------------------------------------------
# GNU makefile for building the WCSLIB 4.4 FORTRAN wrappers.
#
# Summary of the main targets
# ---------------------------
#   build:     Build the library
#
#   clean:     Delete intermediate object files.
#
#   cleaner:   clean, and also delete the test executables.
#
#   realclean (or cleanest): cleaner, and also delete the object library.
#
#   check (or test): Compile and run the test programs.  By default they are
#              executed in batch mode; use
#
#                gmake MODE=interactive check
#
#              to run them interactively.
#
#   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.4 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.4.1.2 2009/08/10 23:56:20 cal103 Exp cal103 $
#-----------------------------------------------------------------------------
# Get configure settings.
include ../makedefs

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

MODULES := cel_f.o \
           fitshdr_f.o \
           lin_f.o \
           log_f.o \
           prj_f.o \
           spc_f.o \
           sph_f.o \
           spx_f.o \
           tab_f.o \
           wcs_f.o \
           wcsfix_f.o \
           wcshdr_f.o \
           wcsunits_f.o

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

# 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 do require PGPLOT.
TEST_P := tspc tprj2 tcel1 ttab2 ttab3 twcsmix

TESTS  := $(TEST_N)
ifneq "$(PGPLOTINC)" ""
ifneq "$(PGPLOTLIB)" ""
  TESTS   += $(TEST_P) tpih2
endif
endif

# Test programs that require CFITSIO.
ifneq "$(CFITSIOINC)" ""
ifneq "$(CFITSIOLIB)" ""
TESTS  += twcstab
endif
endif

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


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

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

$(PICLIB)(%.o) : %.c
	-@ echo ''
	   $(CC) $(CPPFLAGS) $(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 ] ; then \
	     printf 'Press <CR> to run $<: ' ; \
	     read DUMMY ; \
	     if [ '$<' = tunits ] ; then \
	       ./$< < ../C/test/units_test ; \
	     else \
	       ./$< ; \
	     fi ; \
	   else \
	     if [ '$<' = tunits ] ; then \
	       ./$< < ../C/test/units_test ; \
	     elif [ '$<' != twcshdr ] ; then \
	       ./$< < /dev/null 2>&1 ; \
	     fi ; \
	   fi
	-@ echo ''

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

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

build : lib

lib : $(LIBWCS) $(SHRLIB)

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

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

$(SHRLIB) : $(PICLIB)
	-@ echo ''
	-@ $(RM) -r tmp
	   mkdir tmp
	   cd tmp && \
	     $(AR) x ../$(PICLIB) && \
	     $(SHRLD) -o $(@F) *.o && \
	     mv $(@F) ../../C
	-  $(RM) -r tmp

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

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

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

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

cleanest realclean : cleaner
	- $(RM) $(LIBWCS) $(PICLIB) $(SHRLIB)

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

tests : $(TESTS)

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

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

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

twcstab : test/twcstab.f getwcstab_f.o getwcstab.inc $(FGETWCSTAB) \
    $(LIBWCS)
	-@ echo ''
	   $(FC) $(FFLAGS) $(LDFLAGS) -o $@ $< getwcstab_f.o \
	     $(FGETWCSTAB) $(CFITSIOLIB) $(LIBWCS) $(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

# Dependencies
#-------------

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

tcel1   : cel.inc prj.inc
tfitshdr: fitshdr.inc pih.fits
tlin    : lin.inc
tlog    : log.inc
tpih1   : wcs.inc wcshdr.inc pih.fits
tpih2   : wcs.inc wcshdr.inc pih.fits
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
twcsfix : wcs.inc
twcsmix : cel.inc lin.inc prj.inc wcs.inc
twcssub : cel.inc prj.inc wcs.inc
twcstab : wcs.inc wcshdr.inc

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