# Makefile $Revision: 1.16 $
#
# 1) Select the proper EXDIR (path), MANDIR, MANEXT, LIBDIR, SIGVOID,
#	RE_COMP/REGCMP, DFLT_PAGER, and FMOD. Most of the other things aren't
#	normally changed (see the comments with each)
# 2) Select the proper machine/compiler/OS section of code
#	for MS-DOS look for the pattern 'MS-DOS'
# 3) make install
# 4) If you have the command 'file' that uses /etc/magic add the line:
#	38	string		Spreadsheet	sc file

# Allow gmake to make intelligent decisions.
uname = $(shell uname)

# Specify the name of the program.
# All documentation and installation keys on this value.
# 
name=sc
NAME=SC

# This is where the install step puts it.
EXDIR=/mit/sipb/bin

# This is where the man page goes.
MANDIR=/mit/sipb/man/man1
MANEXT=1
MANMODE=644

# This is where the library file (tutorial) goes.
LIBDIR=/mit/sipb/share/$(name)

# Set SIMPLE for lex.c if you do not want arrow keys or lex.c blows up
SIMPLE= #-DSIMPLE

# Set BROKENCURSES if your curses has the nl/nonl bug
# If it does and you do not set BROKENCURSES, the display will
# be staggered across the screen. Also try IDLOKBAD below.
BROKENCURSES= #-DBROKENCURSES

# Set DOBACKUPS if you would like a backup copy of a source file on a save
DOBACKUPS=-DDOBACKUPS

# Set INTERNATIONAL if you need 8 bit characters.  You should
# not set this if you are running 5.3.0.  I think it is OK in 5.3.1.
INTERNATIONAL= #-DINTERNATIONAL

# Set SIGVOID if signal routines are type void.
SIGVOID=-DSIGVOID

# Set IEEE_MATH if you need setsticky() calls in your signal handlers
IEEE_MATH= #-DIEEE_MATH

# Set RINT=-DRINT if you do not have rint() in math.h
RINT= #-DRINT

# Set RE_COMP if you have the re_comp/re_exec regular expression routines
# (most BSD based systems do).
# Set REGCMP if you have the regcmp/regex regular expression routines
# (most System V based systems do)
ifeq ($(uname), SunOS) # xxx what about sgi?
REGCMP=-DREGCMP
else
RE_COMP=-DRE_COMP
endif

# This is the name of a pager like "more".
DFLT_PAGER=-DDFLT_PAGER=\"more\"

# this is the name to save back ups in
SAVE=-DSAVENAME=\"$(NAME).SAVE\"

# path to crypt, do not define if you don't have crypt
#ifeq ($(uname), ULTRIX)
CRYPT=-DCRYPT_PATH=\"/usr/athena/bin/crypt\"
#else
#CRYPT=-DCRYPT_PATH=\"/usr/bin/crypt\"
#endif

# If you get errors about fmod being undefined when you try to
# compile, then define NO_FMOD (most likely BSD4.3 and Mt Xinu).
FMOD= #-DNO_FMOD

# If your system doesn't have notimeout() in curses define NONOTIMEOUT
ifeq ($(uname), AIX)
NO_NOTIMEOUT=-DNONOTIMEOUT
else
NO_NOTIMEOUT=
endif

# flags for lint
LINTFLAGS=-abchxv

# Format of quick reference guide generated by $(name)qref
# Leave undefined for normal text output.
QREF_FMT= #-DTROFF

# **** SYSV curses bugs... ****
# Try setting IDLOKBAD to fix (with an empty spreadsheet):
#	a) Redrawing the bottom half of the screen when you
#	 	move between row 9 <-> 10
#	b) the highlighted row labels being trash when you
#		move between row 9 <-> 10
#	c) On an xterm on Esix Rev. D+ from eating lines
#		 -goto (or move) a few lines (or more) past the bottom
#		 of the screen, goto (or move) to the top line on the
#		 screen, move upward and the current line is deleted, the
#		 others move up even when they should not, check by
#		 noticing the rows become 2, 3, 40, 41, 42... (etc).
#	Known systems/terminfos w/ curses problems:
#	{Esix Rev. D+, AT&T SysV3.2.1}:at386-m,xterm, HP-UX7.0:(not sure)
IDLOKISBAD= #-DIDLOKBAD

