# combined Makefile for ELVIS - a clone of `vi`
#
# After editing this Makefile as described below, you should...
#
# Use `make` to compile all programs
# Use `make install` to copy the programs to the BIN directory
# Use `make clean` to remove all object files
# Use `make clobber` to remove everything except source & documentation
# Use `make uue` to produce uuencoded compressed tar archives of the source
# Use `make sh` to produce shar archives of the source
#
# Several groups of Makefile settings are included below.  Choose *ONE* group
# of settings for your particular system, and leave the others commented out.
# The meanings of these settings are:
#	OBJ	the filename extension for unlinked object files -- usually .o
#	EXE	filename extension for elvis executable file -- .exe, .tpp or null
#	COM	filename extension for executable support files -- .com, .tpp or null
#	EXTRA	version-specific object files used in elvis
#	EXTRA2	version-specific object files used in elvis & elvrec
#	LIBS	any special libraries, such as "-ltermcap"
#	BIN	directory where executables should be installed
#	SBIN	directory where system executables should be installed
#	CC	the C compiler command, possibly with "memory model" flags
#	CFLAGS	compiler flags used to select compile-time options
#	LNK	the linker command needed to link OBJ files (if not cc)
#	LFLAGS	linker flags used to select link-time options
#	SMALL	flag for special small memory model compilation -- usually null
#	OF	link flag to control the output file's name -- usually -o<space>
#	RF	flag used to denote "compile but don't link" -- usually -c
#	PROGS	the list of user programs
#	CHMEM	any extra commands to be run after ELVIS is linked
#	SORT	if the "tags" file must be sorted, then SORT=-DSORT
#	RM	the name of a program that deletes files
#	CP	name of a program that copies files -- copy or cp, usually
#	SYS	type of system & compiler: unx/tos/os9/dos (maybe vms?)
#	DUMMY	usually nothing, but OS9 needs "dummy"
#	CFG	name of compiler configuration file -- usually null

#---- These settings are recommended for Minix-vmd ----
OBJ=	.o
EXE=
COM=
EXTRA=	unix$(OBJ)
EXTRA2=
LIBS=
BIN=	/usr/bin
SBIN=	/usr/sbin
MAN=	/usr/man
CC=	cc
CFLAGS=	-D_MINIX -fnone $(OPT)
LFLAGS=	-fnone
SMALL=
OF=	-o ""
RF=	-c
EVAL=	eval
PROGS=	elvis$(EXE) ctags$(COM) ref$(COM) elvrec$(COM) fmt$(COM) elvprsv$(COM)
CHMEM=
SORT=
RM=	rm -f
CP=	cp
SYS=	unx
DUMMY=
CFG=


###########################################################################
###########################################################################
###                                                                     ###
###     The rest of this Makefile contains no user-serviceable parts    ###
###                                                                     ###
###########################################################################
###########################################################################

OBJS1=	blk$(OBJ) cmd1$(OBJ) cmd2$(OBJ) ctype$(OBJ) curses$(OBJ) cut$(OBJ)
OBJS2=	ex$(OBJ) input$(OBJ) main$(OBJ) misc$(OBJ) modify$(OBJ) move1$(OBJ)
OBJS3=	move2$(OBJ) move3$(OBJ) move4$(OBJ) move5$(OBJ) opts$(OBJ) recycle$(OBJ)
OBJS4=	redraw$(OBJ) regexp$(OBJ) regsub$(OBJ) system$(OBJ) tio$(OBJ) tmp$(OBJ)
OBJS5=	vars$(OBJ) vcmd$(OBJ) vi$(OBJ)
OBJS=	$(OBJS1) $(OBJS2) $(OBJS3) $(OBJS4) $(OBJS5)

ALIAS=	alias$(OBJ)

SRC1=	Elvis.lnk Elvis.mak Elvis.prj Makefile.mix alias.c atari.c \
	ctags.c pc.c ref.c shell.c sysdos.c
SRC2=	elvrec.c wildcard.c fmt.c profile.sh osk.c osk.h elvprsv.c unix.c \
	blk.c config.h
