#
# This is a simplified Makefile for Safe-Tcl.  Before using it, you may wish
# to change some of the following variables:
#
# For many sites, just setting ROOT_DIR to /usr/local
# should suffice, as most of the other Tk-related variables will be derived
# from this.
#
# TK_INCL	where tk.h lives
# TK_LIBDIR	where libtk.a lives
# TK_LIBRARY 	where tk.tcl lives
#
# TCL_INCL	where tcl.h lives
# TCL_LIBDIR	where libtcl.a lives
# TCL_LIBRARY	where init.tcl lives 
#
# XDIR		if your X installation isn't in the standard place, set this
#		to the root directory
#
# XLIB		if your Xlib library isn't in the standard place, you can
#		replace "-lX11" with the name of the file containing your
#		library archive
#
# LIBES		if you need something like a socket emulation library,
#		use it here, e.g., "-lnsl -lsocket"
#
# INSTALL_DIR	full pathname of top-level directory to install wish
#
# RANLIB	if you're using a System-V-based UNIX that doesn't have ranlib,
#		change this definition to "echo"
#
# SHELL		some versions of make (e.g., SGI's) use this variable to
#		determine which shell to use for executing commands
#
# AUXCFLAGS	platform-specific compilation flags; for example, for Ultrix it
#		is NECESSARY to set: 
#			AUXCFLAGS = -Wf,-XNl16384
#
# In addition, there are variables to permit optional compilation with or 
# without the Tcl extension packages TclX and Tcl-dp:
# For TclX:  set TCLXDEFS to "-DUSE_TCLX" 
#                and point TCLX_LIB at libtclx.a
# For Tcl-dp:  set TCLDPDEFS to "-DUSE_TCLDP" 
#                and point TCLDP_LIB at libdpnetwork.a
#
# In addition, these are used to get messaging.c and the mini MH to compile
# correctly.  Here are the options:
#
#	BSD42	4.2BSD-based system, e.g., SunOS, Ultrix, etc.
#	BSD43	4.3BSD-based system (you must also define BSD42)
#	SUN40	SunOS 4.x system (you must also define BSD42)
#
#	SYS5	System V-based system
#	SYS5DIR	"struct dirent" instead of "struct direct"
#	SVR4	System V Release 4 (you must also define SYS5 and SYS5DIR)
#	AUX	A/UX (you must also define SVR4)
#	SOCKETS	if your SV has sockets
#	RENAME	if your SV has rename()
#	FCNTL	if your SV has fcntl()
#
#	VSPRINTF
#		if your OS has vsprintf()
#
#	TYPESIG=type
#		the typecast used by your system for signals.  Either
#		"void" or "int"
#
#	SMTP	use SMTP directly to post mail
#
# By default, you get the flags for SunOS 4.1, e.g.,
#	AUXCFLAGS=-DBSD42 -DSUN40 -DVSPRINTF -DTYPESIG=void -DSMTP
#
# NP		ANSI-C procedure prototypes are turned on, by default, if
#		if supported by the compiler.  If this is the case on your
#		system, define NP to:
#			-DNO_PROTOTYPE
#
# OW		to compile under OpenWindows, you might need to define OW to:
#			-I/usr/openwin/include -L/usr/openwin/lib
# On LINUX, the following settings are reported to work:
# ROOT_DIR		=	/usr
# XDIR		=	/usr/X386
# AUXCFLAGS	=	-DBSD42 -w -O2 -DVSPRINTF -DTYPESIG=void -DLINUX

ROOT_DIR		=	/usr/local
TK_INCL		=	$(ROOT_DIR)/include
TK_LIBDIR		=	$(ROOT_DIR)/lib
TK_LIBRARY	=	$(ROOT_DIR)/lib/tk
TCL_INCL		=	$(ROOT_DIR)/include
TCL_LIBDIR		=	$(ROOT_DIR)/lib
TCL_LIBRARY	=	$(ROOT_DIR)/lib/tcl

XDIR		=	/usr/local/pkg/X11
#XLIB		=	$(XDIR)/lib/libX11.a
XLIB		=	-lX11

