#
# $Id: Makefile,v 1.21 1995/08/06 04:19:46 sls Exp $
#
#
# This software is copyright (C) 1995 by the Lawrence Berkeley Laboratory.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that: (1) source code distributions
# retain the above copyright notice and this paragraph in its entirety, (2)
# distributions including binary code include the above copyright notice and
# this paragraph in its entirety in the documentation or other materials
# provided with the distribution, and (3) all advertising materials mentioning
# features or use of this software display the following acknowledgement:
# ``This product includes software developed by the University of California,
# Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
# the University nor the names of its contributors may be used to endorse
# or promote products derived from this software without specific prior
# written permission.
# 
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

VERSION		= 0.4.2

# SGI's seem to need this...
SHELL		= /bin/sh

# Change these things to point to wherever you want sntl installed
INSTALL_ROOT	=
DESTDIR		= /usr/local
#DESTDIR		= /vol/pub/sntl-0.4
BINDIR		= $(DESTDIR)/bin
MANDIR		= $(DESTDIR)/man/mann
MAN1DIR		= $(DESTDIR)/man/man1
LIBDIR		= $(DESTDIR)/lib
LIBRARY_DIR	= $(DESTDIR)/lib/sntl

# Point these to where the Tcl include files, library files, and tclsh
# can be found
TCL_INCLUDE	= -I/vol/pub/tcl-7.3/include
TCL_LIBS	= -L/vol/pub/tcl-7.3/lib -ltcl7.4
#TCLSH		= /vol/pub/tcl-7.3/bin/tclsh
TCLSH		= /usr/local/bin/tclsh7.4

# Point to where Tk & X11 are
TK_INCLUDE	= -I/usr/X11R6/include
TK_LIBS		= -L/usr/X11R6/lib -ltk4.0 -lX11
WISH		= /usr/local/bin/wish4.0

# any extra libs to link
EXTRA_LIBS 	=  -lieee -lm

# C compiler to use
#CC		= gcc -fwritable-strings -Wall
CC		= cc

# You probably don't need to touch this stuff.
DEFS		=

COPTS		= #-g
CFLAGS		= $(COPTS) $(DEFS) $(TCL_INCLUDE)

RANLIB		= -ranlib
INSTALL		= ./install.sh
#INSTALL		= install

#MKDIR		= -mkdir
MKDIR		= -mkdir -p

LIB_TCL_SRC	= stub_doc.tcl util.tcl msg.tcl bag.tcl form.tcl html.tcl \
		  notify.tcl object.tcl profile.tcl args.tcl

TCL_SCRIPTS	= chlog2html.tcl mkindex.tcl tcl2c.tcl document.tcl \
		  mksh.tcl subst.tcl

MISC_TCL_SRC	= cgi_post_hdr.tcl cgi_post_trl.tcl

SUBSTITUTIONS 	= tclsh=$(TCLSH) wish=$(WISH) sntl_library=$(LIBRARY_DIR) \
		  stl_library=$(LIBRARY_DIR)

LIB_TK_SRC	= tk_util.tcl tkhtml.tcl

WIDGETS_SRC	= widgets/dial.tcl widgets/filechsr.tcl \
		  widgets/simpleentry.tcl widgets/simpletext.tcl \
		  widgets/strip.tcl widgets/simplelist.tcl \
		  widgets/columns.tcl widgets/feedback.tcl

SNTLW_SRC	= widgets.tcl $(LIB_TK_SRC) $(WIDGETS_SRC)

MAN_PAGES	= sntl.n util.n msg.n html.n form.n document.n notify.n \
		  object.n args.n profile.n bag.n tk_util.n \
		  widgets.n widgets/feedback.n widgets/columns.n \
		  widgets/simplelist.n widgets/filechsr.n \
		  tkhtml.n

MAN1_PAGES	= mksh.1 chlog2html.1 tcl2c.1 subst.1 mkindex.1

.SUFFIXES: .c .o .tcl .n .1

all: tclIndex libsntl.a man-pages ChangeLog.html sntl_sh sntlw_sh

sntl-code.c: $(LIB_TCL_SRC) tcl2c.tcl
	$(TCLSH) tcl2c.tcl SNTL sntl-code.c $(LIB_TCL_SRC)

sntlw-code.c: $(SNTLW_SRC) tcl2c.tcl
	$(TCLSH) tcl2c.tcl SNTLW sntlw-code.c $(SNTLW_SRC)

sntl_sh: sntl_sh.o libsntl.a
	$(CC) $(COPTS) -o sntl_sh sntl_sh.o libsntl.a $(TCL_LIBS) $(EXTRA_LIBS)

sntl_sh.c:
	$(TCLSH) mksh.tcl -out sntl_sh.c SNTL_Init

# if you're using Tk 3.6 then remove the -tkmain switch.
sntlw_sh.c:
	$(TCLSH) mksh.tcl -tkmain -out sntlw_sh.c Tk_Init SNTL_Init SNTLW_Init

sntlw_sh: sntlw_sh.o libsntl.a
	$(CC) $(COPTS) -o sntlw_sh sntlw_sh.o libsntl.a \
	$(TK_LIBS) $(TCL_LIBS) $(EXTRA_LIBS)