SRC3=	cmd1.c cmd2.c curses.c
SRC4=	curses.h cut.c ex.c input.c main.c misc.c
SRC5=	modify.c move1.c move2.c move3.c move4.c move5.c opts.c recycle.c redraw.c
SRC6=	regexp.c regexp.h regsub.c tinytcap.c tio.c tmp.c
SRC7=	system.c vars.c vcmd.c vi.c vi.h tinyprnt.c
SRC8=	ctype.c ctype.h borland.h novice.rc prsvdos.c prsvunix.c \
	vmsbuild.com vmsio.c vmslink.opt vmsread.me vmssetup.com vmswild.c \
	amiga.c amiprsv.c amistat.h amisysio.c amitty.c amiwild.c

###########################################################################
all: $(PROGS)

#elvis$(EXE): linkelv.$(SYS)

ctags$(COM): $(CFG) ctags.c
	$(CC) $(CFLAGS) $(SMALL) $(SORT) $(OF)ctags$(COM) ctags.c $(EXTRA2)

ref$(COM): $(CFG) ref.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)ref$(COM) ref.c

elvrec$(COM): $(CFG) elvrec.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)elvrec$(COM) elvrec.c $(EXTRA2)

ex$(COM): linkex.$(SYS)

vi$(COM): linkvi.$(SYS)

view$(COM): linkview.$(SYS)

input$(COM): linkinput.$(SYS)

shell$(COM): $(CFG) shell.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)shell$(COM) shell.c

wildcard$(COM): $(CFG) wildcard.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)wildcard$(COM) wildcard.c ctype$(OBJ)

fmt$(COM): $(CFG) fmt.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)fmt$(COM) fmt.c $(EXTRA2)

elvprsv$(COM): $(CFG) elvprsv.c amiprsv.c prsvunix.c prsvdos.c wildcard.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)elvprsv$(COM) elvprsv.c ctype$(OBJ)

##############################################################################
# "It all depends..."

$(OBJS): vi.h curses.h config.h regexp.h ctype.h

##############################################################################
# The way to link elvis is system dependent, and maybe compiler dependent too.

# elvis executable compilation rule for: AmigaDOS
linkelv.ami: $(OBJS) $(EXTRA) $(EXTRA2)
	$(LNK) $(LFLAGS) $(OF)elvis$(EXE) $(OBJS) $(EXTRA) $(EXTRA2) $(LIBS)
	$(CHMEM)
	@touch $@

# elvis executable compilation rule for: all unix, os9, tos, msc dos
#linkelv.unx linkelv.tos linkelv.os9 linkelv.cl: $(OBJS) $(EXTRA) $(EXTRA2)
elvis$(EXE): $(OBJS) $(EXTRA) $(EXTRA2)
	$(CC) $(LFLAGS) $(OF)elvis$(EXE) $(OBJS) $(EXTRA) $(EXTRA2) $(LIBS)
	$(CHMEM)

linkelv.dos: linkelv.$(CC)

# elvis executable compilation rule for: borland tcc, bcc
linkelv.bcc linkelv.tcc: $(CFG) $(OBJS) $(EXTRA) $(EXTRA2)
	$(CC) $(CFLAGS) $(LIBS)
	@echo done > linkelv.$(CC)

linkelv.vms: $(OBJS) $(EXTRA) $(EXTRA2)
	link$(OF)elvis.exe main,$(CFG)/opt

##############################################################################

# borland/turbo c config files
borland.cfg: $(DUMMY)
	echo $(TC_CFLAGS1)> borland.cfg
	echo $(TC_CFLAGS2)>> borland.cfg

borland.rsp: $(DUMMY)
	echo $(EFLAGS) $(OF)elvis$(EXE) > borland.rsp
	echo $(OBJS1)>> borland.rsp
	echo $(OBJS2)>> borland.rsp
	echo $(OBJS3)>> borland.rsp
	echo $(OBJS4)>> borland.rsp
	echo $(OBJS5)>> borland.rsp
	echo $(EXTRA)>> borland.rsp

##############################################################################
# elvis executable compilation rule for: AmigaDOS
linkex.ami: alias.o
	$(LNK) $(LFLAGS) $(OF)ex$(EXE) alias.o $(LIBS)

linkex.unx linkex.tos linkex.xdos linkex.dos linkex.vms: $(CFG) alias.c
	$(CC) $(CFLAGS) $(SMALL) $(OF)ex$(COM) alias.c

