# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987 Sequent Computer Systems, Inc.
# All rights reserved
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.

# $Header: Makefile 2.50 87/11/05 $

# For a complete build of entire system, type
# "make build MACHINE=machine VERSION=n.n"
# or optionally, for a complete build with the intention of making a tape, type
# "make build MACHINE=machine VERSION=n.n DISK=zd1 DISKTYPE=m2333k"
# and then to actually make the distribution tape, type
# "make tape MACHINE=machine DISK=zd1"
# see table below for intended disk partition usage

# directories to be cleaned

CLEAN= src.k src.nfs.k tools.src.k compilers.k

# files which must be updated with the new DYNIX version number

VERSIONFILES=	src/etc/getty/gettytab \
		src/sys/$(MACHINE)/conf/newvers.sh \
		src/etc/version/mkvers.sh \
		src/usr.etc/telnetd.c \
		src/dist/proto.sh \
		src/5cmd/.adm/issue.sh

error:
	@echo "Sorry, this makefile not meant to be invoked without target"
	@exit 1;

build: buildcheck toolprep
	@exec csh -c '\
	date && umask 022 && \
	set a = ( `printenv | sed -e "/^USER=/d" -e "s/=.*//"` ) && \
	unsetenv $$a && \
	setenv PATH "" && \
	time /ccsbin/ccsmake -M $(MACHINE) $(MFLAGS) \
	DISK=$(DISK) DISKTYPE=$(DISKTYPE) -V `/bin/pwd` VERSION=$(VERSION) all && \
	echo ----------------------------- && \
	echo Dont forget to run make clean && \
	echo -----------------------------' >> makelog 2>&1

buildcheck:
	@sh -c 'if [ "root" != `/usr/ucb/whoami` ]; then \
		echo error: you are not root!; \
		echo usage: make build MACHINE=machine VERSION=n.n; \
		exit 1; \
	else \
		exit 0; \
	fi' >> makelog 2>&1
	@sh -c 'if [ "$(VERSION)" = "" ]; then \
		echo error: VERSION omitted!; \
		echo usage: make build MACHINE=machine VERSION=n.n; \
		exit 1; \
	else \
		exit 0; \
	fi' >> makelog 2>&1
	@sh -c 'if [ "$(MACHINE)" = "" ]; then \
		echo error: MACHINE omitted!; \
		echo usage: make build MACHINE=machine VERSION=n.n; \
		exit 1; \
	else \
		exit 0; \
	fi' >> makelog 2>&1
	@sh -c "if [ \"$(DISK)\" = \"\" ]; then \
		/bin/df . | awk 'NR == 2 \
		{ if (\$$4 < 40000) { \
			print \"There is not enough space in \" \$$6 \" for a build\"; \
			exit 1; \
		  } \
		  exit 0; \
		}'; \
	else \
		exit 0; \
	fi" >> makelog 2>&1
	@/etc/pstat -T | sed 's;.*/;;' > build.pstat
	@awk 'BEGIN { s = "is too small for a build"; } \
	/mfiles/	{ if ($$1 <   200) { printf "DYNIX mfile table %s (%d < 500)\n", s, $$1; exit 1; } next; } \
	/files/		{ if ($$1 <   500) { printf "DYNIX file table %s (%d < 500)\n", s, $$1; exit 1; } next; } \
	/inodes/	{ if ($$1 <   250) { printf "DYNIX inode table %s (%d < 250)\n", s, $$1; exit 1; } next; } \
	/processes/	{ if ($$1 <   400) { printf "DYNIX proc table %s (%d < 400)\n", s, $$1; exit 1; } next; } \
	/swap/		{ if ($$1 < 80000) { printf "DYNIX swap space %s (%d < 80000)\n", s, $$1; exit 1; } next; } \
	' build.pstat >> makelog 2>&1
	@rm -f build.pstat

toolprep:
	@sh -c '\
	if [ "$(DISK)" != "" ] ;then \
		/etc/umount /dev/$(DISK)h; \
		/etc/umount /dev/$(DISK)a; \
	fi; \
	cd tools.src && make hostarchvers && cd .. && \
	A_V=`tools.src/hostarchvers` && \
	rm -fr .ccsmark $(MACHINE) && \
	umask 022 && mkdir $(MACHINE) $(MACHINE)/tools $(MACHINE)/tools/$$A_V $(MACHINE)/tools/$$A_V/lib && \
	install -c /bin/make $(MACHINE)/tools/$$A_V/make && \
	install -c /usr/bin/install $(MACHINE)/tools/$$A_V/install && \
	install -c -m 644 tools.src/makerules.src $(MACHINE)/tools/$$A_V/makerules' >> makelog 2>&1

