# 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=../..
ObjModuleType=std

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

CFLAGS := $(CFLAGS) -I$(ROOT)/gen/firebird/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 := $(GEN_ROOT)/firebird/lib
export DYLD_LIBRARY_PATH
else
LD_LIBRARY_PATH := $(GEN_ROOT)/firebird/lib
export LD_LIBRARY_PATH
endif
GPRE_FLAGS=	-r -m -z -n

EXAMPLES_DEST=	$(GEN_ROOT)/examples
EXAMPLES_SRC=	$(ROOT)/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
INTLEMP_DB=	$(EXAMPLES_DEST)/intlemp.fdb

.PHONY: all examples

all: examples

# examples: $(EMPLOYEE_DB) $(INTLEMP_DB) $(FIREBIRD)/examples/README
examples: $(EMPLOYEE_DB) $(FIREBIRD)/examples/README

$(FIREBIRD)/examples/README:
	$(CP) $(ROOT)/examples/readme $(FIREBIRD)/examples/README
	$(CP) $(ROOT)/examples/*.* $(FIREBIRD)/examples/
	$(CP) $(ROOT)/examples/api/*.* $(FIREBIRD)/examples/api/
#	$(CP) $(ROOT)/examples/build_unix/*.* $(FIREBIRD)/examples/build_unix/
#	$(CP) $(ROOT)/examples/build_win32/*.* $(FIREBIRD)/examples/build_win32/
	$(CP) $(ROOT)/examples/dyn/*.* $(FIREBIRD)/examples/dyn/
#	$(CP) $(ROOT)/examples/empbuild/*.* $(FIREBIRD)/examples/empbuild/
	$(CP) $(ROOT)/examples/include/*.* $(FIREBIRD)/examples/include/
	$(CP) $(ROOT)/examples/stat/*.* $(FIREBIRD)/examples/stat/
	$(CP) $(ROOT)/examples/udf/*.* $(FIREBIRD)/examples/udf/
	$(CP) employee.fdb $(FIREBIRD)/examples/empbuild/
#	$(CP) intlemp.fdb $(FIREBIRD)/examples/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


$(EXAMPLES_DEST)/employee.fdb:	$(EXAMPLES_DEST)/empbuild$(EXEC_EXT) $(INPUT_Sources) $(EXAMPLES_DEST)/isql$(EXEC_EXT)
	-$(RM) employee.fdb
	./empbuild employee.fdb
	-$(CHMOD_6) employee.fdb

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

$(EXAMPLES_DEST)/isql$(EXEC_EXT):
	-$(LN) $(ISQL_STATIC) $(EXAMPLES_DEST)/isql$(EXEC_EXT)

$(EXAMPLES_DEST)/empbuild$(EXEC_EXT): $(EMPBLD_Objects) $(COMMON_Objects) $(LIBFBSTATIC_A) 
	$(EXE_LINK) $(LINK_OPTS) $(EMPBLD_Objects) $(COMMON_Objects) -o $@ -L$(LIB) -lfbstatic $(LINK_LIBS) $(ICU_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_Objects) $(LIBFBSTATIC_A) 
	$(EXE_LINK) $(LINK_OPTS) $(INTLBLD_Objects) $(COMMON_Objects) -o $@ -L$(LIB) -lfbstatic $(LINK_LIBS) $(ICU_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) $^ $@
