#! /bin/sh
#	BSDI $Id: end_install,v 1.3 2000/10/13 16:56:31 polk Exp $

# INSTROOT is normally set by installsw.
if [ "$INSTROOT" = "/" ] ; then
	INSTROOT=
fi

# Nothing to do if we installed new config files...
if [ -f /.install-floppy ] ; then
	if [ ! -d $INSTROOT/etc/X11 -a -d $INSTROOT/etc/X11.bsdi ]; then
		mv $INSTROOT/etc/X11.bsdi $INSTROOT/etc/X11
	fi
	exit 0;
fi

echo "Updating system configuration files"

# Apply missing patches that matter for merges...
f=$INSTROOT/etc/rc
if grep "rc,v 2.92 " $f > /dev/null; then
	echo "Patching $f with BSDI patch M420-001"
	patch $f M420-001-rc.mod > /dev/null 2>&1
fi
f=$INSTROOT/etc/services
if grep "services,v 2.27 " $f > /dev/null; then
	echo "Patching $f with BSDI patch M420-001"
	patch $f M420-001-services.mod > /dev/null 2>&1
fi
f=$INSTROOT/etc/pccard.conf
if grep "pccard.conf,v 1.19 " $f > /dev/null; then
	echo "Patching $f with BSDI patch M420-001"
	patch $f M420-001-pccard.conf.mod > /dev/null 2>&1
fi
f=$INSTROOT/etc/sshd_config
grep "KbdInteractiveAuthentication" $f > /dev/null
if [ $? != 0 ]; then
	echo "Patching $f with BSDI patch M420-010"
	patch $f M420-010-sshd_config.mod > /dev/null 2>&1
fi

