# FITS TCL makefile.in
# $Id: Makefile.in,v 1.19 2015/02/26 16:54:22 irby Exp $

#----------------------------------------------------------------------------
# Definitions:
#----------------------------------------------------------------------------
LIBRARY		= fitstcl

CFILES 		= fitsInit.c fitsUtils.c fitsTcl.c fitsCmds.c fitsIO.c fvTcl.c
SHARED_CFILE	= tclShared.c

OBJECTS		= ${CFILES:.c=.o}
LOCAL_OBJ	= ${OBJECTS} ${SHARED_CFILE:.c=.o}

CFITSIO_OBJ_TMP	= ${shell if [ -f ${CFITSIO_DIR}/Makefile ]; then ${MAKE} -f ${CFITSIO_DIR}/Makefile cfitsioLibObjs; fi | grep buffers}
CFITSIO_OBJ	= ${shell if [ "x${CFITSIO_OBJ_TMP}" != "x" ]; then echo ${CFITSIO_OBJ_TMP}; fi | sed "s: : ${CFITSIO_DIR}/:g" | sed "s:^:${CFITSIO_DIR}/:"}

SHARED_OBJ	= ${LOCAL_OBJ} ${CFITSIO_OBJ}

STATIC_LIB	= lib${LIBRARY}.a
SHARED_LIB	= lib${LIBRARY}${SHLIB_SUFFIX}

IFLAGS		= -I${CFITSIO_DIR} -I${TCL_INC_PATH} -I./

#----------------------------------------------------------------------------
# Configurable macros:
#----------------------------------------------------------------------------
INSTALLDIR	= @prefix@
CC		= @CC@
RANLIB		= @RANLIB@
CFLAGS		= @CFLAGS@
DEFS		= @DEFS@
C_LIB_OPTION	= @C_LIB_OPTION@
SHLIB_SUFFIX	= @SHLIB_SUFFIX@
SHLIB_LD 	= @SHLIB_LD@
LD_FLAGS 	= @LD_FLAGS@

TCL_INC_PATH	= @TCL_INC_PATH@
TCL_LIB_PATH	= @TCL_PATH@
TCL_LIB		= @TCL_LIB@
CFITSIO_DIR	= @CFITSIODIR@
SHLIB_LD_LIBS 	= @LIBS@ @SHLIB_LD_LIBS@

#----------------------------------------------------------------------------
# Targets:
#----------------------------------------------------------------------------
all: ${C_LIB_OPTION}

static: ${STATIC_LIB}

shared: ${SHARED_LIB}

${STATIC_LIB}: ${OBJECTS}
	rm -f ${STATIC_LIB}
	ar cr ${STATIC_LIB} ${OBJECTS}
	${RANLIB} ${STATIC_LIB}

${SHARED_LIB}: build-cfitsio ${LOCAL_OBJ}
	@if [ "x${TCL_LIB_PATH}" = x -o "x${TCL_LIB}" = x ]; then \
		echo "${SHLIB_LD} ${LD_FLAGS} ${SHARED_OBJ} ${SHLIB_LD_LIBS} -o ${SHARED_LIB}"; \
		${SHLIB_LD} ${LD_FLAGS} ${SHARED_OBJ} ${SHLIB_LD_LIBS} \
			-o ${SHARED_LIB}; \
	else \
		echo "${SHLIB_LD} ${LD_FLAGS} ${SHARED_OBJ} ${SHLIB_LD_LIBS} -L${TCL_LIB_PATH} -l${TCL_LIB} -lz -o ${SHARED_LIB}"; \
		${SHLIB_LD} ${LD_FLAGS} ${SHARED_OBJ} ${SHLIB_LD_LIBS} \
			 -L${TCL_LIB_PATH} -l${TCL_LIB} -lz -o ${SHARED_LIB}; \
	fi

build-cfitsio:
	@if [ "x${CFITSIO_DIR}" != x ]; then \
	    if [ -d "${CFITSIO_DIR}" ]; then \
		if [ ! -f ${CFITSIO_DIR}/buffers.o ]; then \
			echo "Configuring and building in ${CFITSIO_DIR}"; \
			cd ${CFITSIO_DIR}; ./configure; \
			${MAKE} stand_alone "CC=${CC}" "CFLAGS=${CFLAGS}" \
               			"FITSIO_OBJ=" "FITSIO_SRC="; \
		fi; \
	    else \
		echo "CFITSIO_DIR='${CFITSIO_DIR}' does not exist!"; \
		exit 1; \
	    fi; \
	else \
	    echo "CFITSIO_DIR was not set!"; \
	    exit 1; \
	fi

install: all
	@if [ "x${C_LIB_OPTION}" = xstatic ]; then \
		echo "cp ${STATIC_LIB} ${INSTALLDIR}/lib/"; \
		cp ${STATIC_LIB} ${INSTALLDIR}/lib/; \
	else \
		echo "cp ${SHARED_LIB} ${INSTALLDIR}/lib/"; \
		cp ${SHARED_LIB} ${INSTALLDIR}/lib/; \
		echo "cp pkgIndex.tcl ${INSTALLDIR}/lib/"; \
		cp pkgIndex.tcl ${INSTALLDIR}/lib/; \
	fi

clean:
	-rm -rf ${LOCAL_OBJ} ${STATIC_LIB} ${SHARED_LIB}

distclean: clean
	-rm -f config.cache config.status config.log Makefile so_locations

#----------------------------------------------------------------------------
# Make target which outputs the list of the .o contained in the fitsTcl lib
# useful to build a single big shared library containing Tcl/Tk and other
# extensions.  used for the Tcl Plugin. 
#----------------------------------------------------------------------------
fitstclLibObjs:
	@echo ${OBJECTS}


.c.o:
	${CC} -c -o ${<D}/${@F} ${CFLAGS} ${DEFS} ${IFLAGS} $<