all: version dist.i headers tools dynix & system5 & documentation whatis & dynix.nfs & doc.nfs

dist.i	\
headers	\
libraries	:; cd src && make     $(MFLAGS) $(MRULES) $@
		   sh -c "if [ ! -d src.nfs ]; then exit 0; else \
		   		cd src.nfs && make $(MFLAGS) $(MRULES) `basename $@ .i`; \
			  fi"

whatis		:; cd src/doc && make $(MFLAGS) $(MRULES) whatis >> ../../makelog.doc 2>&1

dynix		:; -cd src && make -P5 $(MFLAGS) $(MRULES) dynix.libraries && \
			make -P4 $(MFLAGS) $(MRULES) dynix.cmds 
dynix.nfs	:; sh -c "if [ ! -d src.nfs ]; then exit 0; else \
				cd src.nfs && make -P4 $(MFLAGS) $(MRULES) nfs.cmds; \
			  fi"

system5		:; -cd src && make -P8 $(MFLAGS) $(MRULES) 5lib && \
			make -P4 $(MFLAGS) $(MRULES) 5cmd.i

documentation	:; -cd src/usr.bin/troff && make $(MFLAGS) $(MRULES) tmac && \
		   	cd  ../.. && make -P6 $(MFLAGS) $(MRULES) doc.i

doc.nfs		:; sh -c "if [ ! -d src.nfs ]; then exit 0; else \
				cd src.nfs && make $(MFLAGS) $(MRULES) doc.i; \
			  fi"

# see "src/Makefile" target "dynix.cmds"

compilersandtools:; cd compilers && \
			time make -P4 $(MFLAGS) $(MRULES) all install  >>../makelog.compilers 2>&1

tools		:; cd tools.src && \
			time make $(MFLAGS) $(MRULES) INSTALL clean >>../makelog.tools 2>&1

## Update the version numbers.

version:
	@echo VERSION=$(VERSION); test $(VERSION)
	@for file in $(VERSIONFILES); do \
		echo ...updating $$file; \
		rm -f $$file.old; \
		cp $$file $$file.old; \
		chmod +w $$file; \
		sed -e "s/DYNIX(R) *V[^ ]*/DYNIX(R) V$(VERSION)/" \
		    -e "s/DYNIX(R)\(  *[^V]\)/DYNIX(R) V$(VERSION)\1/" \
		    -e "s/- DYNIX\(  *\)V[^ ]*/- DYNIX\1V$(VERSION)/" \
		    -e "s/- DYNIX\(  *[^V]\)/- DYNIX V$(VERSION)\1/" \
		    $$file.old >$$file; \
		rm -f $$file.old; \
	done

## Build from the source distribution

srcbuild	: headers dynix & system5 & documentation whatis & dynix.nfs & doc.nfs &

##  Set up headers and libraries in a source distribution.

srcdist		:; cd src && \
			make $(MFLAGS) $(MRULES) dist.i headers libraries

clean		:;
		/ccsbin/ccsmake -M $(MACHINE) $(MFLAGS) -V `/bin/pwd` cleanup

cleanup		:& $(CLEAN)
$(CLEAN)	:; sh -c "if [ ! -d `basename $@ .k` ]; then exit 0; else \
				cd `basename $@ .k` && \
					make $(MFLAGS) $(MRULES) clean; \
			  fi"

# The following targets assume the results from a build span disk partitions
# (of eagle, m2333k or m2351a) as follows:
#
# partition	contents
# ---------	--------
# a		tmpos (root file system only)
# b		miniroot file system
# h		tmpos/usr (usr file system only)

tape		:
	@exec csh -c '/ccsbin/ccsmake -V `/bin/pwd` -M $(MACHINE) $(MFLAGS) \
	DISK=$(DISK) maketape' > makelog.tape 2>&1

maketape	:
	@exec sh -c '\
	if [ "$(DISK)" != "" ] ;then \
		$(DESTDIR)/etc/buildmini $(DISK) $(DESTDIR) && \
		/bin/cp $(DESTDIR)/etc/maketape /etc/maketape$$$$ && \
		/etc/maketape$$$$ $(DISK) $(DESTDIR) && \
		rm -f /etc/maketape$$$$; \
	else \
		echo error: DISK omitted!; \
		echo example usage: make tape MACHINE=machine DISK=zd1; \
		exit 1; \
	fi'
