#
#  Makefile for System V BIT release.
#
SHELL=		/bin/sh

DEV=		rmt/ctape0

TYPE=		`uname -t`

SRCDIR=		../src
ETCDIR=		$(SRCDIR)/etc
CMDDIR=		$(SRCDIR)/cmds
FPUDIR=		$(SRCDIR)/fpu
OBJDIR=		$(SRCDIR)/objs
SRCDIR=		$(SRCDIR)/srcs

RELDIR=		.
RELETCDIR=	$(RELDIR)/etc.d
RELFPUDIR=	$(RELDIR)/fpu.d
RELSRCDIR=	$(RELDIR)/src.d
RELDOCDIR=	$(RELDIR)/doc.d
RELUTILDIR=	$(RELDIR)/util.d

ETCSRCFILES=	hosts \
		local_hostname
CMDFILES=	BIT \
		config.BIT \
		status.BIT \
		cart.BIT \
		check.BIT \
		clean.BIT \
		comp.BIT \
		exabyte.BIT \
		floppy.BIT \
		fp.BIT \
		grab.BIT \
		kernel.BIT \
		net.BIT \
		nine.BIT \
		purge.BIT \
		sysmsg.BIT \
		watch.BIT \
		prevmake
OBJFILES=	alarm.BIT \
		fptype \
		graphic.BIT \
		printer.BIT
OBJFPUFILES=	alogt \
		asint \
		atant \
		dasint \
		datant \
		dexpt \
		dlogt \
		dpowert \
		dsinht \
		dsint \
		dsqrtt \
		dtanht \
		dtant \
		expt \
		powert \
		sinht \
		sint \
		sqrtt \
		tanht \
		tant \
		parc \
		whet \
		doducd \
		linpackd \
		linpacks \
		alogt-mips2 \
		asint-mips2 \
		atant-mips2 \
		dasint-mips2 \
		datant-mips2 \
		dexpt-mips2 \
		dlogt-mips2 \
		dpowert-mips2 \
		dsinht-mips2 \
		dsint-mips2 \
		dsqrtt-mips2 \
		dtanht-mips2 \
		dtant-mips2 \
		expt-mips2 \
		powert-mips2 \
		sinht-mips2 \
		sint-mips2 \
		sqrtt-mips2 \
		tanht-mips2 \
		tant-mips2 \
		parc-mips2 \
		whet-mips2 \
		doducd-mips2 \
		linpackd-mips2 \
		linpacks-mips2
FPUSRCFILES=	alogt.FPREF \
		asint.FPREF \
		atant.FPREF \
		dasint.FPREF \
		datant.FPREF \
		dexpt.FPREF \
		dlogt.FPREF \
		dpowert.FPREF \
		dsinht.FPREF \
		dsint.FPREF \
		dsqrtt.FPREF \
		dtanht.FPREF \
		dtant.FPREF \
		expt.FPREF \
		powert.FPREF \
		sinht.FPREF \
		sint.FPREF \
		sqrtt.FPREF \
		tanht.FPREF \
		tant.FPREF \
		parc.FPREF \
		whet.in \
		whet.FPREF \
		big.in \
		big.FPREF \
		small.in \
		small.FPREF \
		linpackd.FPREF \
		linpacks.FPREF
FPUFILES=	$(OBJFPUFILES) $(FPUSRCFILES)
ROOTFILES=	ifconfig.BIT \
		util.d/newname \
		util.d/maxup \
		util.d/kvar \
		disk.BIT \
		disktest.BIT \
		mkdir.BIT \
		mport.BIT \
		port.BIT \
		mem.BIT
SRCFILES=	Makefile \
		alarm.c \
		fprevreg.s \
		fptype.c \
		graphic.c \
		mport.c \
		port.c \
		printer.c \
		test.c \
		mem.c
DOCFILES=	BIT_description \
		Install_Guide \
		Site_Install_guide \
		Add_test \
		rnote*
UTILFILES=	addexp \
		enet \
		qic.BIT
MISCFILES=	Makefile \
		CONFIGURATION \
		.cshrc \
		.login \
		cshrc \
		login