linkex.os9: $(DUMMY)
	@echo done >/nil

linkvi.ami linkvi.unx linkvi.tos linkvi.xdos linkvi.dos linkvi.vms: ex$(COM)
	$(CP) ex$(COM) vi$(COM)

linkvi.os9: alias.r
	$(CC) $(CFLAGS) $(OF)vi$(COM) alias.r
	@touch $@

linkview.ami linkview.unx linkview.tos linkview.xdos linkview.dos linkview.vms: ex$(COM)
	$(CP) ex$(COM) view$(COM)

linkview.os9: alias.r
	$(CC) $(CFLAGS) $(OF)view$(COM) alias.r
	@touch $@

linkinput.ami linkinput.unx linkinput.tos linkinput.xdos linkinput.dos linkinput.vms: ex$(COM)
	$(CP) ex$(COM) input$(COM)

linkinput.os9: alias.r
	$(CC) $(CFLAGS) $(OF)input$(COM) alias.r
	@touch $@

##############################################################################
# installation is system-dependent

#install: $(PROGS) inst.$(SYS)

install:	\
	$(BIN)/elvis $(BIN)/vi $(BIN)/ex \
	$(BIN)/ctags $(BIN)/ref $(BIN)/fmt \
	$(BIN)/elvrec $(SBIN)/elvprsv \
	$(MAN)/man1/ctags.1 $(MAN)/man1/elvis.1 $(MAN)/man8/elvprsv.8 \
	$(MAN)/man1/elvrec.1 $(MAN)/man1/fmt.1 $(MAN)/man1/ref.1

$(BIN)/elvis:	elvis
	install -lc elvis $@

$(BIN)/vi $(BIN)/ex:	$(BIN)/elvis
	install -l $(BIN)/elvis $@

$(BIN)/ctags:	ctags
	install -lc ctags $@

$(BIN)/ref:	ref
	install -lc ref $@

$(BIN)/fmt:	fmt
	install -lc fmt $@

$(BIN)/elvrec:	elvrec
	install -c -o root -m 4755 elvrec $@

$(SBIN)/elvprsv:	elvprsv
	install -d -o root -m 700 /usr/preserve
	install -c -o root -m 4755 elvprsv $@

$(MAN)/man1/ctags.1:	doc/ctags.man
	install -lc doc/ctags.man $@

$(MAN)/man1/elvis.1:	doc/elvis.man
	install -lc doc/elvis.man $@

$(MAN)/man8/elvprsv.8:	doc/elvprsv.man
	install -lc doc/elvprsv.man $@

$(MAN)/man1/elvrec.1:	doc/elvrec.man
	install -lc doc/elvrec.man $@

$(MAN)/man1/fmt.1:	doc/fmt.man
	install -lc doc/fmt.man $@

$(MAN)/man1/ref.1:	doc/ref.man
	install -lc doc/ref.man $@

inst.dos: $(DUMMY)
	for %a in ($(PROGS1)) do copy %a $(BIN)
	for %a in ($(PROGS2)) do copy %a $(BIN)
	$(CP) $(BIN)\ex$(COM) $(BIN)\vi$(COM)
	$(CP) $(BIN)\ex$(COM) $(BIN)\view$(COM)
	$(CP) $(BIN)\ex$(COM) $(BIN)\input$(COM)
	@echo ::: YOU STILL NEED TO EDIT YOUR AUTOEXEC.BAT FILE TO
	@echo ::: HAVE TEMP FILES BE PRESERVED AFTER A SYSTEM CRASH.

inst.xdos: $(DUMMY)
	$(CP) $(PROGS) $(BIN)
	$(CP) ex$(COM) $(BIN)/vi$(COM)
	$(CP) ex$(COM) $(BIN)/view$(COM)
	$(CP) ex$(COM) $(BIN)/input$(COM)
	@echo ::: YOU STILL NEED TO EDIT THE AUTOEXEC.BAT FILE TO
	@echo ::: HAVE TEMP FILES BE PRESERVED AFTER A SYSTEM CRASH.

inst.tos: $(DUMMY)
	$(CP) $(PROGS) $(BIN)

