#!/bin/sh
#
# |-----------------------------------------------------------|
# | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
# | All Rights Reserved                                       |
# |-----------------------------------------------------------|
# |          Restricted Rights Legend                         |
# | Use, duplication, or disclosure by the Government is      |
# | subject to restrictions as set forth in                   |
# | subparagraph (c)(1)(ii) of the Rights in Technical        |
# | Data and Computer Software Clause of DFARS 252.227-7013.  |
# |         MIPS Computer Systems, Inc.                       |
# |         950 DeGuigne Avenue                               |
# |         Sunnyvale, California 94088-3650, USA             |
# |-----------------------------------------------------------|
#
# $Header: idreboot,v 1.2.2.1 92/06/24 13:34:45 marker Exp $
#
# v4.51 RISC/OS version of the AT&T idreboot utility.
#
# This is a shell script replacement for the AT&T shell script 'idreboot'.
# The syntax is as follows:
#
#	idreboot 
#
# Reboot script for installable drivers. Forces the user to
# reboot in a consistent manner.
#
#
# This routine gets the name of newly built unix from /etc/.new_unix file.
# After confirmation it performfs the following:
#	1) Moves the new system(unix.rxxx_suffix) from /usr/src/uts/mips 
#	   to '/' directory 
#	2) Links it with /unix
#	3) Removes /etc/.new_unix
#	4) Shutdown the system

#
#	MODIFICATION HISTORY
#	09/25/90	CChan	Ported from SVR3
#	10/07/90	TDhar	Made changes for RISC/os
#	04/10/91	TDhar	Give user a chance to save curren unix.
#				Before copying new unix to / directory,
#				check for space.
#

# Not sure what is system console, root should be able to do it(TD)
# CChan. Added check for /dev/tty0
#if [ `tty` != /dev/console ] && [ `tty` != /dev/syscon ] \
#	&& [ `tty` != /dev/tty0 ]
#then
#	echo "$0:  You must be logged on the console."
#	exit 2
#fi

RC=`id | sed -e 's/uid=//' -e 's/(.*//'`
if [ "$RC" != "0" ]
then
	message -d "You (${LOGNAME}) do not have permission to perform software \
installation.  Please consult your Operations/System Administration Guide for more \
information on assigning permissions to privileged operations."
	exit 1
fi

message -cu "The UNIX System has been reconfigured.  To complete \
the install/remove process a shutdown is now being initiated automatically.\n
If you are installing or removing controller boards, you may power down \
the system after the shutdown has completed."
if  [ "$?" = "0" ]
then
	mips=/usr/src/uts/mips
	cd /
	new_unix=0
	if [ -f /etc/.new_unix ]
	then
		new_unix=`cat /etc/.new_unix`
	else
		echo "$0: Fatal Error: /etc/.new_unix not found"
		exit 1
	fi
	if [ $new_unix = 0 ]
	then
		echo "$0: Fatal Error: /etc/.new_unix is empty"
		rm /etc/.new_unix
		exit 1
	fi
	newunix=${mips}/${new_unix}
	if [ ! -f ${newunix} ]
	then
		echo "$0: Fatal Error: ${newunix} not found"
		rm /etc/.new_unix
		exit 1
	fi
	#
	# Find the number of disk blocks used by current unix
	#
	xx=`du -a /unix`
	set $xx
	cursize=`expr $1 / 2`
	cursize=`expr $cursize + 20`		# to be cautious
	#
	# Find the number of disk blocks used by new unix
	#
	xx=`du -a ${newunix}`
	set $xx
	newsize=`expr $1 / 2`
	newsize=`expr $newsize + 20`		# to be cautious
	#
	# Find no. of disk block available in root partition
	#
	xx=`df|grep "^\/dev\/root "`
	set $xx
	ravail=$5
	ans=0
	first=1
	while [ $ans != 'yes' -o $ans != 'no' ]
	do
		if [ $first = 0 ]
		then
			echo "Please answer yes/no"
		fi
		echo "Do you want to save the current unix? [ yes/no ]"
		read ans
		ans=${ans:=0}
		if [ $ans = 'yes' -o $ans = 'no' ]
		then
			break
		fi
		first=0
	done
	if [ $ans = 'yes' ]
	then
		# Check if we can save it in root partition
		echo "
Checking if we can save it in root partition"
		if [ -f /unix.PRIME ]
		then
			xx=`du -a /unix.PRIME`
			set $xx
			ssize=`expr $1 / 2`
		else
			ssize=0
		fi
		rqd=`expr $cursize + $newsize`
		avail=`expr $ravail + $ssize`
		if [ $avail -gt $rqd ]
		then
			echo "
Saving the current unix as /unix.PRIME"
			cp /unix /unix.PRIME
		else
			echo "
Not enough space in root partition, checking /usr partition"
			cp /unix /usr/unix.PRIME > /dev/null 2>&1
			if [ $? != 0 ]
			then
				echo "
Not enough space in /usr partition, make some space and re-invoke idreboot"
				exit 1
			fi
			echo "
Saving the current unix as  /usr/unix.PRIME"
		fi
	fi
	#
	# Find no. of disk block available in root partition now
	#
	xx=`df|grep "^\/dev\/root "`
	set $xx
	ravail=$5
	# We can use additional 5% space that df shows us, going an extra step
	grace=`expr \( $3 \/ 100 \) \* 5`
	ravail=`expr $ravail + $grace`
	# Check if we have enough space
	if [ $newsize -gt $ravail ]
	then
		echo "
$0 Error: Not enough space in root partition. 
Please make some space and re-invoke idreboot as: /etc/conf/bin/idreboot"
		exit 1
	fi
	echo "
Copying ${newunix} to root directory"
	rm -f /${new_unix} > /dev/null 2>&1
	cp ${newunix} /${new_unix}
	# Can possibly get error if not enough disk space
	if [ $? -ne 0 ]
	then
		echo "Not enough disk space in / for new system"
		exit 1
	else
		rm ${newunix}
	fi
	rm /etc/.new_unix
	echo "
Linking ${new_unix} to /unix"
	ln /$new_unix /unix
	sync;sync
        /etc/shutdown -y -g0 -i0
else
	echo "
			WARNING!! WARNING !!"
	message -d "The UNIX System has now been reconfigured, \
but you have not initiated a system reboot.\
  You should reboot the system via idreboot as soon as possible."
fi
