###########################################################
#	lmkmeta included part :ts=4

###########################################################
#	required volumes/assigns:
#	V36:		- should be official server V36 directory
#	VINCLUDE:	- (possibly local) assign for sys includes,
#					will be V36:include.strip for official build
#	VLIB:		- (possibly local or ram) assign for libs dir.
#					will be V36:lib for official build
#	MYINCLUDE:	- additional search path for temp include files which
#					have not yet been checked in.
#					will be *empty* for official build
#	QUAD:		- used for lattice, and by default precompiled includes.
#				  should be very fast access, e.g., RAM: or RAD:.

VERNUM=36
VERDIR=V$(VERNUM):

# make the standard default target "all"
default: all

###########################################################
#	tools
RX=rx
REXX=$(VERDIR)aug/rexx/
#REXX=work:amiga/native/rexx/

ASM=casm -a
BUMPREV=bumprev
LINK=blink
STRIP=blink QUIET NODEBUG from
# mminstall [strip=[<stripdir>]] dir=<destdir> files=[with <withfile>|{files}]
MMINSTALL=$(RX) $(REXX)mminstall.rexx
CC=lc
RM=delete
AUTODOC=autodoc
VERMACRO=$(RX) $(REXX)vermacro.rexx

# a little weird
ZOOTMP=zoo.tmp
ZOOTMP2=zoo.tmp2
FEEDZOO=$(RX) >$(ZOOTMP2) $(REXX)feedzoo.rexx
#FEEDZOO=$(REXX)feedzoo.rexx >$(ZOOTMP2) 


###########################################################
#	standard build implicit rules
.c.o:
	@echo "$*.c: $(CFLAGS)" NOLINE
	@$(CC) $(CFLAGS) $(CINCL) $*

.asm.o:
	@echo "$*.asm: $(AFLAGS)"
	@$(ASM) $*.asm $(AFLAGS) $(AINCL) -o $*.o

#	disassembled listing; you must declare "foo.lst: foo.o"
.o.lst:
	omd >$*.lst $*.o $*.c

#	automatic revision bumping
#	doesn't pass all dependency files, ala makemeta
.rev.i .rev.h:
	$(BUMPREV) $(VERNUM) $* $*.rev $?

###########################################################
#	standard build file groups

HFILES= $(EXTERNHFILES) $(MYHFILES)
IFILES= $(EXTERNIFILES) $(MYIFILES)

#	everything copied to SRCPATH, and checked into RCS
ALLSRC=$(AFILES) $(CFILES) $(HFILES) $(IFILES) \
	$(MAKEFILE) $(MISCSRC) $(SFDFILE)

###########################################################
#	standard build targets

LFILES=$(MYLIBS) VLIB:lattice/lc.lib VLIB:amiga.lib

$(SUBSYSNAME)_lib.i:	$(SUBSYSNAME)_lib.sfd
	sfd -i $(SUBSYSNAME)_lib.sfd

$(SUBSYSNAME).with: $(MAKEFILE) $(METAFILE)
	@echo $(SUBSYSNAME).with:
	@copy to $(SUBSYSNAME).with <from <
	$(LFLAGS)
	from $(STARTUP) $(FIRSTO) $(OFILES)
	lib $(LFILES)
	to $(SUBSYSNAME).ld
	<

#	default executable build
$(SUBSYSNAME).ld: $(OFILES) $(FIRSTO) $(LFILES) $(SUBSYSNAME).with
	@echo "link $(SUBSYSNAME).ld"
	$(LINK) with $(SUBSYSNAME).with
	$(STRIP) $(SUBSYSNAME).ld to $(SUBSYSNAME).ld.strip

#	ram copy of precompiled include file
$(SYMDIR)$(SYM): $(SYM)
	copy $(SYM) $(SYMDIR)

#	disk copy of precompiled include file
#	ZZZ: would be better to also depend on $(HEADERUPDATE)
$(SYM): $(SYMSRC)
	@echo "	precompiling $(SYMSRC) to $(SYM)"
	lc1 -. $(CINCL) -ph -o$(SYM) $(SYMSRC)