inst.os9: $(DUMMY)
	$(CP) $(PROGS) -rw=$(BIN)
	chd $(BIN); attr -epenprnpw $(PROGS)
	@echo ::: YOU STILL NEED TO EDIT THE /DD/STARTUP FILE, OR WHATEVER,
	@echo ::: TO HAVE TEMP FILES BE PRESERVED AFTER A SYSTEM CRASH.

inst.vms: $(DUMMY)
	@echo how?

##############################################################################
# clobbering is system dependent, because DOS's "copy" can't handle multiple
# filenames

clobber: clean clob.$(SYS)
	@echo -n

clean: $(DUMMY) clob.$(SYS)
	$(RM) *$(OBJ) 
	$(RM) elvis?.uue 
	$(RM) elvis?.sh 
	$(RM) core

clob.unx clob.tos clob.os9 clob.xdos clob.vms: $(DUMMY)
	$(RM) tags refs $(PROGS)

clob.dos : $(DUMMY)
	for %a in ($(PROGS1)) do $(RM) %a
	for %a in ($(PROGS2)) do $(RM) %a
	for %a in (tags refs) do $(RM) %a
	
##############################################################################

uue: elvis1.uue elvis2.uue elvis3.uue elvis4.uue elvis5.uue elvis6.uue elvis7.uue elvis8.uue

elvis1.uue: $(SRC1)
	tar cf elvis1.tar $(SRC1)
	compress -b13 elvis1.tar
	uuencode elvis1.tar.Z <elvis1.tar.Z >elvis1.uue
	$(RM) elvis1.tar*

elvis2.uue: $(SRC2)
	tar cf elvis2.tar $(SRC2)
	compress -b13 elvis2.tar
	uuencode elvis2.tar.Z <elvis2.tar.Z >elvis2.uue
	$(RM) elvis2.tar*

elvis3.uue: $(SRC3)
	tar cf elvis3.tar $(SRC3)
	compress -b13 elvis3.tar
	uuencode elvis3.tar.Z <elvis3.tar.Z >elvis3.uue
	$(RM) elvis3.tar*

elvis4.uue: $(SRC4)
	tar cf elvis4.tar $(SRC4)
	compress -b13 elvis4.tar
	uuencode elvis4.tar.Z <elvis4.tar.Z >elvis4.uue
	$(RM) elvis4.tar*

elvis5.uue: $(SRC5)
	tar cf elvis5.tar $(SRC5)
	compress -b13 elvis5.tar
	uuencode elvis5.tar.Z <elvis5.tar.Z >elvis5.uue
	$(RM) elvis5.tar*

elvis6.uue: $(SRC6)
	tar cf elvis6.tar $(SRC6)
	compress -b13 elvis6.tar
	uuencode elvis6.tar.Z <elvis6.tar.Z >elvis6.uue
	$(RM) elvis6.tar*

elvis7.uue: $(SRC7)
	tar cf elvis7.tar $(SRC7)
	compress -b13 elvis7.tar
	uuencode elvis7.tar.Z <elvis7.tar.Z >elvis7.uue
	$(RM) elvis7.tar*

elvis8.uue: $(SRC8)
	tar cf elvis8.tar $(SRC8)
	compress -b13 elvis8.tar
	uuencode elvis8.tar.Z <elvis8.tar.Z >elvis8.uue
	$(RM) elvis8.tar*

##############################################################################

sh: elvis1.sh elvis2.sh elvis3.sh elvis4.sh elvis5.sh elvis6.sh elvis7.sh elvis8.sh

elvis1.sh: $(SRC1)
	shar $(SRC1) >elvis1.sh

elvis2.sh: $(SRC2)
	shar $(SRC2) >elvis2.sh

elvis3.sh: $(SRC3)
	shar $(SRC3) >elvis3.sh

elvis4.sh: $(SRC4)
	shar $(SRC4) >elvis4.sh

elvis5.sh: $(SRC5)
	shar $(SRC5) >elvis5.sh

elvis6.sh: $(SRC6)
	shar $(SRC6) >elvis6.sh

elvis7.sh: $(SRC7)
	shar $(SRC7) >elvis7.sh

elvis8.sh: $(SRC8)
	shar $(SRC8) >elvis7.sh
