#  
# NrrdIO: stand-alone code for basic nrrd functionality
# Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
# Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
#
# This software is provided 'as-is', without any express or implied
# warranty.  In no event will the authors be held liable for any
# damages arising from the use of this software.
#
# Permission is granted to anyone to use this software for any
# purpose, including commercial applications, and to alter it and
# redistribute it freely, subject to the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must
#    not claim that you wrote the original software. If you use this
#    software in a product, an acknowledgment in the product
#    documentation would be appreciated but is not required.
#
# 2. Altered source versions must be plainly marked as such, and must
#    not be misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source distribution.
#

### The process of creating the NrrdIO source distribution from a Teem
### source distribution is automated, and is controlled by this GNU
### makefile:
###
### for the NrrdIO in ITK:
###
###    svn update
###    rm -f itk_NrrdIO_mangle.h
###    touch itk_NrrdIO_mangle.h
###    make -f pre-GNUmakefile clean
###    setenv ITK_NRRDIO
###    make -f pre-GNUmakefile
###    unsetenv ITK_NRRDIO
###    make -f sample-GNUmakefile
###    perl mangle.pl itk >! itk_NrrdIO_mangle.h
###    make -f sample-GNUmakefile clean
###
### for the NrrdIO in Teem:
###
###    svn update
###    make -f pre-GNUmakefile clean
###    make -f pre-GNUmakefile
###    make -f sample-GNUmakefile
###    make -f sample-GNUmakefile clean
###
### This file is the single place where the required Teem source files
### are listed.  Once the NrrdIO source distribution is created, this
### GNU makefile is completely moot.  TEEM_SRC_ROOT must be set in order
### to locate the Teem source.  We use this instead of TEEM_ROOT to avoid
### conflict with Teem's own make system

$(if $(TEEM_SRC_ROOT),,\
$(warning *)\
$(warning * Environment variable TEEM_SRC_ROOT not set. This)\
$(warning * needs to be set to the directory containing the)\
$(warning * src and include directories of the Teem source)\
$(warning * distribution.)\
$(warning *)\
$(error Make quitting))

### The following sources are pretty much stubs, to create the symbols, 
### but not the functionality of different formats and the bzip2 encoding.
### As such, they are NOT copied from Teem but are made for NrrdIO.
###
NEED_NOT = encodingBzip2.c formatEPS.c formatPNG.c formatPNM.c \
  formatText.c formatVTK.c

### These are still needed for exact same functionality as in Teem
###
TEEM_HDRS = $(addprefix teem/, \
  teem32bit.h teemDio.h teemEndian.h teemPng.h teemQnanhibit.h)

### NEED_{AIR,BIFF,NRRD}: the source files from teem that we need
### NEED_HDRS: the headers that we need
###
NEED_AIR = $(addprefix air/, \
  754.c mop.c array.c parseAir.c dio.c \
  sane.c endianAir.c string.c enum.c miscAir.c)
NEED_BIFF = $(addprefix biff/, biffbiff.c biffmsg.c)
NEED_NRRD = $(addprefix nrrd/, \
  accessors.c defaultsNrrd.c enumsNrrd.c arraysNrrd.c methodsNrrd.c \
  reorder.c axis.c simple.c comment.c keyvalue.c endianNrrd.c \
  parseNrrd.c gzio.c read.c write.c format.c formatNRRD.c \
  encoding.c encodingRaw.c encodingAscii.c encodingHex.c encodingGzip.c \
  subset.c)
NEED_SRCS = $(NEED_AIR) $(NEED_BIFF) $(NEED_NRRD)
NEED_PUB_HDRS = air/air.h biff/biff.h \
  $(addprefix nrrd/, nrrdDefines.h nrrdEnums.h nrrdMacros.h nrrd.h)
NEED_PRIV_HDRS = air/privateAir.h nrrd/privateNrrd.h

### Building NrrdIO requires the teem*.h headers, and the processed sources
### from air, biff, and nrrd
###
.PHONY: all
all: $(TEEM_HDRS) $(NEED_SRCS) $(NEED_PRIV_HDRS) NrrdIO.h $(ITK_SRC_ROOT)/Utilities/itk_zlib.h NrrdIO_Srcs.txt

### NrrdIO.h is basically the result of cat'ing together all the 
### teem headers in $(NEED_HDRS), but we do need to "unteem" them.
###
$(NEED_PUB_HDRS):
	tail --lines=+22 $(TEEM_SRC_ROOT)/src/$@ \
         | perl unteem.pl \
         | grep -v HAS_BEEN_INCLUDED > $(notdir $@)
$(NEED_PRIV_HDRS):
	tail --lines=+22 $(TEEM_SRC_ROOT)/src/$@ \
         | cat preamble.c - \
         | perl unteem.pl \
         | grep -v HAS_BEEN_INCLUDED > $(notdir $@)
NrrdIO.h: $(NEED_PUB_HDRS)
	cat preamble.c $(notdir $(NEED_PUB_HDRS)) > NrrdIO.h
	rm -f $(notdir $(NEED_PUB_HDRS))

### NrrdIO_Srcs.txt is a list of all the source files that must be
### compiled together to create libNrrdIO
###
NrrdIO_Srcs.txt:
	echo $(notdir $(NEED_SRCS)) $(NEED_NOT) > $@

### The teem*.h headers: we need only change the pre-amble here
###
teem/%.h: 
	tail --lines=+22 $(TEEM_SRC_ROOT)/include/$(notdir $@) \
          | cat preamble.c - > $(notdir $@)

### The rest: by design, these targets have the library names as part
### of the name (e.g. nrrd/simple.c) and that allows us to locate the
### source file without any VPATH games
###
%.h %.c:
	tail --lines=+22 $(TEEM_SRC_ROOT)/src/$@ \
          | cat preamble.c - \
          | perl unteem.pl \
          | grep -v HAS_BEEN_INCLUDED > $(notdir $@)

### To start from scratch
###
clean:
	rm -f $(notdir $(TEEM_HDRS) $(NEED_SRCS) \
                       $(NEED_PUB_HDRS) $(NEED_PRIV_HDRS)) \
              NrrdIO.h NrrdIO_Srcs.txt \
              *.o libNrrdIO.a sampleIO