CREATEDFILES=	BITPATH \
		DATA.BIT \
		pass.full \
		pass.temp \
		pass.errs \
		LOG.BIT \
		SYSNAME \
		FPUTYPE \
		LOCK.BIT.clean

BITID=		430
BITGRP=		other
ROOTID=		root

default:	help

help:
		@echo ""
		@echo "Makefile for `cat VERSION*`"
		@echo ""
		@echo " The following targets are supported:"
		@echo "    install:   Full installation for manufacturing - can only be run once."
		@echo "		  - Save and replace /etc/hosts and /etc/local_hostname."
		@echo "		  - Setup file modes for all BIT files."
		@echo "		  - Add user bit to /etc/passwd (if not already done)."
		@echo "		  - Add QIC device files, if needed."
		@echo ""
		@echo "    bit:       Setup BIT - run any time BIT is modified."
		@echo "		  - Setup file modes for all BIT files."
		@echo "		  - Add user bit to /etc/passwd (if not already done)."
		@echo "		  - Add QIC device files, if needed."
		@echo ""
		@echo "    restore:   Return machine to original state."
		@echo "		  - Restore /etc files - any changes since install are lost."
		@echo "		  - Delete all BIT files and BIT QIC device files."
		@echo ""
		@echo "    reltape:   Make a BIT release tape."
		@echo "		  - Remove extraneous BIT files."
		@echo "		  - Copy all BIT files to tape."
		@echo ""

Iamroot:
		@if [ "`whoami`" != "root" ]; \
		then \
		  echo ""; \
		  echo " WARNING - This makefile is normally run by root."; \
		  echo ""; \
		fi


