#
# @configure_input@
#

#========================================================================
# All these assignments are substituted by configure
#========================================================================

PKG_SOURCES	= @PKG_SOURCES@
PKG_OBJECTS	= @PKG_OBJECTS@

PKG_STUB_SOURCES = @PKG_STUB_SOURCES@
PKG_STUB_OBJECTS = @PKG_STUB_OBJECTS@

PKG_TEST_SOURCES = @PKG_TEST_SOURCES@
PKG_TEST_OBJECTS = @PKG_TEST_OBJECTS@

PKG_TCL_SOURCES = @PKG_TCL_SOURCES@

PKG_HEADERS	= @PKG_HEADERS@
PKG_PRIVATE_HEADERS=@PKG_PRIVATE_HEADERS@

PKG_LIB_FILE	= @PKG_LIB_FILE@
PKG_TEST_LIB_FILE = @PKG_TEST_LIB_FILE@
PKG_STUB_LIB_FILE = @PKG_STUB_LIB_FILE@

SHELL		= @SHELL@

srcdir		= @srcdir@
prefix		= @prefix@
exec_prefix	= @exec_prefix@

bindir		= @bindir@
libdir		= @libdir@
includedir	= @includedir@
datarootdir     = @datarootdir@
datadir		= @datadir@
mandir		= @mandir@

DESTDIR		=

INSTALL_OPTIONS =
INSTALL		= $(SHELL) $(srcdir)/tclconfig/install-sh -c ${INSTALL_OPTIONS}
INSTALL_DATA_DIR = ${INSTALL} -d -m 755
INSTALL_PROGRAM	= ${INSTALL} -m 555
INSTALL_DATA	= ${INSTALL} -m 444
INSTALL_SCRIPT	= ${INSTALL_PROGRAM}
INSTALL_LIBRARY	= ${INSTALL_DATA}

PACKAGE_NAME	= @PACKAGE_NAME@
PACKAGE_VERSION	= @PACKAGE_VERSION@
CC		= @CC@
CFLAGS_DEFAULT	= @CFLAGS_DEFAULT@
CFLAGS_WARNING	= @CFLAGS_WARNING@

CLEANFILES=@CLEANFILES@
EXEEXT=@EXEEXT@
LDFLAGS_DEFAULT=@LDFLAGS_DEFAULT@
MAKE_LIB=@MAKE_LIB@
MAKE_SHARED_LIB=@MAKE_SHARED_LIB@
MAKE_STATIC_LIB=@MAKE_STATIC_LIB@
MAKE_STUB_LIB=@MAKE_STUB_LIB@
MAKE_TEST_LIB=@MAKE_TEST_LIB@
OBJEXT=@OBJEXT@
RANLIB=@RANLIB@
RANLIB_STUB=@RANLIB_STUB@
SHLIB_CFLAGS=@SHLIB_CFLAGS@
SHLIB_LD=@SHLIB_LD@
SHLIB_LD_LIBS=@SHLIB_LD_LIBS@
STLIB_LD=@STLIB_LD@
TCL_SRC_DIR=@TCL_SRC_DIR@
TCL_BIN_DIR=@TCL_BIN_DIR@
# Not actually used, but can help when tracing errors
#TCL_LIBS=@TCL_LIBS@
#MT=@MT@
TCLSH_PROG=@TCLSH_PROG@
INCLUDES=@PKG_INCLUDES@ @TCL_INCLUDES@
PKG_CFLAGS=@PKG_CFLAGS@
DEFS=@DEFS@ $(PKG_CFLAGS)
CPPFLAGS=@CPPFLAGS@
LIBS=@PKG_LIBS@ @LIBS@ @MATH_LIBS@
PKG_TEST_LIBS=${PKG_STUB_LIB_FILE}
AR=@AR@
CFLAGS=@CFLAGS@
CYGPATH=@CYGPATH@
SDX=@SDX@
#RC=@RC@
#RCFLAGS=@RES_DEFS@
#RES=@RES_SUFFIX@
#ADD_MANIFEST=@ADD_MANIFEST@