LIBES		=

INSTALL_DIR	=	$(ROOT_DIR)
RANLIB		=	ranlib
SHELL		=	/bin/sh

# If you want to add Extended Tcl (TclX) to the TRUSTED interpreter, 
# you may do so by setting the following two lines
#TCLX_LIB		=	${ROOT_DIR}/lib/libtclx.a
#TCLXDEFS		=	-DUSE_TCLX

# Similarly, If you want to add Tcl-dp (Distributed Programming)
# you may do so by setting the following two lines
#TCLDP_LIB		=	${ROOT_DIR}/lib/libdpnetwork.a
#TCLDPDEFS		=	-DUSE_TCLDP

AUXCFLAGS	=	-DBSD42 -DSUN40 -DVSPRINTF -DTYPESIG=void -DSMTP
#NP		=	-DNO_PROTOTYPE
OW		=	-I$(XDIR)/include

CC		=	cc

# We offer swish_cflags to allow further interface styles.
# See createsafe.c and interfaceStyle.h.
SWISH_ISTYLE    = SAFETCL_INTERFACESTYLE_TK3_6
SWISH_CFLAGS	= -I. -I$(TCL_INCL) -I$(TK_INCL)  $(TCLXDEFS) \
                  $(TCLDPDEFS) -g ${NP} ${OW} ${AUXCFLAGS} -DRPATHS \
                  -DTCL_LIBRARY=\"${TCL_LIBRARY}\" \
                  -DTK_LIBRARY=\"${TK_LIBRARY}\" \
                  -D$(SWISH_ISTYLE)

STCLSH_CFLAGS	= -I. -I$(TCL_INCL) $(TCLXDEFS) \
                  $(TCLDPDEFS) ${NP} ${AUXCFLAGS} -DRPATHS \
                  -DTCL_LIBRARY=\"${TCL_LIBRARY}\" \
                  -DTK_LIBRARY=\"\"

SWISH_LIBS	= $(TK_LIBDIR)/libtk.a $(TCLX_LIB) $(TCLDP_LIB) \
                  $(TCL_LIBDIR)/libtcl.a
STCLSH_LIBS	= $(TCLX_LIB) $(TCLDP_LIB) $(TCL_LIBDIR)/libtcl.a


all:		swish stclsh

install:	swish stclsh
		rm -f $(INSTALL_DIR)/bin/swish
		cp swish $(INSTALL_DIR)/bin
		chmod 755 swish
		rm -f $(INSTALL_DIR)/bin/stclsh
		cp stclsh $(INSTALL_DIR)/bin
		chmod 755 stclsh


# IF you have MH 6.8 installed on your system, you might want to read the file
# mh-6.8/READ-ME.  Regardless, leave the next line alone.
MHLIB	=	libmh.a

libmh.a:;	cd mh-6.8; make RANLIB=$(RANLIB) AUXCFLAGS="$(AUXCFLAGS)"


TCL-C	= init.c restrict.c unrestrict.c 
TCL-O	= $(TCL-C:.c=.o)
TK-C	= button.c entry.c listbox.c menu.c text.c tk.c tkerror.c
TK-O	= $(TK-C:.c=.o)
SWISH_SRCS	= safe-tkMain.c stinit.c createsafe.c messaging.c \
		  tkInterfaceStyle.c
SWISH_OBJS	= $(SWISH_SRCS:%.c=swish_obj/%.o)
STCLSH_SRCS	= stclsh.c createsafe.c messaging.c \
		  genericInterfaceStyle.c
STCLSH_OBJS	= $(STCLSH_SRCS:%.c=stclsh_obj/%.o)

swish: swish_obj $(SWISH_OBJS) $(TCL-O) $(TK-O) $(SWISH_LIBS) $(MHLIB)
	$(CC) $(SWISH_CFLAGS) $(SWISH_OBJS) $(TCL-O) $(TK-O) \
		$(SWISH_LIBS) $(XLIB) $(MHLIB) $(LIBES) -lm -o $@

