#-[Tue Oct 20 12:16:20 1992 by cxh]-
#
# Makefile for tcl - SQL interface

# Copyright 1992 Regents of the University of California.
# Permission to use, copy, modify, and distribute this
# software and its documentation for any purpose and without
# fee is hereby granted, provided that the above copyright
# notice appear in all copies.  The University of California
# makes no representations about the suitability of this
# software for any purpose.  It is provided 'as is' without
# express or implied warranty.


PREFIXDIR = 	/usr/local
BINDIR =	$(PREFIXDIR)/bin
LIBDIR = 	$(PREFIXDIR)/lib/tcl/tclsql
MANDIR =	$(PREFIXDIR)/man/man3
TCL_LIBDIR =	/usr/local/lib/tcl
TK_LIBDIR =	/usr/local/lib/tk
INGRES_LIBDIR = /usr/users/ingres/ingres/lib

INSTALL_OWNER =	bin
INSTALL_GROUP = bin

# ---- You should not have to change any thing below this line ----
PROGS =		tclsql sqlwish
THISPROG =	tclsql
SRCS =		sql.sc delete.sc exec.sc opendb.sc sqlcurs.sc immediate.sc \
			tclsql.c cursorcmds.c tclsqlTest.c sqlwish.c coltype.c 

TMPS =		sql.c delete.c exec.c opendb.c sqlcurs.c \
			immediate.c
OBJS =		sql.o delete.o exec.o opendb.o sqlcurs.o \
			immediate.o tclsql.o cursorcmds.o coltype.o

TCLSQL_OBJS =	$(OBJS) tclsqlTest.o
SQLWISH_OBJS =	$(OBJS) sqlwish.o

LINTS =		sql.c delete.c exec.c opendb.c sqlcurs.c \
			immediate.c \
			tclsql.c cursorcmds.c coltype.c
TCLSQL_LINTS =		$(LINTS) tclsqlTest.c
SQLWISH_LINTS =		$(LINTS) sqlwish.c

# Files to go into the distribution
DISTFILES =	Makefile README INSTALL README.dbdict TODO BUGS \
		$(SRCS) tclsql.h coltype.h curs.h sqlbr dbdict \
		tbldef.tcl sqltools.tcl

LIBFLAGS =	-L$(TCL_LIBDIR) -L$(INGRES_LIBDIR) -L$(TK_LIBDIR) \
		-ltcl -lingres -ltk -lX11 -lm

# Name of tcl lint library.  (The tcl lint library must be made by hand).
LINTLIBS =	-ltcl
INCLUDES =	-I$(TCL_LIBDIR) -I$(TK_LIBDIR)

# GCC won't compile sqlwish, but GCC will work with tclsql.
#CC = 		gcc
#CFLAGS = 	-g -O -Wunused -Wswitch -Wcomment -Wtrigraphs -Wformat -Wchar-subscripts -Wparentheses -Wuninitialized

CC = 		cc
CFLAGS =	-O
# Used for test case coverage on Suns.
#CFLAGS = 	-a

# Embedded SQL preprocessor.
#  The -p flag generates #line directives to the C compiler.
#  Note that you cannot use the -p flag and tcov.
ESQL =		esqlc -p

# Used for tracking down malloc problems on Suns.
#MALLOC_DEBUG = 	/usr/lib/debug/malloc.o /usr/lib/debug/mallocmap.o
#OTHERCFLAGS =		-DMALLOC_DEBUG
MALLOC_DEBUG =
OTHERCFLAGS =

# Rules
.SUFFIXES: .sc

.sc.o:
	$(ESQL) $<
	$(CC) -c $(CFLAGS) $(OTHERCFLAGS) $(INCLUDES) $*.c

.sc.c:
	$(ESQL) $<

.c.o:
	$(CC) -c $(CFLAGS) $(OTHERCFLAGS) $(INCLUDES) $<

all: $(PROGS)

$(THISPROG): $(TCLSQL_OBJS)
	$(CC) $(CFLAGS) $(INCLUDES) $(TCLSQL_OBJS) $(MALLOC_DEBUG) $(LIBFLAGS) -o $(THISPROG)

sqlwish: sqlwish.o $(SQLWISH_OBJS)
	$(CC) $(CFLAGS) $(INCLUDES) $(SQLWISH_OBJS) $(LIBFLAGS) -o sqlwish

#  Run this rule if you have the Ingres 'tutorial' database.
#  The contents of the database are in the tests/tutorial_db directory.