#	revision bumping
$(SUBSYSNAME)_rev.h $(SUBSYSNAME)_rev.i: $(SUBSYSNAME)_rev.rev \
		$(OFILES)

$(SUBSYSNAME)_rev.rev:
	echo >$(SUBSYSNAME)_rev.rev 0

#	zoo file
# if this depends on ALLSRC
$(SUBSYSNAME).zoo: FRC
	-@$(RM) $(SUBSYSNAME).zoo
	@copy to zoo.tmp <from <
	$(ALLSRC)
	$(BINFILES)
	<
	@$(FEEDZOO) zoo.tmp 
	@zoo	<zoo.tmp2 aI $(SUBSYSNAME).zoo
	@$(RM) zoo.tmp
	@$(RM) zoo.tmp2

zoo: $(SUBSYSNAME).zoo


#	autodocs
$(SUBSYSNAME).doc: FRC
	autodoc >utility.doc -C -c -a -I -FRAM:adoctemp $(CFILES) $(AFILES)

doc: $(SUBSYSNAME).doc

#	force target never exists
FRC:
	@echo "" NOLINE

###########################################################
#	installation directories

# initial path components
SYSINCL=$(VERDIR)include.strip/
SYSINCLNS=$(VERDIR)include/
LIBDIR=$(VERDIR)lib/

# directory path terminators
SFD=$(LIBDIR)sfd
SRCPATH=$(VERDIR)$(SRCDIR)

###########################################################
#	installation targets

backup:
	@$(VERMACRO) BACKUPDIR $(BACKUPDIR) files=FRC
	copy \#? $(BACKUPDIR)

clean:
	$(RM) \#?.o \#?.sym \#?.lst \#?.obj \#?.bak \#?.tmp

#	check in includes, sfd
#	strip must be in mminstall
headers:
	@$(VERMACRO) INCDIR $(INCDIR) files=$(EXTERNHFILES) $(EXTERNIFILES)
	@$(MMINSTALL) strip=$(SYSINCL)$(INCDIR) dir=$(SYSINCLNS)$(INCDIR)  \
	files= $(EXTERNHFILES) $(EXTERNIFILES)
	@$(MMINSTALL) dir=$(SFD) files=$(SFDFILE)

#	check in source, executable, sfd, includes, docs
#
#	check RCS (ZZZ not implemented)
#	copy source, .ld, .library, fd, includes, to source dir
#	copy docs
#	copy external includes, stripped and nostrip
internal: $(BINFILES)
	@$(VERMACRO) SRCDIR $(SRCDIR) files=source_is
	@echo "copying source to $(SRCPATH)"
	@$(MMINSTALL) dir=$(SRCPATH) files=<with <
	$(ALLSRC)
	<
	@$(MMINSTALL) dir=$(SRCPATH) files=$(SUBSYSNAME)_rev.rev

	@$(VERMACRO) BINDIR $(BINDIR) files=$(BINFILES)
	@echo "copying binaries to $(VERDIR)$(BINDIR)"
	@$(MMINSTALL) dir=$(VERDIR)$(BINDIR) files=$(BINFILES)

	@$(VERMACRO) SRCDIR $(SRCDIR) files=$(SUBSYSNAME).doc $(MISCDOC)
	@echo "copying docs to $(SRCPATH)"
	@$(MMINSTALL) dir=$(SRCPATH) files= $(SUBSYSNAME).doc $(MISCDOC)

	@$(VERMACRO) DOCDIR $(DOCDIR) files=$(SUBSYSNAME).doc $(MISCDOC)
	@echo "copying docs to $(VERDIR)$(DOCDIR)"
	@$(MMINSTALL) dir=$(VERDIR)$(DOCDIR) files= $(SUBSYSNAME).doc $(MISCDOC)

	@$(VERMACRO) INCDIR $(INCDIR) files=$(EXTERNHFILES) $(EXTERNIFILES)
	@echo "copying external includes"
	@$(MMINSTALL) strip=$(SYSINCL)$(INCDIR) dir=$(SYSINCLNS)$(INCDIR) \
	files= $(EXTERNHFILES) $(EXTERNIFILES)

	@echo "copying .sfd file (if any)"
	@$(MMINSTALL) dir=$(SFD) files=$(SFDFILE)
