#
# MTtcl
#

# Directory prefix for file install.
# Modify to suit your installation.  By default files are installed in
# ../install relative to this Makefile.
# The default TCL_LIBRARY is set from this value (see CONFIGURE_FLAGS).
#
prefix:sh = cd .. ; echo `pwd`/install

INSTALL_TOP = $(prefix)

# Location of tk.h and libtk respectively
#
TK_DIR = /doe/dts/tcl/include
TK_BIN_DIR = /doe/dts/tcl/lib
TK_RUNPATH = -R$(TK_BIN_DIR)

# Top of X11 install tree
# X include files should be in $(X_DIR)/include
#
X_DIR = /usr/openwin

# Uncomment the following two lines to link mttclsh and mtwish statically
# with libtcl.  The executable and its memory footprint will be larger,
# but you won't have to worry about RUNPATH or LD_LIBRARY_PATH.  If
# static linking is used, TCL_RUNPATH should _not_ be set.
#
#B_STATIC = -Bstatic
#B_DYNAMIC = -Bdynamic
 
# The Location of MT-sturdy tcl.h and libtcl relative to the tcl_thread
# and examples directories.
#
TCL_DIR = ../tcl7.4
TCL_BIN_DIR = ../tcl7.4

# The run-path for mttclsh and mtwish are set from TCL_RUNPATH; this should
# be the install directory of libtcl.so.7.  If static linking is used,
# TCL_RUNPATH should _not_ be set.
#
TCL_RUNPATH = -R$(INSTALL_TOP)/lib

# Flags given to configure
# Configure is run on the tcl7.4 source after it has been patched.
# By default the prefix is set to $(INSTALL_TOP)
#
CONFIGURE_FLAGS = --prefix=$(INSTALL_TOP)

# --- Should not need to modify anything below this line

MAKE_FLAGS = \
	INSTALL_TOP=$(INSTALL_TOP) \
	TCL_DIR=$(TCL_DIR) \
	TCL_BIN_DIR=$(TCL_BIN_DIR) \
	TCL_RUNPATH=$(TCL_RUNPATH) \
	TK_DIR=$(TK_DIR) \
	TK_BIN_DIR=$(TK_BIN_DIR) \
	TK_RUNPATH="$(TK_RUNPATH)" \
	X_DIR=$(X_DIR)

all: mtpatch tclconfig tcl tcl_thread

tcl: always
	@echo "Making in tcl7.4"
	@(cd tcl7.4 ; make $(MAKE_FLAGS))

tcl_thread: always
	@echo "Making in tcl_thread"
	@(cd tcl_thread ; make $(MAKE_FLAGS))

mtpatch: tcl7.4 tcl7.4/tclAsync.c.orig

tcl7.4/tclAsync.c.orig:
	@echo "Applying MT-sturdy patch to Tcl7.4"
	@(cd tcl7.4 ; patch < ../mtsturdy.patch)
	@(cd tcl7.4 ; if [ -f *.rej ]; then \
		echo "" ; \
		echo "*** Some patches failed to apply." ; \
		echo "*** Patches in the following files will need to be applied by hand." ; \
		echo *.rej ; \
		echo "" ; \
		exit 1 ; \
	fi )

tcl7.4:
	@echo ""
	@echo "*** Could not find Tcl7.4 source tree."
	@echo "*** Extract the tcl7.4 archive into this directory."
	@echo ""
	@exit 1

tclconfig: tcl7.4 tcl7.4/config.log

tcl7.4/config.log:
	@echo "Configuring in tcl7.4"
	@(cd tcl7.4 ; ./configure $(CONFIGURE_FLAGS))


#
# Example stuff
#

EXAMPLES_MAKE_FLAGS = \
	INSTALL_TOP=$(INSTALL_TOP) \
	TCL_DIR=../tcl7.4 \
	TCL_BIN_DIR=../tcl7.4 \
	TCL_RUNPATH=-R../tcl7.4 \
	TK_DIR=$(TK_DIR) \
	TK_BIN_DIR=$(TK_BIN_DIR) \
	TK_RUNPATH="$(TK_RUNPATH)" \
	TCL_THREAD_DIR=../tcl_thread \
	TCL_THREAD_RUNPATH=-R../tcl_thread \
	X_DIR=$(X_DIR)