# If you don't have idlok() in your curses define NOIDLOK
NO_IDLOK= #-DNOIDLOK

# If moving right off the screen causes the screen to not redraw
# properly, define RIGHT_CBUG to get around a curses problem on some
# boxes, this forces screen redraws when going right off the screen
RIGHTBUG= #-DRIGHT_CBUG

# If you have problems with your yacc try bison, Berkeley yacc, or
# some other yacc. Some systems don't allow you to
# increase the number of terminals (mostly AT&T), SCO's does though.
YACC=yacc

# MS-DOS needs y_tab instead of the normal y.tab
#YTAB=y_tab
YTAB=y.tab

# Command to use to make temporary copies of some source files.
LN=ln -s

#### SYSTEM DEFINES ####

ifeq ($(uname), SunOS)
CC=gcc
CFLAGS=  -DSYSV4 -DSYSV3 -O
LIB=-lm -lcurses -lgen
endif

ifeq ($(uname), IRIX)
CFLAGS=  -DSYSV4 -DSYSV3 -O
LIB=-lm -lcurses
endif

ifeq ($(uname), ULTRIX)
CFLAGS= -O -DBSD42
LIB=-lm -lcurses -ltermcap
endif

ifeq ($(uname), AIX)
CFLAGS= -O -DSYSV2 -DCHTYPE=int -DNLS
LIB=-lm -lPW -lcurses
endif

ifeq ($(uname), Linux)
CC=gcc
CFLAGS= -O -DBSD43
LIB=-lm -lncurses
endif

ifeq ($(uname), NetBSD)
CFLAGS= -O -DBSD43
LIB=-lm -lcurses -ltermcap -lcompat
endif

# All of the source files
SRC=Makefile cmds.c crypt.c eres.sed format.c gram.y help.c interp.c \
	lex.c psc.c range.c sc.c sc.h screen.c sres.sed version.c \
	vi.c vmtbl.c xmalloc.c

# The objects
OBJS=cmds.o crypt.o format.o gram.o help.o interp.o lex.o range.o sc.o \
	screen.o version.o vi.o vmtbl.o xmalloc.o

# The documents in the Archive
DOCS=CHANGES README sc.doc psc.doc tutorial.sc VMS_NOTES torev build.com

all:	$(name) p$(name) $(name)qref

$(name):$(PAR) 	$(OBJS)
	$(CC) ${LDFLAGS} ${OBJS} ${LIB} -o $(name)

# Alternative link for MS-DOS
#$(name): 	$(OBJS)
#	link ${LDFLAGS} ${OBJS},$(name),,${LIB};

gram.c:	gram.y
	$(YACC) -d gram.y
	mv $(YTAB).c gram.c

$(YTAB).h:	gram.y

p$(name):	psc.c pvmtbl.o pxmalloc.o
	$(CC) $(CFLAGS) ${LDFLAGS} -o p$(name) psc.c pvmtbl.o pxmalloc.o ${PSCLIB}

# Alternative link for MS-DOS (NB: MSC 5.1 has no getopt.c)
#p$(name): 	psc.o pvmtbl.o pxmalloc.o getopt.o
#	link ${LDFLAGS} psc.o pvmtbl.o pxmalloc.o getopt.o,p$(name);

qhelp.c: help.c
	-rm -f qhelp.c
	${LN} help.c qhelp.c

$(name)qref:	qhelp.c sc.h
	$(CC) $(CFLAGS) $(LDFLAGS) -DQREF $(QREF_FMT) -DSCNAME=\"$(NAME)\" -o $(name)qref qhelp.c