# START BUILD_CRB_AUTO
# AUTOMATICALLY GENERATED, SEE ../build_crb
# Script to patch or replace system files based on checksums of
# the previous versions of the file.
# $1 == directory
# $2 == file name
# $3 == replace, patch and rcs file prefix (defaults to $2)
# $4 == original checksum
fupdate()
{
	V="BSD/OS 4.3"
	LOG=$INSTROOT/var/db/install.log

	# If no replace/patch file ($3) specified, use the file name ($2)
	if [ "X$3" = "X" ]; then
		pfx=$2
	else
		pfx=$3
	fi

	# If the file doesn't exist, copy in our version.
	if [ ! -f $1/$2 ]; then
		# If the directory doesn't exist, create it.
		# XXX
		# Note, we're not chowning intermediate directories.
		if [ ! -d $1 ]; then
			mkdir -p $1
			chown root.wheel $1
			chmod 755 $1
		fi
		cp ${pfx}.bsdi $1/$2 || {
			echo "$V: cannot install $1/$2" >> $LOG
			cp ${pfx}.bsdi $1/$2 >>$LOG 2>&1
			read -p 'Enter carriage-return to continue:' answer
			return
		}
		echo "Installing $1/$2"
		chown root.wheel $1/$2
		chmod 644 $1/$2
		echo "$V: installed BSDI version of $1/$2" >> $LOG
		return
	fi

	# If they got an early copy of the file, ignore it.
	if cmp $1/$2 ${pfx}.bsdi > /dev/null; then
		return
	fi

	echo "Updating $1/$2"

	# If the file is as previously shipped, install the new one.
	#
	# XXX
	# Use string comparison: numerical comparisons in test will overflow.
	sum=`cksum $1/$2 | awk '{print $1}'`
	if [ $sum = $4 ]; then
		cp ${pfx}.bsdi $1/$2
		echo "$V: installed BSDI version of $1/$2" >> $LOG
		return
	fi

	# If there's a patch file, try and do a patch.
	if [ -s ${pfx}.patch ] ; then
		cp $1/$2 $2.system
		if patch -N $2.system ${pfx}.patch > /dev/null 2>&1 ; then
			# Bring the RCS line up to date.
			pt='BSDI[ 	].*,v [0-9][0-9]*.[0-9][0-9]*'
			if [ -s ${pfx}.rcs ] &&
			    grep "${pt}" $2.system > /dev/null; then
				(echo "/${pt}/d" && \
				 echo '-' && \
				 echo ".r ${pfx}.rcs" && \
				 echo 'w' && \
				 echo 'q') | ed $2.system > /dev/null
			fi

			cp $2.system $1/$2
			rm -f $2.system.orig

			echo "$V: merged $1/$2 successfully" >> $LOG
			return
		fi

		# Patch failure.
		# Copy the .rej and attempted patch file into place.
		cp $2.system.rej $1/$2.rej
		chown root.wheel $1/$2.rej
		chmod 444 $1/$2.rej
		cp $2.system $1/$2.patch
		chown root.wheel $1/$2.patch
		chmod 444 $1/$2.patch

		rm -f $2.system.orig $2.system.rej
	fi

	# Upgrade failure.
	# Copy the .bsdi file into place.
	cp ${pfx}.bsdi $1/$2.bsdi
	chown root.wheel $1/$2.bsdi
	chmod 444 $1/$2.bsdi

	echo "$V: UNABLE TO MERGE $1/$2" >> $LOG
	echo "$V:    the BSD/OS version to merge is $1/${pfx}.bsdi" \
	    >> $LOG
	if [ -s ${pfx}.patch ] ; then
		echo "$V:    the BSD/OS attempted patch is $1/$2.patch" \
		    >> $LOG
		echo "$V:    the BSD/OS failed patches is $1/$2.rej" \
		    >> $LOG
	fi
	echo "UNABLE TO UPGRADE $1/$2."
	echo "	The $1/${pfx}.bsdi file contains the current"
	echo "	BSDI version of this file, and should be manually integrated."
	if [ -s ${pfx}.patch ] ; then
		echo "	In addition, the $2.patch file contains a copy of the"
		echo "	file after all possible merges were made, and the "
		echo "	$2.rej file contains the list of the parts of the "
		echo "	file that could not be merged."
	fi
	read -p 'Enter carriage-return to continue:' answer
}
f="$INSTROOT/dev/MAKEDEV.local" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 555 "$f"
fupdate "$INSTROOT/etc" "changelist" "changelist" "45023820"
f="$INSTROOT/etc/changelist" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/crontab" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/csh.cshrc" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/csh.login" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/csh.logout" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/daily" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/daily.local" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/dialer.rules.local" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 644 "$f"
f="$INSTROOT/etc/disktab" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/dm.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "gettytab" "gettytab" "1881082224"
f="$INSTROOT/etc/gettytab" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "group" "group" "2541880774"
f="$INSTROOT/etc/group" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/hosts" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "ifhp.conf" "ifhp.conf" "0"
f="$INSTROOT/etc/ifhp.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 444 "$f"
fupdate "$INSTROOT/etc" "inetd.conf" "inetd.conf" "1148907325"
f="$INSTROOT/etc/inetd.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "inetd.conf.nowrappers" "inetd.conf.nowrappers" "2942030222"
f="$INSTROOT/etc/inetd.conf.nowrappers" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/irs.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/login.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/mail/aliases" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/mail.rc" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "mailcap" "mailcap.bsdi" "1647801754"
f="$INSTROOT/etc/mailcap" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/man.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/mime.types" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/monthly" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/monthly.local" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/mtools.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/mtree/special" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 600 "$f"
f="$INSTROOT/etc/namedb/root.cache" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/netscripts/Demand.down" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Demand.login" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Demand.up" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Dialin.down" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Dialin.login" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Dialin.up" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Dialout.down" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Dialout.login" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/Dialout.up" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 550 "$f"
f="$INSTROOT/etc/netscripts/HOME/.forward" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 440 "$f"
f="$INSTROOT/etc/netscripts/SKEL/dot.forward" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 440 "$f"
fupdate "$INSTROOT/etc" "netstart.proto" "netstart.proto" "3998143042"
f="$INSTROOT/etc/netstart.proto" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/networks" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "pccard.conf" "pccard.conf" "1001635290"
f="$INSTROOT/etc/pccard.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/phones" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/protocols" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "rc" "rc" "3179041928"
f="$INSTROOT/etc/rc" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/rc.configure/0.keyboard.0" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 555 "$f"
f="$INSTROOT/etc/rc.configure/1.configure.0" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 555 "$f"
f="$INSTROOT/etc/rc.hardware/0.apm.0" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 444 "$f"
f="$INSTROOT/etc/rc.hardware/1.cs.0" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 444 "$f"
fupdate "$INSTROOT/etc" "rc.local" "rc.local" "3635042735"
f="$INSTROOT/etc/rc.local" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/remote" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/rpc" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "services" "services" "100507559"
f="$INSTROOT/etc/services" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/shells" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "shlib.map" "shlib.map" "2851805146"
f="$INSTROOT/etc/shlib.map" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/sliphome/slip.hosts" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 640 "$f"
f="$INSTROOT/etc/sliphome/slip.login" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 755 "$f"
f="$INSTROOT/etc/sliphome/slip.logout" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 755 "$f"
fupdate "$INSTROOT/etc" "ssh_config" "ssh_config" "4249103738"
f="$INSTROOT/etc/ssh_config" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "sshd_config" "sshd_config" "1574733469"
f="$INSTROOT/etc/sshd_config" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/syslog.conf" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/ttys" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
fupdate "$INSTROOT/etc" "ttys.conf" "ttys.conf" "4145662827"
f="$INSTROOT/etc/ttys.conf" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 444 "$f"
f="$INSTROOT/etc/ttys.conf.local" && \
    [ -f "$f" ] && chown root.netdial "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/CONFIG" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/L-devices" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/L-dialcodes" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/L.aliases" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/L.cmds" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/L.sys" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 640 "$f"