EXTRA_PATH	= $(top_builddir):$(TCL_BIN_DIR)
PKG_ENV		= TCL_LIBRARY=`@CYGPATH@ $(TCL_SRC_DIR)/library` \
		  @LD_LIBRARY_PATH_VAR@="$(EXTRA_PATH):$(@LD_LIBRARY_PATH_VAR@)" \
		  PATH="$(EXTRA_PATH):$(PATH)" \
		  TCLLIBPATH="$(top_builddir)"
TCLSH_PROG	= @TCLSH_PROG@
TCLSH		= $(PKG_ENV) $(TCLSH_PROG)

#========================================================================
# None of these assignments are substituted by configure
#========================================================================

SRC_DIR=$(srcdir)
BINARIES=$(PKG_LIB_FILE) $(PKG_STUB_LIB_FILE) $(PKG_TEST_LIB_FILE)
PKG_DIR=$(PACKAGE_NAME)$(PACKAGE_VERSION)
PKG_KIT_ROOT=$(PKG_DIR)-$(PLATFORM)
PKG_KIT=$(PKG_KIT_ROOT).kit
pkglibdir=$(libdir)/$(PKG_DIR)
top_builddir=.
TCL_VERSION_REQ=8.5b1
GDB=gdb
VALGRIND=valgrind
CONFIG_CLEAN_FILES="Makefile tclooConfig.sh"
COMPILE=$(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
VPATH=$(SRC_DIR)/generic:$(SRC_DIR)/unix:$(SRC_DIR)/win:$(SRC_DIR)/macosx:$(SRC_DIR)
TESTFLAGS=
KIT_PKG_ROOT=$(PKG_KIT_ROOT).vfs/lib/$(PACKAGE_NAME)
ITERATIONS=1000
TCL_TOOLS_DIR=$(TCL_SRC_DIR)/tools

#========================================================================
# This is something that is found by executing a Tcl script
#========================================================================

PLATFORM:=$(shell echo 'package require platform;puts [platform::generic]' | $(TCLSH))

#========================================================================
# Master rules
#========================================================================

all: package libraries
package: $(PKG_LIB_FILE) pkgIndex.tcl
testpackage: $(PKG_TEST_LIB_FILE)
libraries: $(PKG_STUB_LIB_FILE)
install: all install-package install-libraries install-headers install-doc
kit: sdx_valid $(PKG_KIT)

#========================================================================
# Basic installation rules
#========================================================================

install-package: package
	@mkdir -p $(DESTDIR)$(pkglibdir)
	$(INSTALL_PROGRAM) $(PKG_LIB_FILE) $(DESTDIR)$(pkglibdir)/$(PKG_LIB_FILE)
	$(INSTALL_DATA) pkgIndex.tcl $(DESTDIR)$(pkglibdir)/pkgIndex.tcl
	$(INSTALL_DATA) tclooConfig.sh $(DESTDIR)$(pkglibdir)/tclooConfig.sh
#	for p in $(SRC_DIR)/library/*.tcl ; do \
#	    destp=`basename $$p`; \
#	    echo " Install $$destp $(DESTDIR)$(pkglibdir)/$$destp"; \
#	    $(INSTALL_DATA) $$p $(DESTDIR)$(pkglibdir)/$$destp; \
#	done
install-headers:
	@echo "Installing header files in $(DESTDIR)$(includedir)"
	@mkdir -p $(DESTDIR)$(includedir)
	@list='$(PKG_HEADERS)'; for p in $$list; do \
	    echo "Installing $(SRC_DIR)/$$p" ; \
	    destp=`basename $$p`; \
	    $(INSTALL_DATA) $(SRC_DIR)/$$p $(DESTDIR)$(includedir)/$$destp ; \
	done;
install-private-headers:
	@echo "Installing private header files in $(DESTDIR)$(includedir)"
	@mkdir -p $(DESTDIR)$(includedir)
	@list='$(PKG_PRIVATE_HEADERS)'; for p in $$list; do \
	    echo "Installing $(SRC_DIR)/$$p" ; \
	    destp=`basename $$p`; \
	    $(INSTALL_DATA) $(SRC_DIR)/$$p $(DESTDIR)$(includedir)/$$destp ; \
	done;
install-libraries: libraries
	@echo "Installing $(PKG_STUB_LIB_FILE) in $(DESTDIR)$(pkglibdir)"
	@mkdir -p $(DESTDIR)$(pkglibdir)
	$(INSTALL_DATA) $(PKG_STUB_LIB_FILE) $(DESTDIR)$(pkglibdir)

#========================================================================
# Install documentation. Unix manpages should go in the $(mandir) directory.
#========================================================================

install-doc: doc
	@echo "Installing documentation in $(DESTDIR)$(mandir)"
	@mkdir -p $(DESTDIR)$(mandir)/mann
	@list='$(SRC_DIR)/doc/*.n'; for i in $$list; do \
	    echo "Installing $$i"; \
	    rm -f $(DESTDIR)$(mandir)/mann/`basename $$i`; \
	    $(INSTALL_DATA) $$i $(DESTDIR)$(mandir)/mann ; \
	done
	@mkdir -p $(DESTDIR)$(mandir)/man3
	@list='$(SRC_DIR)/doc/*.3'; for i in $$list; do \
	    echo "Installing $$i"; \
	    rm -f $(DESTDIR)$(mandir)/man3/`basename $$i`; \
	    $(INSTALL_DATA) $$i $(DESTDIR)$(mandir)/man3 ; \
	done

# A do-nothing target at the moment
doc:

#========================================================================
# Run the test suite or benchmarks for the package, or just start a shell
# primed to load the package.
#========================================================================

test: package testpackage libraries
	@echo $(TCLSH_PROG) $(SRC_DIR)/tests/all.tcl $(TESTLOAD) $(TESTFLAGS)
	@$(TCLSH) `$(CYGPATH) $(SRC_DIR)/tests/all.tcl` $(TESTLOAD) $(TESTFLAGS)

benchmarks: package libraries
	@echo Benchmark Iterations = $(ITERATIONS)
	@list='$(SRC_DIR)/benchmarks/*.tcl'; for i in $$list; do \
	    echo "Benchmarking $$i"; \
	    time $(TCLSH) `$(CYGPATH) $$i` $(ITERATIONS); \
	done

shell: package libraries
	@echo $(TCLSH_PROG) $(SCRIPT)
	@$(TCLSH) $(SCRIPT)

valgrind: package libraries
	@echo $(VALGRIND) $(TCLSH_PROG) $(SCRIPT)
	@$(PKG_ENV) $(VALGRIND) $(TCLSH_PROG) $(SCRIPT)

valgrindtest: package libraries
	@echo $(VALGRIND) $(TCLSH_PROG) $(SRC_DIR)/tests/all.tcl $(TESTLOAD) $(TESTFLAGS)
	@$(PKG_ENV) $(VALGRIND) $(TCLSH_PROG) `$(CYGPATH) $(SRC_DIR)/tests/all.tcl` $(TESTLOAD) $(TESTFLAGS) -singleproc 1

valgrindshell: binaries libraries
	$(PKG_ENV) valgrind $(VALGRINDARGS) $(TCLSH_PROG) $(SCRIPT)

#========================================================================
# These rules say how to run tclsh in a hostile Windows environment, while
# still working on Unix.
#========================================================================

gdb.run:
	@echo "catch {puts \"target exec [file attributes [file normalize [info nameofexecutable]] -shortname]\";exit};puts \"target exec [file normalize [info nameofexecutable]]\"" | $(TCLSH) > $@
	@echo "set env TCL_LIBRARY=$(TCL_SRC_DIR)/library" >> $@
	@echo "set env LD_LIBRARY_PATH=$(EXTRA_PATH):$(LD_LIBRARY_PATH)" >> $@
	@echo "set env TCLLIBPATH=$(top_builddir)" >> $@
	@sh -c 'test x$${OSTYPE} = xmsys && echo "set new-console off" >> $@' || true

gdb: package libraries gdb.run
	@echo gdb $(TCLSH_PROG) $(ARGS)
	@gdb --command=gdb.run $(ARGS)
	@rm gdb.run
gdbtest: package libraries gdb.run
	@echo "set args `$(CYGPATH) $(SRC_DIR)/tests/all.tcl` $(TESTLOAD) $(TESTFLAGS) -singleproc 1" >>gdb.run
	@echo gdb $(TCLSH_PROG) $(MANGLED)
	@gdb --command=gdb.run
	@rm gdb.run

#========================================================================
# The rules for actually doing the build of the package.
#========================================================================

$(PKG_LIB_FILE): $(PKG_OBJECTS)
	-rm -f $(PKG_LIB_FILE)
	${MAKE_LIB}
	$(RANLIB) $(PKG_LIB_FILE)
$(PKG_STUB_LIB_FILE): $(PKG_STUB_OBJECTS)
	-rm -f $(PKG_STUB_LIB_FILE)
	${MAKE_STUB_LIB}
	$(RANLIB_STUB) $(PKG_STUB_LIB_FILE)
$(PKG_TEST_LIB_FILE): $(PKG_STUB_LIB_FILE)
	$(MAKE) PKG_TEST_LIB_FILE=dummy PKG_LIB_FILE=$(PKG_TEST_LIB_FILE) \
		PKG_OBJECTS="$(PKG_TEST_OBJECTS)" \
		LIBS="$(PKG_TEST_LIBS)" \
		CFLAGS="${SHLIB_CFLAGS} -DUSE_TCL_STUBS -DUSE_TCLOO_STUBS" \
		$(PKG_TEST_LIB_FILE)
.c.@OBJEXT@:
	$(COMPILE) -c `$(CYGPATH) $<` -o $@
.rc.$(RES):
	$(RC) $@ $(RCFLAGS) "$<"
pkgIndex.tcl: Makefile
	-@echo Creating pkgIndex.tcl
	@( \
	echo 'if {[catch {package require Tcl $(TCL_VERSION_REQ)}]} return'; \
	echo 'package ifneeded $(PACKAGE_NAME) $(PACKAGE_VERSION) \
	    [list load [file join $$dir $(PKG_LIB_FILE)] $(PACKAGE_NAME)]' \
	) > pkgIndex.tcl
Makefile: $(SRC_DIR)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) \
	  && CONFIG_FILES=$@ CONFIG_HEADERS= $(SHELL) ./config.status