#
#  Setup file permissions and make sure there is a user bit in /etc/passwd.
#     This assumes the machine is already setup to run and therefore
#     does not need /etc/hosts and /etc/local_hostname replaced.
#     Targets short and perm are for histories sake.
#
bit BIT \
short perm:	Iamroot
		@echo " Setup BIT - run any time BIT is modified."
		@echo " Setting permissions for $(RELDIR) files"
		@chmod 0755      .
		@chown $(BITID)  .
		@chgrp $(BITGRP) .

		@chmod 0444       ./VERSION*
		@chown $(BITID)   ./VERSION*
		@chgrp $(BITGRP)  ./VERSION*

		@cp cshrc .cshrc
		@cp login .login
		@for i in $(MISCFILES); \
		do \
		  chmod 0644      ./$$i; \
		  chown $(BITID)  ./$$i; \
		  chgrp $(BITGRP) ./$$i; \
		done

		@chmod 0444      ./README
		@chown $(BITID)  ./README
		@chgrp $(BITGRP) ./README

		@for i in $(CMDFILES); \
		do \
		  chmod 0755      ./$$i; \
		  chown $(BITID)  ./$$i; \
		  chgrp $(BITGRP) ./$$i; \
		done

		@/bin/touch      ./REPORT.BIT
		@chmod 0664      ./REPORT.BIT
		@chown $(BITID)  ./REPORT.BIT
		@chgrp $(BITGRP) ./REPORT.BIT

		@for i in $(OBJFILES); \
		do \
		  chmod 0555      ./$$i; \
		  chown $(BITID)  ./$$i; \
		  chgrp $(BITGRP) ./$$i; \
		done

		@for i in $(ROOTFILES); \
		do \
		  chmod 04755     ./$$i; \
		  chown $(ROOTID) ./$$i; \
		  chgrp $(BITGRP) ./$$i; \
		done

		@chmod 0775      ./$(RELSRCDIR)
		@chown $(BITID)  ./$(RELSRCDIR)
		@chgrp $(BITGRP) ./$(RELSRCDIR)

		@for i in $(SRCFILES); \
		do \
		  chmod 0444      ./$(RELSRCDIR)/$$i; \
		  chown $(BITID)  ./$(RELSRCDIR)/$$i; \
		  chgrp $(BITGRP) ./$(RELSRCDIR)/$$i; \
		done

		@chmod 0775      ./$(RELDOCDIR)
		@chown $(BITID)  ./$(RELDOCDIR)
		@chgrp $(BITGRP) ./$(RELDOCDIR)

		@for i in $(DOCFILES); \
		do \
		  chmod 0444      ./$(RELDOCDIR)/$$i; \
		  chown $(BITID)  ./$(RELDOCDIR)/$$i; \
		  chgrp $(BITGRP) ./$(RELDOCDIR)/$$i; \
		done

		@chmod 0775      ./$(RELUTILDIR)
		@chown $(BITID)  ./$(RELUTILDIR)
		@chgrp $(BITGRP) ./$(RELUTILDIR)

		@for i in $(UTILFILES); \
		do \
		  chmod 0555      ./$(RELUTILDIR)/$$i; \
		  chown $(BITID)  ./$(RELUTILDIR)/$$i; \
		  chgrp $(BITGRP) ./$(RELUTILDIR)/$$i; \
		done

		@if [ -d ./$(RELFPUDIR) ]; \
		then \
		  chmod 0775      ./$(RELFPUDIR); \
		  chown $(BITID)  ./$(RELFPUDIR); \
		  chgrp $(BITGRP) ./$(RELFPUDIR); \
		fi

		@for i in $(FPUSRCFILES); \
		do \
		  if [ -f "./$(RELFPUDIR)/$$i" ]; \
		  then \
		    chmod 0444      ./$(RELFPUDIR)/$$i; \
		    chown $(BITID)  ./$(RELFPUDIR)/$$i; \
		    chgrp $(BITGRP) ./$(RELFPUDIR)/$$i; \
		  fi; \
		done

		@for i in $(OBJFPUFILES); \
		do \
		  if [ -f "./$(RELFPUDIR)/$$i" ]; \
		  then \
		    chmod 0555      ./$(RELFPUDIR)/$$i; \
		    chown $(BITID)  ./$(RELFPUDIR)/$$i; \
		    chgrp $(BITGRP) ./$(RELFPUDIR)/$$i; \
		  fi; \
		done

		@chmod 0775      ./$(RELETCDIR)
		@chown $(BITID)  ./$(RELETCDIR)
		@chgrp $(BITGRP) ./$(RELETCDIR)

		@for i in $(ETCSRCFILES); \
		do \
		  chmod 0444      ./$(RELETCDIR)/$$i; \
		  chown $(BITID)  ./$(RELETCDIR)/$$i; \
		  chgrp $(BITGRP) ./$(RELETCDIR)/$$i; \
		done

		@for i in $(CREATEDFILES); \
		do \
		  if [ -f "$$i" ]; \
		  then \
		    chmod 0664      ./$$i; \
		    chown $(BITID)  ./$$i; \
		    chgrp $(BITGRP) ./$$i; \
		  fi; \
		done

		@echo " Checking that /etc/passwd contains user bit"
		@if [ "`grep '^bit::' /etc/passwd || true`" = "" ]; \
		then \
			echo "   /etc/passwd does not contain user bit - adding it now."; \
			if [ -f ./$(RELETCDIR)/passwd.sav ]; \
			then \
				echo "   Not saving /etc/passwd - $(RELETCDIR)/passwd.sav already exists."; \
			else \
				echo "   Saving /etc/passwd"; \
				cp /etc/passwd ./$(RELETCDIR)/passwd.sav; \
			fi; \
			echo "   Adding user bit to /etc/passwd"; \
			echo "bit::430:40::`pwd`:/bin/csh" >> /etc/passwd; \
		fi

		@echo " Checking that BIT has QIC device files for drive 0"
		@$(RELUTILDIR)/qic.BIT 0

		@echo make $@ done

previous:		
		@/bin/sh $(RELDIR)/prevmake