# Alternative link for MS-DOS
#$(name)qref:	qhelp.c sc.h
#	$(CC) -AL -O -Foqhelp.o -c -DQREF -DSCNAME=\"$(name)\" qhelp.c
#	link ${LDFLAGS} qhelp.o,$(name)qref;

pvmtbl.c: vmtbl.c
	-rm -f pvmtbl.c
	${LN} vmtbl.c pvmtbl.c

pvmtbl.o: sc.h pvmtbl.c
	$(CC) ${CFLAGS} -c -DPSC pvmtbl.c

pxmalloc.c: xmalloc.c
	-rm -f pxmalloc.c
	${LN} xmalloc.c pxmalloc.c

# Objects

cmds.o: cmds.c sc.h
	$(CC) ${CFLAGS} ${DOBACKUPS} ${CRYPT} -c cmds.c

crypt.o: crypt.c sc.h
	$(CC) ${CFLAGS} ${CRYPT} ${DOBACKUPS} -c crypt.c

format.o: format.c

help.o: help.c sc.h
	$(CC) ${CFLAGS} ${CRYPT} -c help.c

qhelp.o: qhelp.c sc.h
	$(CC) ${CFLAGS} ${CRYPT} -c qhelp.c

interp.o:	interp.c sc.h
	$(CC) ${CFLAGS} ${IEEE_MATH} ${SIGVOID} ${RINT} ${RE_COMP} ${REGCMP} ${FMOD} -c interp.c

gram.o:	sc.h $(YTAB).h gram.c
	$(CC) ${CFLAGS} -c gram.c
	sed < gram.y > experres.h -f eres.sed
	sed < gram.y > statres.h -f sres.sed

lex.o:	sc.h $(YTAB).h gram.o lex.c
	$(CC) ${CFLAGS} ${SIMPLE} ${IEEE_MATH} ${SIGVOID} ${NO_NOTIMEOUT} -c lex.c

pxmalloc.o: sc.h pxmalloc.c
	$(CC) ${CFLAGS} -c -DPSC pxmalloc.c

range.o: range.c sc.h

sc.o:	sc.h sc.c
	$(CC) ${CFLAGS} ${DFLT_PAGER} ${RIGHTBUG} ${SIGVOID} ${SAVE} -c sc.c

screen.o:	sc.h screen.c
	$(CC) ${CFLAGS} ${BROKENCURSES} ${IDLOKISBAD} ${INTERNATIONAL} ${RIGHTBUG} ${SIGVOID} ${NO_IDLOK} -c screen.c

vi.o: vi.c sc.h

# other stuff

clean:
	rm -f *.o *res.h y.tab.h debug core gram.c $(name).1 \
	p$(name).1 y.output pxmalloc.c pvmtbl.c qhelp.c y_tab.h

clobber:
	rm -f *.o *res.h $(YTAB).h $(name) p$(name) debug core gram.c $(name).1 \
	$(name).man p$(name).man p$(name).1 y.output $(name)qref \
	pxmalloc.c pvmtbl.c qhelp.c

shar: ${SRC} ${DOCS}
	shar -c -m 64000 -f shar ${DOCS} ${SRC}

sshar: ${SRC}
	shar -c -m 1000000 -f shar ${SRC}

lint: sc.h sc.c lex.c gram.c interp.c cmds.c crypt.c range.c help.c \
      vi.c version.c xmalloc.c format.c vmtbl.c
	lint ${LINTFLAGS} ${CFLAGS} ${SIMPLE} sc.c lex.c gram.c interp.c \
        cmds.c crypt.c range.c help.c vi.c version.c xmalloc.c format.c \
        vmtbl.c -lcurses -lm 
	make lintqref

lintqref: help.c
	lint ${LINTFLAGS} ${CFLAGS} ${SIMPLE} -DQREF help.c