#========================================================================
# How to rebuild the package's stub table.
#========================================================================

genstubs: $(TCL_TOOLS_DIR)/genStubs.tcl $(SRC_DIR)/generic/tclOO.decls
	@echo $(TCLSH_PROG) $(TCL_TOOLS_DIR)/genStubs.tcl $(SRC_DIR)/generic $(SRC_DIR)/generic/tclOO.decls
	@$(TCLSH) `$(CYGPATH) $(TCL_TOOLS_DIR)/genStubs.tcl` `$(CYGPATH) $(SRC_DIR)/generic` `$(CYGPATH) $(SRC_DIR)/generic/tclOO.decls`

#========================================================================
# Build a starkit - this is experimental!
#========================================================================

sdx_valid:
	@test "$(SDX)" != "none" || ( echo "Cannot build kit; no sdx!"; false )
$(PKG_KIT): $(PKG_LIB_FILE)
	-@mkdir -p $(KIT_PKG_ROOT)/$(PLATFORM)
	cp $(PKG_LIB_FILE) $(KIT_PKG_ROOT)/$(PLATFORM)
	-@echo Creating $(KIT_PKG_ROOT)/pkgIndex.tcl
	@( \
	echo 'if {[catch {package require Tcl $(TCL_VERSION_REQ)}]} return'; \
	echo 'package ifneeded $(PACKAGE_NAME) $(PACKAGE_VERSION)' \
	    '[list load [file join $$dir $(PLATFORM) $(PKG_LIB_FILE)] $(PACKAGE_NAME)]' \
	) > $(KIT_PKG_ROOT)/pkgIndex.tcl
	$(SDX) wrap $(PKG_KIT)
	-@rm -rf $(PKG_KIT_ROOT).vfs $(PKG_KIT_ROOT).bat

