# ---------------------------------------------------------------------
# Linux Makefile for FreeAdhocUDF
# This file is part of FreeAdhocUDF.
# ---------------------------------------------------------------------
# FreeAdhocUDF - copyright (c) 2004 - 2009 adhoc dataservice GmbH
# Christoph Theuring / Peter Mandrella / Georg Horn
# <http://www.udf.adhoc-data.de> eMail <udf@adhoc-data.de>
# based on FreeUDFLibC - copyright (c) 1999 Gregory Deatz
# changes by adhoc dataservice GmbH
# ---------------------------------------------------------------------
#
# ---------------------------------------------------------------------
# InterBase/FireBird Installation Directory
# uncommend to use /include and /lib form installed InterBase/FireBird
# and for make install copy FreeAdhocUDF.so to the $(DBDIR)/UDF/ directory
# ---------------------------------------------------------------------
#DBDIR=/opt/firebird
#DBDIR=/usr/local/firebird
#DBDIR=/opt/interbase
#DBDIR=/usr/local/interbase
# ----------------------------------------------------------------------
# uncomment your choice of the DB-system
# for InterBase you can choose the newest version for all ib versions
# for FireBird 1.x you must choose firebird_1_5
# for FireBird 2.x you can choose the newest firebird 2.x version
# for FireBird 2.x amd64 you can chosse the newest firebird 2.x_64 version
# ----------------------------------------------------------------------
#DBSYSTEM=interbase_6_0_2
#DBSYSTEM=interbase_7_1
#DBSYSTEM=interbase_7_5
#DBSYSTEM=interbase_2007
#DBSYSTEM=interbase_2009
#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 (interbase, $(findstring interbase, $(DBSYSTEM)))
  FAU=FreeAdhocUDF_ib.so
endif
ifeq (firebird_1_5, $(findstring firebird_1_5, $(DBSYSTEM)))
  FAU=FreeAdhocUDF_fb15.so
endif
ifeq (firebird_2_, $(findstring firebird_2_, $(DBSYSTEM)))
  ifeq (_amd64, $(findstring _amd64, $(DBSYSTEM)))
    FAU=FreeAdhocUDF_fb2x64.so
  else
    FAU=FreeAdhocUDF_fb2x.so
  endif 
endif
# ----------------------------------------------------------------------
# Location of header files and libraries
# ----------------------------------------------------------------------
# set the directory for the associated libraries
ifeq (/, $(findstring /, $(DBDIR)))
  LIB=$(DBDIR)/lib
else
  ifeq (_amd64, $(findstring _amd64, $(DBSYSTEM)))
     LIB=../linux/$(subst _amd64,,$(DBSYSTEM))_amd64/lib64  
  else
     LIB=../linux/$(DBSYSTEM)/lib
  endif   
endif   
# set the directory for the associated header files
ifeq (/, $(findstring /, $(DBDIR)))
  INCLUDE=$(DBDIR)/include
else
  ifeq (_amd64, $(findstring _amd64, $(DBSYSTEM)))
     INCLUDE=../linux/$(subst _amd64,,$(DBSYSTEM))_amd64/include  
  else
     INCLUDE=../linux/$(DBSYSTEM)/include
  endif   
endif   
# ----------------------------------------------------------------------
# set compiler and linker options
# ----------------------------------------------------------------------
# set the associated clientlib to the DB-system
# gds for InterBase, fbclient for FireBird   
# ----------------------------------------------------------------------
ifeq (firebird, $(findstring firebird, $(DBSYSTEM)))		
  CLIENTLIB=fbclient
else 
  CLIENTLIB=gds
endif
# ----------------------------------------------------------------------
# potential set flag for a 32 bit FreeAdhocUDF compile on a 64 bit system
# ----------------------------------------------------------------------
# point to the 32bit libraries on a 64bit system
LIB32=/usr/lib32
ifneq ($(wildcard i*86), $(findstring $(wildcard i*86), $(shell uname -a)))
	SYSTEM='the system is 64bit'
  ifneq (_x64, $(findstring _x64, $(DBSYSTEM)))
    CF=-m32
    LF=-mi386linux -L$(LIB32)
	  FREEADHOCUDF='the FreeAdhocUDF is 32bit'
  else
    CF=-m64
    LF=
	  FREEADHOCUDF='the FreeAdhocUDF is 64bit'
  endif  
else
	SYSTEM='the system is 32bit'
  ifneq (_amd64, $(findstring _amd64, $(DBSYSTEM)))
	  FREEADHOCUDF='the FreeAdhocUDF is 32bit'
    CF=
    LF=    
  else
    $(error no 64bit compiling on a 32bit system!)
  endif
endif
# ---------------------------------------------------------------------
# Compiler and linker Defines
# ---------------------------------------------------------------------
CC=gcc
CFLAGS=-Wall -fpic -O -c $(CF) -I$(INCLUDE)
LD=ld
LDFLAGS=-G -Bsymbolic $(LF) -L$(LIB) -lm -lc -lib_util 
# ---------------------------------------------------------------------
# Generic Compilation Rules
# ---------------------------------------------------------------------
.c.o:
	$(CC) $< $(CFLAGS) -o $@
# ---------------------------------------------------------------------
# The UDF objects
# ---------------------------------------------------------------------
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  diverse_functions.o
  
all: $(FAU)

$(FAU): $(UDF_OBJs)
	$(LD) $(UDF_OBJs) -o $@ $(LDFLAGS) -l$(CLIENTLIB)
  
clean:
	rm -f *.o *.so *.bak *.~* core
	@echo ----------
	@echo clean done
	@echo ----------
    
new:	clean depend all
	@echo -------------------------------
	@echo chosen $(DBSYSTEM)
	@echo libs are located $(LIB)
	@echo headers are located $(INCLUDE)
	@echo clientlib is $(CLIENTLIB)
	@echo $(SYSTEM)
	@echo $(FREEADHOCUDF)
	@echo libname is $(FAU)
	@echo ------------------------------

install: all
	cp $(FAU) $(DBDIR)/UDF/FreeAdhocUDF.so

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