stclsh: stclsh_obj $(STCLSH_OBJS) $(TCL-O) $(STCLSH_LIBS) $(MHLIB)
	$(CC) $(STCLSH_CFLAGS) $(STCLSH_OBJS) $(TCL-O) \
		$(STCLSH_LIBS) $(MHLIB) $(LIBES) -lm -o $@

clean:; /bin/rm -f swish stclsh libmh.a $(TCL-C) $(TK-C) $(TCL-O) $(TK-O)
	/bin/rm -rf swish_obj stclsh_obj
	(cd mh-6.8; make clean)

swish_obj/safe-tkMain.o: safe-tkMain.c
	$(COMPILE.c) $(SWISH_CFLAGS) safe-tkMain.c -o $@
# For TclX & Tcl-dp compilation, stinit.o depends on the Makefile.
swish_obj/stinit.o:  stinit.c Makefile
	$(COMPILE.c) $(SWISH_CFLAGS) stinit.c -o $@
swish_obj/createsafe.o: interfaceStyle.h genericInterfaceStyle.h createsafe.c
	$(COMPILE.c) $(SWISH_CFLAGS) createsafe.c -o $@
swish_obj/messaging.o: messaging.c
	$(COMPILE.c) $(SWISH_CFLAGS) messaging.c -o $@
swish_obj/tkInterfaceStyle.o: tkInterfaceStyle.h tkInterfaceStyle.c
	$(COMPILE.c) $(SWISH_CFLAGS) tkInterfaceStyle.c -o $@

stclsh_obj/stclsh.o: stclsh.c
	$(COMPILE.c) $(STCLSH_CFLAGS) stclsh.c -o $@
stclsh_obj/createsafe.o: interfaceStyle.h genericInterfaceStyle.h  createsafe.c
	$(COMPILE.c) $(STCLSH_CFLAGS) createsafe.c -o $@
stclsh_obj/messaging.o: messaging.c
	$(COMPILE.c) $(STCLSH_CFLAGS) messaging.c -o $@
stclsh_obj/genericInterfaceStyle.o: genericInterfaceStyle.h genericInterfaceStyle.c
	$(COMPILE.c) $(STCLSH_CFLAGS) genericInterfaceStyle.c -o $@

swish_obj:
	test -d $@ || mkdir $@
		
stclsh_obj:
	test -d $@ || mkdir $@
		
button.c: ${TK_LIBRARY}/button.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/button.tcl | sed -e 1,99999s/XXXXXXXX/ButtonContents/ > button.c

entry.c: ${TK_LIBRARY}/entry.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/entry.tcl | sed -e 1,99999s/XXXXXXXX/EntryContents/ > entry.c

init.c: ${TCL_LIBRARY}/init.tcl
	sed -f encap-tcl.sed   < ${TCL_LIBRARY}/init.tcl | sed -e 1,99999s/XXXXXXXX/InitContents/ > init.c

listbox.c: ${TK_LIBRARY}/listbox.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/listbox.tcl | sed -e 1,99999s/XXXXXXXX/ListboxContents/ > listbox.c

menu.c: ${TK_LIBRARY}/menu.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/menu.tcl | sed -e 1,99999s/XXXXXXXX/MenuContents/ > menu.c

restrict.c: restrict.tcl
	sed -f encap-tcl.sed < restrict.tcl | sed -e 1,99999s/XXXXXXXX/RestrictContents/ > restrict.c

text.c: ${TK_LIBRARY}/text.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/text.tcl | sed -e 1,99999s/XXXXXXXX/TextContents/ > text.c

tkerror.c: ${TK_LIBRARY}/tkerror.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/tkerror.tcl | sed -e 1,99999s/XXXXXXXX/TkerrorContents/ > tkerror.c

tk.c: ${TK_LIBRARY}/tk.tcl
	sed -f encap-tcl.sed < ${TK_LIBRARY}/tk.tcl | sed -e 1,99999s/XXXXXXXX/TkContents/ > tk.c

unrestrict.c: unrestrict.tcl
	sed -f encap-tcl.sed < unrestrict.tcl | sed -e 1,99999s/XXXXXXXX/UnrestrictContents/ > unrestrict.c