libsntl.a: sntl-code.o sntlw-code.o
	ar rv libsntl.a sntl-code.o sntlw-code.o
	$(RANLIB) libsntl.a

tclIndex: mkindex.tcl
	$(TCLSH) mkindex.tcl . $(LIB_TCL_SRC) $(SNTLW_SRC)
	$(MAKE) widgets.tcl
	$(TCLSH) mkindex.tcl . $(LIB_TCL_SRC) $(SNTLW_SRC) widgets.tcl

install: all mkdirs install-scripts install-misc
	$(INSTALL) -c -m 0644 libsntl.a $(INSTALL_ROOT)$(LIBDIR)
	$(RANLIB) $(LIBDIR)/libsntl.a
	for file in man_macros $(LIB_TCL_SRC) $(SNTLW_SRC); do \
	  echo "# Installing $$file"; \
	  $(INSTALL) -c -m 0444 $$file $(INSTALL_ROOT)$(LIBRARY_DIR); \
	done
	rm -f $(INSTALL_ROOT)$(LIBRARY_DIR)/tclIndex
	$(TCLSH) mkindex.tcl $(INSTALL_ROOT)$(LIBRARY_DIR)
	for file in $(MAN_PAGES); do \
	  echo "# Installing $$file"; \
	  $(INSTALL) -c -m 0444 $$file $(INSTALL_ROOT)$(MANDIR); \
	done
	for file in $(MAN1_PAGES); do \
	  echo "# Installing $$file"; \
	  $(INSTALL) -c -m 0444 $$file $(INSTALL_ROOT)$(MAN1DIR); \
	done
	$(INSTALL) -c -m 0555 sntl_sh $(INSTALL_ROOT)$(BINDIR)
	$(INSTALL) -c -m 0555 sntlw_sh $(INSTALL_ROOT)$(BINDIR)

widgets.tcl: widgets.tcl.in
	$(TCLSH) subst.tcl widgets.tcl.in widgets.tcl $(SUBSTITUTIONS)

sntl.tcl: sntl.tcl.in
	$(TCLSH) subst.tcl sntl.tcl.in sntl.tcl $(SUBSTITUTIONS)

mkdirs:
	$(MKDIR) $(INSTALL_ROOT)$(LIBDIR)
	$(MKDIR) $(INSTALL_ROOT)$(LIBRARY_DIR)
	$(MKDIR) $(INSTALL_ROOT)$(MANDIR)
	$(MKDIR) $(INSTALL_ROOT)$(MAN1DIR)
	$(MKDIR) $(INSTALL_ROOT)$(BINDIR)

install-scripts: $(TCL_SCRIPTS)
	for file in $(TCL_SCRIPTS); do \
	  echo "# Installing $$file"; \
	  rm -f $(INSTALL_ROOT)$(BINDIR)/$$file; \
	  $(TCLSH) subst.tcl $$file $(INSTALL_ROOT)$(BINDIR) $(SUBSTITUTIONS); \
	  chmod 0555 $(INSTALL_ROOT)$(BINDIR)/$$file; \
        done

install-misc: $(MISC_TCL_SRC)
	$(TCLSH) subst.tcl cgi_post_hdr.tcl $(INSTALL_ROOT)$(LIBRARY_DIR) $(SUBSTITUTIONS)
	$(INSTALL) -c -m 0444 cgi_post_trl.tcl $(INSTALL_ROOT)$(LIBRARY_DIR)

.tcl.n:
	$(TCLSH) document.tcl $<

.tcl.1:
	$(TCLSH) document.tcl -extension 1 $<

man-pages: $(MAN_PAGES) $(MAN1_PAGES)

ChangeLog.html: ChangeLog
	$(TCLSH) chlog2html.tcl -title "SNTL Change Log"

clean:
	rm -f *~ $(MAN_PAGES) $(MAN1_PAGES) tclIndex widgets.tcl sntl.tcl
	rm -f ChangeLog.html sntl_sh sntl_sh.c sntlw_sh sntlw_sh.c
	rm -f *.o sntl-code.c sntlw-code.c libsntl.a
	rm -f widgets/*~
	rm -rf sntl-$(VERSION)
	rm -f sntl-$(VERSION).tar.gz

dist:
	rm -rf sntl-$(VERSION)
	-mkdir sntl-$(VERSION)
	-mkdir sntl-$(VERSION)/widgets
	cp COPYRIGHT Makefile ChangeLog README install.sh man_macros widgets.tcl.in sntl.tcl.in \
	  sntl-$(VERSION)
	cp widgets/ChangeLog sntl-$(VERSION)/ChangeLog.widgets
	cp $(LIB_TCL_SRC) $(TCL_SCRIPTS) $(MISC_TCL_SRC) $(LIB_TK_SRC) \
	  sntl-$(VERSION)
	cp $(WIDGETS_SRC) sntl-$(VERSION)/widgets
	tar cf - sntl-$(VERSION) | gzip -9v > sntl-$(VERSION).tar.gz