lintpsc: psc.c vmtbl.c
	lint ${LINTFLAGS} ${CFLAGS} ${SIMPLE} -DPSC psc.c vmtbl.c

inspect: sc.h sc.c lex.c gram.c interp.c cmds.c crypt.c
	/bruces/ianj/bin/i386/inspect -abv -t 8 sc.c lex.c gram.c interp.c \
	cmds.c crypt.c range.c xmalloc.c help.c vi.c

print: sc.h gram.y sc.c lex.c interp.c cmds.c crypt.c 
	prc sc.h gram.y sc.c lex.c interp.c cmds.c crypt.c | lpr

$(name).1:	sc.doc
	name=$(name) NAME=$(NAME) LIBDIR=$(LIBDIR) sh torev sc.doc >  $(name).1
#	sed -e s/pname/$(name)/g -e s/PNAME/$(NAME)/g \
#	   -e s%#LIBDIR#%$(LIBDIR)%g sc.doc >  $(name).1
#	REVISION=`sed -e '/Revision/!D' -e 's/.*$Revision: 1.16 $(
#	sed -e s/pname/$(name)/g -e s/PNAME/$(NAME)/g \
#		-e s%#LIBDIR#%$(LIBDIR)%g \
#		-e 's/#REVISION#/$(REVISION)/' sc.doc >  $(name).1

$(name).man:	$(name).1
	nroff -man $(name).1 > $(name).man

laser:	$(name).1
	itpf -x -Pim2 -man $(name).1

p$(name).1:	psc.doc
	name=$(name) NAME=$(NAME) LIBDIR=$(LIBDIR) sh torev psc.doc > p$(name).1
#	sed -e s/pname/$(name)/g -e s/PNAME/$(NAME)/g psc.doc >  p$(name).1

p$(name).man:	p$(name).1
	nroff -man p$(name).1 > p$(name).man

install: $(EXDIR)/$(name) $(EXDIR)/$(name)qref $(EXDIR)/p$(name) \
	 $(LIBDIR)/tutorial \
	 $(MANDIR)/$(name).$(MANEXT) $(MANDIR)/p$(name).$(MANEXT)

$(EXDIR)/$(name): $(name)
	cp $(name) $(EXDIR)
	strip $(EXDIR)/$(name)

$(EXDIR)/$(name)qref: $(name)qref
	cp $(name)qref $(EXDIR)
	strip $(EXDIR)/$(name)qref

$(EXDIR)/p$(name): p$(name)
	cp p$(name) $(EXDIR)
	strip $(EXDIR)/p$(name)

$(LIBDIR)/tutorial: tutorial.sc $(LIBDIR)
	cp tutorial.sc $(LIBDIR)/tutorial.$(name)
	chmod $(MANMODE) $(LIBDIR)/tutorial.$(name)

$(LIBDIR):
	mkdir $(LIBDIR)

$(MANDIR)/$(name).$(MANEXT): $(name).1
	cp $(name).1 $(MANDIR)/$(name).$(MANEXT)
	chmod $(MANMODE) $(MANDIR)/$(name).$(MANEXT)

$(MANDIR)/p$(name).$(MANEXT): p$(name).1
	cp p$(name).1 $(MANDIR)/p$(name).$(MANEXT)
	chmod $(MANMODE) $(MANDIR)/p$(name).$(MANEXT)

diffs: ${SRC}
	for i in ${DOCS} ${SRC}  ;\
		do \
		rcsdiff -c -r6.18 $$i ;\
		done

# THA 10/14/90  Added code to make a patchfile
patchfile: ${SRC} ${DOCS}
	rm -f patchfile
	for i in ${SRC} ${DOCS} ;\
		do \
		diffc $$i /users/toma/sc/sc6.12/$$i >> patchfile ;\
		done

files:
	@find $(DOCS) $(SRC) -print
