#
# nlitcl-2.1
# Makefile for nlitcl with Extended Tcl
#
# Tom Poindexter 

###############################################################################
#
# set the following defines as needed: 


#------------------------------------------------------------------------------
# define where Tcl source and libraries are located, relative to nlitcl

TCL_DIR=../tcl7.3

#------------------------------------------------------------------------------
# define where Tk source and libraries are located, relative to nlitcl

TK_DIR=../tk3.6

#------------------------------------------------------------------------------
# define where Extended Tcl source and libraries are located, relative to nlitcl

TCLX_DIR=../tclX7.2a

#------------------------------------------------------------------------------
# define name for nlitcl tcl executable, could be "ntcl", "nlitcl", etc.
# I just use Extended Tcl's "tcl"

TCL_EXEC=tcl

#------------------------------------------------------------------------------
# define name for nlitcl wishx executable, could be "nwishx", "nliwishx", etc.
# I just use Extended Tcl's "wishx"

WISH_EXEC=wishx

#------------------------------------------------------------------------------
# include compile and link flags gleaned from Tcl and Tk Makefiles
# tclflags.mk is created by the target "get-tcl-flags:" (see below)
# the following flags should be set in tclflags.mk:
#
# from Tcl's Makefile:
# MATH_LIBS		# math libs needed to link tclsh
# AC_FLAGS		# compile time flags, for tcl.h, tclUnix.h
# PROTO_FLAGS		# use ansi function prototypes or not
# MEM_DEBUG_FLAGS	# use memory debug (ckalloc/ckfree)
# CFLAGS		# other $(CC) flags, e.g. -O or -g
#
# and from Tk's Makefile
# X11_INCLUDES		# where X11 includes are, if not /usr/include
# X11_LIB_SWITCHES	# what X11 libs and associated network libs, if any
# LIBS			# all libs needed to link wish

# Tk's Makefile uses TCL_BIN_DIR to find libtcl.a, use our TCL_DIR
TCL_BIN_DIR=$(TCL_DIR)

include tclflags.mk

#------------------------------------------------------------------------------
# get the SYSLIBS definition from TclX
# the SYSLIBS from TclX/tkmaster will be used in linking tcl and wishx

include $(TCLX_DIR)/tkmaster/src/SYSLIBS

#------------------------------------------------------------------------------
# define c compiler
#
CC=/usr/local/gcc/bin/gcc
# CC=cc

#------------------------------------------------------------------------------
# define where SWAN libraries are located

SWAN=/home/abacus/sybase/swan

NLIINC=$(SWAN)/include
NLILIB=$(SWAN)/lib/libnlc.a
NLICCOPTS=-I$(NLIINC)


#------------------------------------------------------------------------------
# where to copy the final executable and man page, probably the same
# place that TclX put it's files and name the man page

BIN_DIR=/usr/local/bin
MAN_DIR=/usr/local/man/mann
MAN_PAGE=nlitcl.n

#------------------------------------------------------------------------------
# define ranlib command for systems that need it; touch otherwise

RANLIB=ranlib
#RANLIB=touch

#------------------------------------------------------------------------------
# how to install

INSTALL=cp
#INSTALL=install -c
#INSTALL=ln -s

#
# end of defines
#
###############################################################################

# define all the compile flags
TCL_CFLAGS=$(AC_FLAGS) $(GENERIC_FLAGS) $(PROTO_FLAGS) $(MEM_DEBUG_FLAGS) \
	   $(CFLAGS)

all:	$(TCL_EXEC) $(WISH_EXEC) $(MAN_PAGE)

tclXAppInit.c:	$(TCLX_DIR)/tclmaster/src/tclXAppInit.c
	sed -f sybinit.sed <$(TCLX_DIR)/tclmaster/src/tclXAppInit.c \
			   >tclXAppInit.c

tkXAppInit.c:	$(TCLX_DIR)/tkmaster/src/tkXAppInit.c
	sed -f sybinit.sed <$(TCLX_DIR)/tkmaster/src/tkXAppInit.c \
			   >tkXAppInit.c

tclXAppInit.o:	tclXAppInit.c
	$(CC) -O -c -I$(TCLX_DIR)/tclmaster/include -I$(TCL_DIR) $(TCL_CFLAGS) \
	   tclXAppInit.c

