# ---------------------------------------------------------------------
# MacOSX Firebird Makefile for FreeAdhocUDF
# This file is part of FreeAdhocUDF.
# ---------------------------------------------------------------------
# FreeAdhocUDF - copyright (c) 2004 - 2009 adhoc dataservice GmbH
# Christoph Theuring / Peter Mandrella / Georg Horn / Paul Beach
# <http://www.udf.adhoc-data.de> eMail <udf@adhoc-data.de>
# based on FreeUDFLibC - copyright (c) 1999 Gregory Deatz
# changes by adhoc dataservice GmbH
# ---------------------------------------------------------------------
#
#----------------------------------------------------------------------
# Set the architecture PowerPC or Intel
#----------------------------------------------------------------------
#ARCH=ppc
ARCH=x86
# ---------------------------------------------------------------------
# FireBird Installation Directory
# FreeAdhocUDF.so is copied to the $(DBDIR)/UDF/ directory
# only for "make install" 
# ---------------------------------------------------------------------
DBDIR=/Library/Frameworks/Firebird.framework/Resources/English.lproj/var
# ----------------------------------------------------------------------
# uncomment your choice of the DB-system
# for FireBird 1.x you must choose firebird_1_5
# for FireBird 2.x you must choose firebird_2_1
# for FireBird64 2.x you must choose firebird_2_1_64  
# ----------------------------------------------------------------------
# For Firebird 1.5.5 and below the FreeAdhocUDF must not have a dylib
# extension for it to work. This problem will be fixed in the next release
# of Firebird 1.5.6

#DBSYSTEM=firebird_1_5_5
#DBSYSTEM=firebird_2_0_4
#DBSYSTEM=firebird_2_0_4_amd64
#DBSYSTEM=firebird_2_1_1
#DBSYSTEM=firebird_2_1_1_amd64
DBSYSTEM=firebird_2_5_a1
#DBSYSTEM=firebird_2_5_a1_amd64

# ----------------------------------------------------------------------
# set the output name
# ----------------------------------------------------------------------
ifeq (firebird_1_5, $(DBSYSTEM))
  FAU=FreeAdhocUDF_fb15.dylib
endif
ifeq (firebird_2_, $(findstring firebird_2_, $(DBSYSTEM)))
  ifeq (_64, $(findstring _64, $(DBSYSTEM)))
    FAU=FreeAdhocUDF_fb2x64.dylib
  else
    FAU=FreeAdhocUDF_fb2x.dylib
  endif 
endif
  
# ----------------------------------------------------------------------
# Location of header files and libraries
# ----------------------------------------------------------------------
# set the directory for the associated libraries
# Firebird 1.5.5 must be linked with ib_util.c
# This will be fixed in Firebird 1.5.6.
LIB=-framework firebird -L/Library/Frameworks/Firebird.Framework/Versions/A/Libraries
# Firebird 1.5.5
#LIB=-framework firebird
# set the directory for the associated header files
INCLUDE=/Library/Frameworks/Firebird.framework/Headers

# ----------------------------------------------------------------------
# set architecture and/or set flag options on MacOSX 
# ----------------------------------------------------------------------
	ifneq (_amd64, $(findstring _amd64, $(DBSYSTEM))) 
	ifeq ($(ARCH),x86)
	CF=-arch i386
	FREEADHOCUDF='the FreeAdhocUDF is intel 32bit'
	endif
	endif
	
	ifneq (_amd64, $(findstring _amd64, $(DBSYSTEM))) 
	ifeq ($(ARCH),ppc) 
	CF=-arch ppc
	FREEADHOCUDF='the FreeAdhocUDF is powerpc 32bit'
	endif
	endif
	
	ifeq (_amd64, $(findstring _amd64, $(DBSYSTEM))) 
	ifeq ($(ARCH),x86)
	CF=-arch x86_64
	FREEADHOCUDF='the FreeAdhocUDF is intel 64bit'
	endif
	endif
	
	ifeq (_amd64, $(findstring _amd64, $(DBSYSTEM))) 
	ifeq ($(ARCH),ppc)	
	CF=-arch ppc64
	FREEADHOCUDF='the FreeAdhocUDF is powerpc 64bit'
	endif
	endif  
 
# ---------------------------------------------------------------------
# Compiler and linker Defines
# UDF libraries on MacOSX must be bundles to work.
# ---------------------------------------------------------------------
CC=gcc
CFLAGS=-Wall -fPIC -DMACOSX -O -c $(CF) -I$(INCLUDE)
LD=ld
LDFLAGS=$(CF) $(LIB) -bundle -flat_namespace -undefined suppress -lib_util
# Firebird 1.5.5
#LDFLAGS=$(CF) $(LIB) -bundle -flat_namespace -undefined suppress 
# ---------------------------------------------------------------------
# Generic Compilation Rules
# ---------------------------------------------------------------------

.c.o:
	$(CC) $< $(CFLAGS) -o $@

# ---------------------------------------------------------------------
# The UDF objects
# ---------------------------------------------------------------------
# ib_util.o can be removed when Firebird on MacOSX can link against
# a dynamic library version of libib_util.dylib
UDF_OBJs = date_functions.o math_functions.o string_functions.o \
	blob_functions.o md5c.o sysdep.o uuid.o uuid_functions.o \
	const_functions.o conv_functions.o ib_util.o diverse_functions.o

all: $(FAU)

$(FAU): $(UDF_OBJs)
	$(LD) $(UDF_OBJs) -o $@ $(LDFLAGS) 
  
clean:
	rm -f *.o *.so *.bak *.~* core
  
new:	clean depend all
	@echo chosen $(DBSYSTEM)
	@echo libs are located $(LIB)
	@echo headers are located $(INCLUDE)
	@echo $(SYSTEM)
	@echo $(FREEADHOCUDF)
	@echo libname is $(FAU)

install: all
ifeq (firebird_1_5, $(DBSYSTEM))
	cp $(FAU) $(DBDIR)/UDF/FreeAdhocUDF
else
	cp $(FAU) $(DBDIR)/UDF/FreeAdhocUDF.dylib
endif

depend:
	cat Makefile | sed '/^# DO NOT DELETE/,$$d' > Makefile.tmp
	mv Makefile.tmp Makefile
	chmod 777 Makefile
	echo "# DO NOT DELETE THIS LINE - MAKE DEPEND DEPENDS ON IT" >> Makefile
	echo "" >> Makefile
	for i in *.c; do gcc $(CFLAGS) -E -MM $$i >> Makefile; done
	@echo -----------
	@echo depend done
	@echo -----------

# DO NOT DELETE THIS LINE - MAKE DEPEND DEPENDS ON IT
