#
# Copyright 2005, 2006 University of Leiden.
#
# This file is part of MIA+EWS.
#
# MIA+EWS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# MIA+EWS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MIA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Makefile for the OIR C routines in EWS and MIA
#
# $Id: makec,v 1.9 2006/08/29 15:17:50 koehler Exp $
# Updated to include wvgoff (HP) w. jaffe 2003/12/03
#
# Last Change: Tue Aug 29 16:27:22 2006

# directories relative to the CVS tree
BINDIR = ../bin
OBJDIR = ../object
INCDIR = ../include
LIBDIR = ../lib

# location of CFITSIO
CFITSIO_BASE = ../../cfitsio
CFITSIO_INC  = $(CFITSIO_BASE)
CFITSIO_LIB  = $(CFITSIO_BASE)


# compiler
CC = gcc $(CCEXT)

# compilation options
INC_OPTS = -I$(CFITSIO_INC) -I$(INCDIR) 
LIB_OPTS = -L$(CFITSIO_LIB) -L$(LIBDIR) -L$(OBJDIR) 
ifeq ($(OSTYPE),solaris)
   LIB_OPTS = -L$(CFITSIO_LIB) -L$(LIBDIR) -L$(OBJDIR) -lsocket -lnsl 
endif

ifeq ($(CPUTYPE),x86_64)
   CC_OPT32 = -m32
else
   CC_OPT32 = 		# unless overridden on cmd line
endif

CC_OPTS = -W -pedantic -std=c99 $(CC_OPT32)

# libraries needed for all executables
BASELIBS = -lEWS -lcfitsio -lm 

# compilation pattern for executables
%: %.c
	$(CC) -o $(BINDIR)/$(notdir $@) $(CC_OPTS) $(INC_OPTS) $(LIB_OPTS) $< $(BASELIBS)

# compilation pattern for objects
%.o: %.c
	$(CC) -c -o $(OBJDIR)/$(notdir $@) $(CC_OPTS) $(INC_OPTS) $<
# Tell make file to search for exec sources in this directory
vpath %.c exec
vpath %.o $(OBJDIR)
vpath % $(BINDIR)
#sources
sources = $(wildcard *.c)
# executables
EXECUTABLES = oir1dCompressData oirAutoFlag oirAverageVis oirCalibrateVis\
	      oirChopPhotometry oirChopPhotoImages oirMakePhotoSpectra\
	      oirCompressData oirFormFringes oirGroupDelay oirRedCal\
	      oirRotateGroupDelay oirRotateInsOpd oirWaveCal\
	      oirMakeMask oirMeanRMS oirSci2HiPhot midiChopimg\
	      oirCrossCoeff oirRescaleSpectra oirShiftMask

all: lib exec
# utility library
$(LIBDIR)/libEWS.a: 
#ar rc $(LIBDIR)/$@ $(addprefix $(OBJDIR)/,$(notdir $^))
	ar rc $(LIBDIR)/$@ $(addprefix $(OBJDIR)/,$(notdir $^))
lib: sources
	ar rc $(LIBDIR)/libEWS.a $(OBJDIR)/*.o
	ranlib $(LIBDIR)/libEWS.a
sources: $(sources)
	$(CC) -c  $(CC_OPTS) $(INC_OPTS) $(sources)
	mkdir -p $(OBJDIR)
	mkdir -p $(LIBDIR)
	mkdir -p $(BINDIR)
	mv *.o $(OBJDIR)
exec: $(EXECUTABLES) 

# exception from the pattern def'ed above
midiChopimg: midiChopimg.o midiMultifile.o
	$(CC) -o $(BINDIR)/$(notdir $@) $(CC_OPTS) $(INC_OPTS) $(LIB_OPTS) $(OBJDIR)/midiChopimg.o $(OBJDIR)/midiMultifile.o $(BASELIBS)
#was:	$(CC) -o midiChopimg midiChopimg.o midiMultifile.o $(LIB) $(EXTRA_LIBS)

clean:
	rm -f $(OBJDIR)/*.o $(LIBDIR)/*.a $(BINDIR)/* || exit 0