tkXAppInit.o:	tkXAppInit.c
	$(CC) -O -c -I$(TCLX_DIR)/tclmaster/include \
		 -I$(TCLX_DIR)/tkmaster/include -I$(TK_DIR) -I$(TCL_DIR) \
		$(X11_INCLUDES) $(TCL_CFLAGS) tkXAppInit.c

nlitcl.o:	nlitcl.c libnlitcl.a
	$(CC) -O -Wall -c -g -I$(TCLX_DIR)/tclmaster/include -I$(TCL_DIR) $(NLICCOPTS) \
	   $(TCL_CFLAGS) nlitcl.c
	ar cr libnlitcl.a nlitcl.o
	-$(RANLIB) libnlitcl.a
	touch nlitcl.o

libnlitcl.a:	$(NLILIB)
	-mkdir libtmp
	-cd libtmp; rm -f *.o *.a
	cp $(NLILIB) libtmp
	cd libtmp; for L in lib*.a ; do ar x $$L; done
	cd libtmp; ar cr ../libnlitcl.a *.o
	-cd libtmp; rm -f *.o *.a

$(TCL_EXEC):	libnlitcl.a nlitcl.o tclXAppInit.o 
	$(CC) tclXAppInit.o libnlitcl.a -L$(TCLX_DIR)/tclmaster/lib \
	    -ltclx -ltcl $(SYSLIBS) -o $(TCL_EXEC)

$(WISH_EXEC):	libnlitcl.a nlitcl.o tkXAppInit.o
	$(CC) tkXAppInit.o libnlitcl.a \
	   -L$(TCLX_DIR)/tclmaster/lib -L$(TCLX_DIR)/tkmaster/lib \
	   -ltkx -ltk -ltclx -ltcl $(SYSLIBS) -o $(WISH_EXEC)

$(MAN_PAGE):	nlitcl.man
	nroff -man nlitcl.man >$(MAN_PAGE)

get-tcl-flags:
	egrep '^MATH_LIBS'         $(TCL_DIR)/Makefile    >tclflags.mk
	egrep '^AC_FLAGS'          $(TCL_DIR)/Makefile   >>tclflags.mk
	egrep '^PROTO_FLAGS'       $(TCL_DIR)/Makefile   >>tclflags.mk
	egrep '^MEM_DEBUG_FLAGS'   $(TCL_DIR)/Makefile   >>tclflags.mk
	egrep '^CFLAGS'            $(TCL_DIR)/Makefile   >>tclflags.mk
	egrep '^X11_INCLUDES'      $(TK_DIR)/Makefile    >>tclflags.mk
	egrep '^X11_LIB_SWITCHES'  $(TK_DIR)/Makefile    >>tclflags.mk
	egrep '^LIBS'              $(TK_DIR)/Makefile | \
	    sed -e 's@libtk.a@$(TK_DIR)/libtk.a@p'       >>tclflags.mk
	@echo "tclflags.mk made.  check that the following are defined:"
	@echo "MATH_LIBS  AC_FLAGS  PROTO_FLAGS  MEM_DEBUG_FLAGS  CFLAGS "
	@echo "X11_INCLUDES  X11_LIB_SWITCHES  LIBS"
	@echo
	@echo "tclflags.mk:"
	@cat   tclflags.mk
	@echo
	@echo  'if the flags are suitable, type "make" to build'

install:	$(TCL_EXEC) $(WISH_EXEC) $(MAN_PAGE)
	$(INSTALL) `pwd`/$(TCL_EXEC)  $(BIN_DIR)/$(TCL_EXEC)
	$(INSTALL) `pwd`/$(WISH_EXEC) $(BIN_DIR)/$(WISH_EXEC)
	$(INSTALL) `pwd`/$(MAN_PAGE)  $(MAN_DIR)/$(MAN_PAGE)

tests:	$(TCL_EXEC)
	./$(TCL_EXEC) -f nlitcl.test

clean:
	rm -f $(TCL_EXEC) $(WISH_EXEC) $(MAN_PAGE) nlitcl.o libnlitcl.a \
	      tclXAppInit.c tkXAppInit.c tclXAppInit.o tkXAppInit.o
	-rm -f libtmp/*.o libtmp/*.a
	cp tclflags.mk.orig tclflags.mk

###############################################################################
# end of Makefile
###############################################################################
