# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
# You may obtain a copy of the Licence at
# http://www.gnu.org/licences/lgpl.html
#
# As a special exception this file can also be included in modules
# with other source code as long as that source code has been
# released under an Open Source Initiative certificed licence.
# More information about OSI certification can be found at:
# http://www.opensource.org
#
# This module 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 Lesser General Public Licence for more details.
#
# This module was created by members of the firebird development
# team.  All individual contributions remain the Copyright (C) of
# those individuals and all rights are reserved.  Contributors to
# this file are either listed below or can be obtained from a CVS
# history command.
#
#  Created by:  Mark O'Donohue <mark.odonohue@ludwig.edu.au>
#
#  Contributor(s):
#
#
#
ROOT=$(shell cd ../..; pwd)

# Add the install include directory to the search path since the
# examples need to build using those headers values.

CFLAGS := $(CFLAGS) -I$(FB_BUILD)/include


include     $(ROOT)/gen/make.defaults
ifdef SFIO_EXAMPLES
include     $(ROOT)/gen/make.platform.solaris.examples
else
include     $(ROOT)/gen/make.platform
endif
include     $(ROOT)/gen/make.rules
include     $(ROOT)/gen/make.shared.variables

@SET_MAKE@

PATH := ./:$(BIN):$(PATH)
export PATH

#ifeq ($(PLATFORM),DARWIN)
#DYLD_LIBRARY_PATH := $(DYLD_LIBRARY_PATH):$(GEN_ROOT)/firebird/lib
#export DYLD_LIBRARY_PATH
#else
#LD_LIBRARY_PATH := $(LD_LIBRARY_PATH):$(GEN_ROOT)/firebird/lib
#export LD_LIBRARY_PATH
#endif
GPRE_FLAGS=	-m -z -n

LIB_LINK_RPATH = $(call LIB_PLATFORM_RPATH,$(if $(subst intl,,$(1)),$(LIB),$(LIB)/../intl))
LINK_DARWIN_RPATH = -Wl,-rpath,@loader_path/../$(TARGET)/firebird
LIB_LINK_MAPFILE =

EXAMPLES_DEST=	$(GEN_ROOT)/examples
EXAMPLES_SRC=	$(ROOT)/examples
EXAMPLES_FB=	$(FIREBIRD)/examples

EMPBLD_Objects=	$(EXAMPLES_DEST)/empbuild.o

INTLBLD_Objects= $(EXAMPLES_DEST)/intlbld.o

INPUT_Files   =	empddl.sql empdml.sql indexoff.sql indexon.sql \
		job.inp lang.inp proj.inp qtr.inp
INPUT_Sources =   $(addprefix $(EXAMPLES_DEST)/, $(INPUT_Files))

INTL_Files    =	intlddl.sql intldml.sql indexoff.sql indexon.sql \
		job.inp lang.inp proj.inp qtr.inp
INTL_Sources =   $(addprefix $(EXAMPLES_DEST)/, $(INTL_Files))

EMPLOYEE_DB=	$(EXAMPLES_DEST)/employee.fdb
FINAL_EMPDB=	$(EXAMPLES_FB)/empbuild/employee.fdb
INTLEMP_DB=	$(EXAMPLES_DEST)/intlemp.fdb

EXTAUTH_PLUGIN=	$(EXAMPLES_FB)/prebuilt/libfbSampleExtAuth.$(SHRLIB_EXT)

.PHONY: all examples

all: examples

# examples: $(EMPLOYEE_DB) $(INTLEMP_DB) $(EXAMPLES_FB)/README
examples:	$(FINAL_EMPDB) $(EXAMPLES_FB)/README $(EXTAUTH_PLUGIN)

$(EXTAUTH_PLUGIN):	$(EXAMPLES_FB)/extauth/Makefile
	$(MAKE) -C $(EXAMPLES_FB)/extauth OUT=$(EXAMPLES_FB)/prebuilt INTERMED=$(TMP_ROOT)/examples/extauth