#========================================================================
# How to clean up after a build.
#========================================================================

clean:
	-test -z "$(BINARIES)" || rm -f $(BINARIES)
	-rm -f *.$(OBJEXT) core *.core
	-test -z "$(CLEANFILES)" || rm -f $(CLEANFILES)
distclean: clean
	-rm -f *.tab.c
	-rm -f $(CONFIG_CLEAN_FILES)
	-rm -f config.cache config.log config.status

#========================================================================
# Distribution creation (FROM sampleextension)
# You may need to tweak this target to make it work correctly.
#========================================================================

#COMPRESS	= tar cvf $(PKG_DIR).tar $(PKG_DIR); compress $(PKG_DIR).tar
COMPRESS	= tar zcvf $(PKG_DIR).tar.gz $(PKG_DIR)
DIST_ROOT	= /tmp/dist
DIST_DIR	= $(DIST_ROOT)/$(PKG_DIR)

dist-clean:
	rm -rf $(DIST_DIR) $(DIST_ROOT)/$(PKG_DIR).tar.*

dist: dist-clean
	mkdir -p $(DIST_DIR)
	cp -p $(srcdir)/ChangeLog $(srcdir)/README* $(srcdir)/license* \
		$(srcdir)/aclocal.m4 $(srcdir)/configure $(srcdir)/*.in \
		$(DIST_DIR)/
	chmod 664 $(DIST_DIR)/Makefile.in $(DIST_DIR)/aclocal.m4
	chmod 775 $(DIST_DIR)/configure $(DIST_DIR)/configure.in

	for i in $(srcdir)/*.[ch]; do \
	    if [ -f $$i ]; then \
		cp -p $$i $(DIST_DIR)/ ; \
	    fi; \
	done;

	mkdir $(DIST_DIR)/tclconfig
	cp $(srcdir)/tclconfig/install-sh $(srcdir)/tclconfig/tcl.m4 \
		$(DIST_DIR)/tclconfig/
	chmod 664 $(DIST_DIR)/tclconfig/tcl.m4
	chmod +x $(DIST_DIR)/tclconfig/install-sh

	list='demos generic library mac tests unix win'; \
	for p in $$list; do \
	    if test -d $(srcdir)/$$p ; then \
		mkdir $(DIST_DIR)/$$p; \
		cp -p $(srcdir)/$$p/*.* $(DIST_DIR)/$$p/; \
	    fi; \
	done
	mkdir $(DIST_DIR)/doc
	for p in $(srcdir)/doc/*.?; do \
	    soelim -I $(srcdir)/doc $$p >$(DIST_DIR)/doc/`basename $$p`; \
	done

	(cd $(DIST_ROOT); $(COMPRESS);)

#========================================================================
# Magic declarations to make.
#========================================================================

.SUFFIXES: .c .$(OBJEXT) .rc .$(RES)
.PHONY: all package clean depend distclean doc install libraries test kit
.PHONY: sdx_valid doc dist dist-clean benchmarks shell genstubs gdb gdbtest
.PHONY: valgrind valgrindtest testpackage