f="$INSTROOT/etc/uucp/USERFILE" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/daily" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 755 "$f"
f="$INSTROOT/etc/uucp/remote.unknown" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 644 "$f"
f="$INSTROOT/etc/uucp/uuxqt_hook" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 755 "$f"
f="$INSTROOT/etc/uucp/weekly" && \
    [ -f "$f" ] && chown uucp.uucp "$f" && chmod 755 "$f"
f="$INSTROOT/etc/weekly" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/etc/weekly.local" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/root/.cshrc" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/root/.klogin" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 600 "$f"
f="$INSTROOT/root/.login" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
f="$INSTROOT/root/.profile" && \
    [ -f "$f" ] && chown root.wheel "$f" && chmod 644 "$f"
# END BUILD_CRB_AUTO

# Run chpass to add the new postgres login
echo "Adding postgres user to master password file and databases"
if [ X$INSTROOT = X ]; then
	chpass -a "$(cat postgresuser)" >/dev/null 2>&1
else
	chroot $INSTROOT chpass -a "$(cat postgresuser)" >/dev/null 2>&1
fi

# If they have a /etc/dhcpd.conf, make sure they have a ddns line..
f="$INSTROOT/etc/dhcpd.conf";
if [ -f $f ]; then
	grep ddns-update-style $f >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		mv $f $f.orig
		cat <<EOF >$f
# ddns lines added by upgrade; ddns-style is required by the new dhcpd
ddns-updates off;
ddns-update-style ad-hoc;

EOF
		cat $f.orig >>$f
		chown root.wheel $f
		chmod 644 $f
		echo "Added ddns configuration (off) to existing dhcpd.conf"
	fi
fi

# Create any new devices.
# XXX NONE for 4.3
#echo "Creating new devices"
#f=$INSTROOT/dev/foo0 
#rm -f ${f}
#mknod ${f} c 66 0
#chmod 666 ${f}

# If we didn't fix the /etc/rc file, we're in serious trouble.
# Do it anyway, and hope it works.
f=$INSTROOT/etc/rc
if [ -f $f.patch ]; then
	mv -i $f $f.orig
	mv -i $f.bsdi $f
	echo "$V: $f: UNABLE TO MERGE!!" >> $LOG
	echo "$V: $f: UPGRADED IT ANYWAY, SO THE SYSTEM WILL BOOT!!" >> $LOG
	echo "$V:    the original version is $f.orig" >> $LOG
	echo "$V: $f: UNABLE TO MERGE!!"
	echo "$V: $f: UPGRADED IT ANYWAY, SO THE SYSTEM WILL BOOT!!"
	echo "$V:    the original version is $f.orig"
	read -p 'Enter carriage-return to continue:' answer
fi

exit 0