$(EXAMPLES_FB)/extauth/Makefile:
ifeq ($(TOMCRYPT_BUILD_FLG),Y)
	mkdir -p $(EXAMPLES_FB)/extauth/tomcrypt.include
	$(CP) $(ROOT)/extern/libtomcrypt/src/headers/* $(EXAMPLES_FB)/extauth/tomcrypt.include
endif
	tar cf - -C $(ROOT)/examples/extauth . | tar xf - -C $(EXAMPLES_FB)/extauth/

$(FINAL_EMPDB):	$(EMPLOYEE_DB)
	$(RM) $(FINAL_EMPDB)
	$(CP) $(EMPLOYEE_DB) $(FINAL_EMPDB)

$(EXAMPLES_FB)/README:
	$(CP) $(ROOT)/examples/*.* $(EXAMPLES_FB)/
	$(CP) $(ROOT)/examples/api/*.* $(EXAMPLES_FB)/api/
	$(CP) $(ROOT)/examples/dbcrypt/*.* $(EXAMPLES_FB)/dbcrypt/
	$(CP) $(ROOT)/examples/include/*.* $(EXAMPLES_FB)/include/
	$(CP) $(ROOT)/examples/interfaces/*.* $(EXAMPLES_FB)/interfaces/
	$(CP) $(ROOT)/examples/package/*.* $(EXAMPLES_FB)/package/
	$(CP) $(ROOT)/examples/stat/*.* $(EXAMPLES_FB)/stat/
	$(CP) $(ROOT)/examples/udf/*.* $(EXAMPLES_FB)/udf/
	$(CP) $(ROOT)/examples/udr/*.* $(EXAMPLES_FB)/udr/
	$(CP) $(ROOT)/examples/object_pascal/*.* $(EXAMPLES_FB)/object_pascal/
	$(CP) $(ROOT)/examples/object_pascal/[mM]ake* $(EXAMPLES_FB)/object_pascal/
	$(CP) $(ROOT)/examples/object_pascal/common/*.* $(EXAMPLES_FB)/object_pascal/common/
#	$(CP) intlemp.fdb $(EXAMPLES_FB)/empbuild/
	$(CP) $(ROOT)/examples/readme $(EXAMPLES_FB)/README
	$(CP) $(ROOT)/examples/empbuild/employe2.sql $(EXAMPLES_FB)/empbuild/

$(EXAMPLES_DEST)% : $(EXAMPLES_SRC)%
	$(CP) $^ $@

# Ok the following little story is, first build empbuild.fdb an empty db
# using a script file.  Then we can compile the empbuild.e file, then we
# can run it to create employee.fdb database populated with data by the
# program.  Thats how I found it.
# MOD July-2001
#
# Another problem here is that empbuild does not fail when it gets an error
# so MAKE thinks it's all run ok.  empbuild should be modified to return an
# error when it fails.
# MOD 28-July-2002


$(EMPLOYEE_DB):	$(EXAMPLES_DEST)/empbuild$(EXEC_EXT) $(INPUT_Sources) $(EXAMPLES_DEST)/isql$(EXEC_EXT)
	-$(RM) $(EMPLOYEE_DB)
	./empbuild $(EMPLOYEE_DB)
	$(GFIX) -write sync $(EMPLOYEE_DB)
	-$(CHMOD_6) $(EMPLOYEE_DB)

# To get past the fact isql is called from the programs, we create a local link in this directory

$(EXAMPLES_DEST)/isql$(EXEC_EXT):
	-$(RM) $(EXAMPLES_DEST)/isql$(EXEC_EXT)
	$(LN) $(ISQL) $(EXAMPLES_DEST)/isql$(EXEC_EXT)

$(EXAMPLES_DEST)/empbuild$(EXEC_EXT): $(EMPBLD_Objects) $(COMMON_LIB)
	$(EXE_LINK) $(EXE_LINK_OPTIONS) $(LSB_UNDEF) $^ -o $@ -L$(LIB) $(FIREBIRD_LIBRARY_LINK) $(LINK_LIBS)

$(EXAMPLES_DEST)/empbuild.c:	$(EXAMPLES_DEST)/empbuild.fdb $(EXAMPLES_DEST)/empbuild.e

$(EXAMPLES_DEST)/empbuild.fdb : $(EXAMPLES_DEST)/empddl.sql $(EXAMPLES_DEST)/empbld.sql $(EXAMPLES_DEST)/isql$(EXEC_EXT)
	-$(RM) $(EXAMPLES_DEST)/empbuild.fdb
	$(EXAMPLES_DEST)/isql$(EXEC_EXT) -i empbld.sql

# The chain for intlemp.fdb is the same a script file to create an empty database
# to allow a .e program to be compiled, to then create and populate with data
# the intlemp.fdb database.

$(EXAMPLES_DEST)/intlemp.fdb:	$(EXAMPLES_DEST)/intlbld$(EXEC_EXT) $(INTL_Sources) $(EXAMPLES_DEST)/isql$(EXEC_EXT)
	-$(RM) intlemp.fdb
	./intlbld intlemp.fdb
	-$(CHMOD_6) intlemp.fdb

$(EXAMPLES_DEST)/intlbld$(EXEC_EXT): $(INTLBLD_Objects) $(COMMON_LIB)
	$(EXE_LINK) $(EXE_LINK_OPTIONS) @^ -o $@ -L$(LIB) $(FIREBIRD_LIBRARY_LINK) $(LINK_LIBS)

$(EXAMPLES_DEST)/intlbld.c:	$(EXAMPLES_DEST)/intlbuild.fdb $(EXAMPLES_DEST)/intlbld.e

$(EXAMPLES_DEST)/intlbuild.fdb : $(EXAMPLES_DEST)/intlddl.sql $(EXAMPLES_DEST)/intlbld.sql $(EXAMPLES_DEST)/isql$(EXEC_EXT)
	-$(RM) intlbuild.fdb
	$(EXAMPLES_DEST)/isql$(EXEC_EXT) -i intlbld.sql


$(EXAMPLES_DEST)/%.sql: $(EXAMPLES_SRC)/empbuild/%.sql
	$(CP) $^ $@

$(EXAMPLES_DEST)/%.inp: $(EXAMPLES_SRC)/empbuild/%.inp
	$(CP) $^ $@

$(EXAMPLES_DEST)/%.e: $(EXAMPLES_SRC)/empbuild/%.e
	$(CP) $^ $@

$(EXAMPLES_DEST)/%.h: $(EXAMPLES_SRC)/common/%.h
	$(CP) $^ $@