tests: $(THISPROG)
	(cd tests; ../$(THISPROG) < all)

# compile with the -a option to get test coverage stats
tcov: $(THISPROG)
	(cd tests; ../$(THISPROG) < all)
	tcov -a $(TCLSQL_LINTS)

install: $(PROGS) install_bin install_lib install_man

install_bin:
	install -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0755 tclsql sqlwish $(BINDIR)

install_lib:
	install -d -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0755 $(LIBDIR)
	install -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0755 sqlbr dbdict $(LIBDIR)
	install -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0644 *.tcl $(LIBDIR)
	install -d -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0755 $(LIBDIR)/tests
	install -d -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0755 $(LIBDIR)/tests/tutorial_db
	
	install -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0644 tests/README tests/all tests/defs tests/*.test $(LIBDIR)/tests
	install -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0644 tests/tutorial_db/* $(LIBDIR)/tests/tutorial_db

install_man:
	install -d -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0755 $(MANDIR)
	install -o $(INSTALL_OWNER) -g $(INSTALL_GROUP) -m 0644 man/man3/* $(MANDIR)
	

tags:	$(SRCS)
	etags $(SRCS)

clean:	
	rm -f a.out core *~ *.o *.lis $(TMPS) $(PROGS) $(THISPROG).tar *.d *.tcov

indent:
	indent -v $(SRCS)

lint: tclsql_lint sqlwish_lint

tclsql_lint: $(TCLSQL_LINTS)
	lint $(INCLUDES) $(OTHERCFLAGS) $(TCLSQL_LINTS) $(LINTLIBS)

sqlwish_lint: $(SQLWISH_LINTS)
	lint $(INCLUDES) $(OTHERCFLAGS) $(SQLWISH_LINTS) $(LINTLIBS)

depend: $(LINTS)
	makedepend $(INCLUDES) $(LINTS) tclsqlTest.c sqlwish.c *.h

# From here on down is used to make a distribution

# Directory to hold links to files in distribution.
# (This allows us to create a tar files that has files inside a directory
#  one level down instead of files inside the the current directory.)
#
TCLSQL_VERSION =	1.0
TMPDISTDIR =		tclsql-$(TCLSQL_VERSION)

TARFILE =		tclsql-$(TCLSQL_VERSION).tar

catman:
	@echo "ignore error messages like: opendir:man1: No such file or directory"
	-catman -M man

$(TMPDISTDIR):
	mkdir $(TMPDISTDIR)

tar: $(TARFILE)

README: man/man3/tclsql.3
	nroff -man man/man3/tclsql.3 | col -b | \
	sed -e '/Last change/d' -e '/^tclsql(3)/d' | \
	cat -s > README

$(TARFILE): $(DISTFILES) $(TMPDISTDIR) catman README
	# Clean up then end of this Makefile
	makedepend
	rm -rf $(TMPDISTDIR)/*
	mkdir -p $(TMPDISTDIR)/man/man3 $(TMPDISTDIR)/tests
	chmod -R a+r $(DISTFILES) man tests/tutorial_db
	chmod 755 $(TMPDISTDIR) $(TMPDISTDIR)/man $(TMPDISTDIR)/man/man3 \
		dbdict sqlbr $(TMPDISTDIR)/tests
	(cd $(TMPDISTDIR);\
	 ln -s `echo "$(DISTFILES)" | \
		awk '{ for(i=1;i<=NF;i++)print "../"$$i}' - ` .)
	rm -f man/man3/*~ man/man3/#* tests/*~ tests/#*
	(cd $(TMPDISTDIR)/man/man3;\
	 ln -s ../../../man/man3/* .)
	(cd $(TMPDISTDIR)/tests; ln -s ../../tests/* .)
	tar -chf $(TARFILE) $(TMPDISTDIR)
	cp $(TARFILE) /tmp/$(TARFILE)
	chmod a+r /tmp/$(TARFILE)
	rm -f /tmp/tclsql.tar; ln -s /tmp/$(TARFILE) /tmp/tclsql.tar

toradon: $(TARFILE)
	rcp $(TARFILE) radon:/tmp/$(TARFILE)
	rsh radon cp /tmp/$(TARFILE) /usr/users/cxh/tmp/$(TARFILE)
	rsh radon chmod a+r  /tmp/$(TARFILE)

# DO NOT DELETE THIS LINE -- make depend depends on it.