examples: always
	@echo "Making examples (using build libraries)"
	@(cd examples ; make $(EXAMPLES_MAKE_FLAGS))

examples_installed: always
	@echo "Making examples (using installed libraries)"
	@(cd examples ; make TCL_THREAD_DIR=../tcl_thread $(MAKE_FLAGS))

always:

#
# Install stuff
#

MAN_DIR =	$(INSTALL_TOP)/man
MANN_DIR =	$(INSTALL_TOP)/man/mann
MAN1_DIR =	$(INSTALL_TOP)/man/man1

install:
	@echo "Installing MT-sturdy Tcl7.4"
	@(cd tcl7.4 ; make install $(MAKE_FLAGS))
	@echo "Installing Tcl threads"
	@(cd tcl_thread ; make install $(MAKE_FLAGS))


install-man:
	@for i in $(MAN_DIR) $(MANN_DIR) $(MAN1_DIR) ; \
	    do \
	    if [ ! -d $$i ] ; then \
		echo "Making directory $$i"; \
		mkdir $$i; \
		chmod 755 $$i; \
	    fi; \
	done;
	@cd man; for i in *.n; \
	do \
	    echo "Installing man/$$i"; \
	    rm -f $(MANN_DIR)/$$i; \
	    sed -e '/mttcl\.macros/r mttcl.macros' -e '/mttcl\.macros/d' \
		    $$i > $(MANN_DIR)/$$i; \
	    chmod 444 $(MANN_DIR)/$$i; \
	    done;
	@cd man; for i in *.1; \
	do \
	    echo "Installing man/$$i"; \
	    rm -f $(MAN1_DIR)/$$i; \
	    sed -e '/mttcl\.macros/r mttcl.macros' -e '/mttcl\.macros/d' \
		    $$i > $(MAN1_DIR)/$$i; \
	    chmod 444 $(MAN1_DIR)/$$i; \
	    done;



#
# MTtcl external release rules
#

MTTCL_WS:sh =	while [ ! -d Codemgr_wsdata -a ! -d MTtcl1.0 ] ; do cd .. ; done ; pwd
MTTCL = 	$(MTTCL_WS)/mttcl

RELEASE_DIR=$(MTTCL_WS)/MTtcl1.0
LICENSE_TERMS=$(MTTCL_WS)/mttcl/license.terms

release: release_dir release_copy release_man release_tcl_thread release_examples release_doc
	@rm -f $(RELEASE_DIR)/license.terms
	cp $(LICENSE_TERMS) $(RELEASE_DIR)
	@rm -f $(RELEASE_DIR)/man/license.terms
	cp $(LICENSE_TERMS) $(RELEASE_DIR)/man
	@rm -f $(RELEASE_DIR)/tcl_thread/license.terms
	cp $(LICENSE_TERMS) $(RELEASE_DIR)/tcl_thread
	@rm -f $(RELEASE_DIR)/examples/license.terms
	cp $(LICENSE_TERMS) $(RELEASE_DIR)/examples

release_dir:
	@mkdir -p $(RELEASE_DIR)

release_copy:
	@for f in `cat release.files` ; do \
	    echo $$f ; \
	    rm -f $(RELEASE_DIR)/$$f ; \
	    cp -p $$f $(RELEASE_DIR) ; \
	done;
	(cd $(RELEASE_DIR) ; chmod 444 `cat $(MTTCL_WS)/mttcl/release.files` ; chmod 644 Makefile)

release_man:
	@(cd man ; make release RELEASE_DIR=$(RELEASE_DIR))

release_tcl:
	@(cd tcl7.4 ; make release RELEASE_DIR=$(RELEASE_DIR))

release_tcl_thread:
	@(cd tcl_thread ; make release RELEASE_DIR=$(RELEASE_DIR) MTTCL_WS=$(MTTCL_WS))

release_examples:
	@(cd examples ; make release RELEASE_DIR=$(RELEASE_DIR) MTTCL_WS=$(MTTCL_WS))

release_doc:
	@(cd doc ; make release RELEASE_DIR=$(RELEASE_DIR) MTTCL_WS=$(MTTCL_WS))

makepatch:
	rm -f patch.tmp
	diff -c tcl7.4/ tcl_patched/ | grep -v '^Only' | grep -v '^Common' > patch.tmp

.INIT: Makefile