#
#  Replace /etc/hosts and /etc/local_hostname.  Can only be run once.
#  The first line of /etc/local_hostname is preserved and the network
#  gets restarted to force local_hostname to be reread.
#
etc:		previous
		@echo " Full installation for manufacturing - can only be run once."
		@echo "    /etc/hosts and /etc/local_hostname are being saved and replaced."
		@echo "    These files get restored when BIT is restored."
		@echo ""
		@echo " Updating /etc/hosts"
		@cp /etc/hosts ./$(RELETCDIR)/hosts.sav
		@cp ./$(RELETCDIR)/hosts /etc/hosts

		@if [ -f /etc/local_hostname ]; \
		then \
		  echo " Updating /etc/local_hostname and restarting tcp"; \
		  cp /etc/local_hostname ./$(RELETCDIR)/local_hostname.sav; \
		  sed -e "1s/.*/`sed -n 1p ./$(RELETCDIR)/local_hostname.sav`/" \
		    ./$(RELETCDIR)/local_hostname > /etc/local_hostname; \
		  /etc/init.d/tcp start 1> /dev/null 2>&1 ;\
		else \
		  echo " Creating /etc/local_hostname and starting tcp"; \
		  cp ./$(RELETCDIR)/local_hostname /etc/local_hostname; \
		  /usr/ucb/hostname `sed -n '1p' /etc/local_hostname`;\
		  /etc/init.d/tcp start 1> /dev/null 2>&1 ;\
		fi

		@echo make $@ done

#
#  Installation of BIT in Manufacturing - assumes /etc/hosts and
#     /etc/local_hostname are defaults and must be replaced.
#
install:	Iamroot etc bit
		@cat /dev/null > ./REPORT.BIT

#
#  Restoration of the OS.
#
restore:	Iamroot
		@echo " Return machine to original state."
		@echo " Restoring original /etc files"
		@if [ -f ./$(RELETCDIR)/hosts.sav ]; \
		then \
			cp ./$(RELETCDIR)/hosts.sav /etc/hosts; \
		fi
		@if [ -f ./$(RELETCDIR)/passwd.sav ]; \
		then \
			cp ./$(RELETCDIR)/passwd.sav /etc/passwd; \
		fi
		@if [ -f ./$(RELETCDIR)/local_hostname.sav ]; \
		then \
			cp ./$(RELETCDIR)/local_hostname.sav /etc/local_hostname; \
		fi
		@if ls /dev/rmt/BIT-Q* > /dev/null 2>&1 ; \
		then \
			echo " Removing BIT's QIC device files: /dev/rmt/BIT-Q*"; \
			rm -f /dev/rmt/BIT-Q* > /dev/null 2>&1 ; \
		fi
		@echo " Removing BIT directory"
		@rm -fr `pwd`

#
#  Write a BIT release tape.
#
reltape:
		@echo " Cleaning up BIT directory."
		@cat /dev/null > ./REPORT.BIT
		@rm -f $(CREATEDFILES)
		@echo " Temporarily moving $(RELETCDIR)/*.sav to .bak - must be restored."
		@if [ -f ./$(RELETCDIR)/hosts.sav ]; \
		then \
			mv ./$(RELETCDIR)/hosts.sav /etc/hosts.bak; \
		fi
		@if [ -f ./$(RELETCDIR)/passwd.sav ]; \
		then \
			mv ./$(RELETCDIR)/passwd.sav /etc/passwd.bak; \
		fi
		@if [ -f ./$(RELETCDIR)/local_hostname.sav ]; \
		then \
			mv ./$(RELETCDIR)/local_hostname.sav /etc/local_hostname.bak; \
		fi
		@echo " Writing a BIT release tape"
		@cd ..; tar cvFf /dev/$(DEV) ./bit
		@echo " Restoring $(RELETCDIR)/*.bak to .sav."
		@if [ -f ./$(RELETCDIR)/hosts.bak ]; \
		then \
			mv ./$(RELETCDIR)/hosts.bak /etc/hosts.sav; \
		fi
		@if [ -f ./$(RELETCDIR)/passwd.bak ]; \
		then \
			mv ./$(RELETCDIR)/passwd.bak /etc/passwd.sav; \
		fi
		@if [ -f ./$(RELETCDIR)/local_hostname.bak ]; \
		then \
			mv ./$(RELETCDIR)/local_hostname.bak /etc/local_hostname.sav; \
		fi
		@echo make $@ done
