#
# This file is a Makefile for JS9.  If it has the name "Makefile.in"
# then it is a template for a Makefile;  to generate the actual Makefile,
# run "./configure", which is a configuration script generated by the
# "autoconf" program (constructs like "@foo@" will get replaced in the
# actual Makefile.
#

PACKAGE =		@PACKAGE_NAME@
VERSION =		@PACKAGE_VERSION@
DISTNAME =		${PACKAGE}-${VERSION}
DDISTNAME =		${PACKAGE}-data

# code shared with astroem
JSFITSIO =	../astroem/jsfitsio
WRAPPERS =	../astroem/wrappers

#----------------------------------------------------------------
# Things you can change to personalize the Makefile for your own
# site (you can make these changes in either Makefile.in or
# Makefile, but changes to Makefile will get lost if you re-run
# the configuration script).
#----------------------------------------------------------------

# Default top-level directories in which to install architecture-
# specific files (exec_prefix) and machine-independent files such
# as scripts (prefix).  The values specified here may be overridden
# at configure-time with the --exec-prefix and --prefix options
# to the "configure" script.

prefix =		@prefix@
exec_prefix =		@exec_prefix@

# The following definition can be set to non-null for special systems
# like AFS with replication.  It allows the pathnames used for installation
# to be different than those used for actually reference files at
# run-time.  INSTALL_ROOT is prepended to $prefix and $exec_prefix
# when installing files.
INSTALL_ROOT =

# Directory in which to install the .a or .so binaries
LIB_INSTALL_DIR =	$(INSTALL_ROOT)$(exec_prefix)/lib

# Directory in which to install the program wish:
BIN_INSTALL_DIR =	$(INSTALL_ROOT)$(exec_prefix)/bin

# Directory in which to install the include files
INCLUDE_INSTALL_DIR =	$(INSTALL_ROOT)$(prefix)/include

# Top-level directory for manual entries:
MAN_INSTALL_DIR =	$(INSTALL_ROOT)$(prefix)/man

# Directory in which to install manual entries
MAN1_INSTALL_DIR =	$(MAN_INSTALL_DIR)/man1

# Directory in which to install manual entries
# procedures:
MAN3_INSTALL_DIR =	$(MAN_INSTALL_DIR)/man3

# extra Libs required to link (e.g. socket libraries)
LIBS =            	@CFITSIOLIB@ @FUNLIB@ @EXTRA_LIBS@ -lm

# includes
INCS =			-I. @CFITSIOINC@ @FUNINC@ \
			-I./util -I./$(JSFITSIO) -I./$(WRAPPERS)

# To change the compiler switches, for example to change from -O
# to -g, change the following line:
CFLAGS = 		@CFLAGS@

# To add ld switches, change the following line:
LDFLAGS =		@LDFLAGS@

# Some versions of make, like SGI's, use the following variable to
# determine which shell to use for executing commands:
SHELL =			/bin/sh

# There are just too many different versions of "install" around;
# better to use the install-sh script that comes with the distribution,
# which is slower but guaranteed to work.

INSTALL = 		@srcdir@/../build/install-sh -c
INSTALL_PROGRAM =	${INSTALL}
INSTALL_DATA =		${INSTALL} -m 644

#----------------------------------------------------------------
# The information below is modified by the configure script when
# Makefile is generated from Makefile.in.  You shouldn't normally
# modify any of this stuff by hand.
#----------------------------------------------------------------

AC_FLAGS =	@DEFS@
RANLIB =	@RANLIB@

#----------------------------------------------------------------
# The information below should be usable as is.  The configure
# script won't modify it and you shouldn't need to modify it
# either.
#----------------------------------------------------------------
RM =		rm -rf
CP =		cp -p

CC =		@CC@

CC_SW =		 ${CFLAGS} ${AC_FLAGS} ${INCS}

DEPEND_SW = 	${CFLAGS} -I. ${AC_FLAGS}

# type of backend server (or none)
HELPER =	@HELPER@
USEHELPER =	@USEHELPER@

# which library
FITSLIB =	@FITSLIB@

SRCS =		js9helper.c

PROGS =		js9helper

INCLUDES =	js9helper.h

UTILLIB	=	libutil.a

ifeq ($(FITSLIB), cfitsio)
HELPEROBJS =	js9helper.o jsfitsio.o listhdu.o healpix.o
else
HELPEROBJS =	js9helper.o
endif

UTILOBJS = 	$(wildcard util/*.o)

all:		progs

ifeq ($(USEHELPER), yes)
progs:		$(PROGS)
else
progs:	
endif

js9helper:	$(UTILLIB) $(HELPEROBJS)
		$(CC) $(LDFLAGS) $(HELPEROBJS) -o $@ $(UTILLIB) $(LIBS)

$(UTILLIB):	$(UTILOBJS)
		@(cd util; \
		env CC="$(CC)" CFLAGS="$(CC_SW)" make; \
		mv libutil.a ..)

jsfitsio.o:	$(JSFITSIO)/jsfitsio.c
		$(CC) -c $(CC_SW) -o $@ $+

healpix.o:	$(JSFITSIO)/healpix.c
		$(CC) -c $(CC_SW) -o $@ $+

listhdu.o:	$(WRAPPERS)/listhdu.c
		$(CC) -c $(CC_SW) -o $@ $+

js9helper.o:	js9helper.c
		$(CC) -c $(CC_SW) -o $@ $+

install:	install-binaries

install-binaries: FORCE
	@(if [ x$(USEHELPER) = xyes ]; then \
	  echo "Installing programs ..."; \
	  for i in $(LIB_INSTALL_DIR) $(INCLUDE_INSTALL_DIR) $(BIN_INSTALL_DIR) ; \
	    do \
	    if [ ! -d $$i ] ; then \
		echo "making directory $$i"; \
		mkdir -p $$i; \
		chmod 755 $$i; \
		else true; \
		fi; \
	    done; \
	  for i in $(PROGS) ; \
	    do \
		if [ -f $$i.exe ] ; then \
		    echo "installing $$i.exe" ; \
		    $(INSTALL_PROGRAM) $$i.exe $(BIN_INSTALL_DIR)/$$i.exe ; \
		elif [ -f $$i ] ; then \
		    echo "installing $$i" ; \
		    $(INSTALL_PROGRAM) $$i $(BIN_INSTALL_DIR)/$$i ; \
		fi; \
	    done; \
	  else \
	    echo "nothing to install for 'binaries' (no helper configured)"; \
	  fi;)

clean:		FORCE
		@($(RM) *.a *.so *.dylib *.o *.exe core core.* errs *pure* \
			foo* *~ \#* TAGS *.E a.out errors \
			$(UTILLIB) $(PROGS) $(TESTPROGS) \
			util/*.a util/*.o \
			gmon.out *.pg *.bak \
			config.info config.log ltest \
			autom4te.cache a.out.dSYM tmp/*)

distclean:	clean
		$(RM) Makefile config.status config.cache config.log

maintainer-clean:: clean
		$(RM) config.status config.cache config.log

Makefile:	Makefile.in
		@(if [ -f config.status ] ; then 			\
			$(SHELL) config.status;      			\
		fi)

depend:		FORCE
		makedepend -- $(DEPEND_SW) -- $(SRCS)

errcheck:	FORCE
		@(egrep '[^x]error|warning|ld:|collect2:|make:' foo | \
		  egrep -v "^lex.*but not used"; true)

FORCE:

# DO NOT DELETE THIS LINE -- make depend depends on it.
