#!/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: idmakesys,v 1.3.2.1 92/06/24 13:34:44 marker Exp $
# This is a shell script to be used to create the sysgen and
# kernel configuration files in /usr/src/uts/mips/master.d from
# the configuration files of the currently running system. If
# for some reason, the configuration files for the currently
# running system cannot be found, then the baseline configuration
# files for this system model will be used.
#
# SYNTAX:
#	idmakesys [-s <suffix>]
#
# where
#	-s <suffix> specifies the suffix to be used for the newly
#				created config files. The default is 'sys'
#
# The purpose of this RISC/OS specific id command is to provide a
# mechanism for V.3 Install scripts to ensure that default
# kernel.rxxxx_sys and sysgen.rxxxx_sys files exist when 'idcheck'
# is run. This is not strictly needed for 'idbuild' or 'idinstall'
# as they call 'idmakesys' themselves. This way, V.3 Install
# scripts need only to add the 'idmakesys' command at the beginning
# before any of the other id commands are called.


#	MODIFICATION HISTORY
#	11/09/90	CChan	Initial Coding
#	12/07/90	TDhar	Trap interrupts
#	12/13/90	TDhar	Make adjustment for if there is no other
#				unix in '/' directory.
#	03/25/91	TDhar	If the new configuration file exist but
#				the running unix is not the new unix then
#				check the current configuration file (give
#				importance to local configuration files) if
#				the current configuration files are the latest
#				then make new config files from current config
#				files.
#				
#	04/09/91	TDhar	Change the default suffix to local

# Setup some global variables
masterd=/usr/src/uts/mips/master.d
root=/
mips=/usr/src/uts/mips
idbin=/etc/conf/bin

# Setup default values
sflg=0
#suffix='sys'
model=`uname -t`
case $model in
	m2000-6|m2000-8)
		suffix=ijc_local
		;;
	*)
		suffix=local
		;;
esac

# Parse the command line
while getopts s: option
do
	case "$option"
	in
		s) sflg=1
		   suffix=$OPTARG;;
		\?) echo 'USAGE: idmakesys  [-s <suffix>]'
		   exit 1;;
	esac
done

if [ $suffix = 'std' -o $suffix = 'ijc' ]
then
	echo "$0: You are not allowed to change the standard configuration files"
	exit 1
fi

trap '' 1 2 3 9 15
# Build the corresponding kernel and sysgen filenames
stdunix=`${idbin}/idfindsys -u`
newunix=`echo $stdunix | sed "s/_.*\$/_$suffix/"`
newsysgen=`echo $newunix | sed 's/unix/sysgen/'`
newkernel=`echo $newunix | sed 's/unix/kernel/'`

# Find the currently running system. Check / first.
currentunix=0
cd $root 
lst=0
lst=`ls unix.*` > /dev/null
for xx in $lst
do
	if cmp -s $xx unix > /dev/null
	then
		currentunix=$xx
	fi
done

# Not found in /, so check /usr/src/uts/mips
if [ $currentunix = 0 ]
then
	cd $mips
	lst=0
 	lst=`ls unix.*` > /dev/null
 	for xx in $lst
	do
		if cmp -s $xx /unix > /dev/null  #cmpare with /unix
		then
			currentunix=$xx
		fi
	done
fi

# If not found in either directory, then use the *std config files.
if [ $currentunix = 0 ]
then
	currentunix=$stdunix
fi

# Make the config filenames for the currently running system
currentsysgen=`echo $currentunix | sed 's/unix/sysgen/'`
currentkernel=`echo $currentunix | sed 's/unix/kernel/'`

#
# If new configuration exist 
#
if [ -f $masterd/$newsysgen -a -f $masterd/$newkernel ]
then
	# If currentunix is newunix then we are in good shape 
	if [ $currentunix = $newunix ]
	then
		exit 0
	fi
	#
	# The current configuration files do not exist, probably some
	# wizard changed the name of unix. Best we can do is go with
	# existing new configuration files. Good luck !!!
	#
	if [ ! -f $masterd/$currentsysgen -o ! -f $masterd/$currentkernel ]
	then
		exit 0
	fi

	#
	# We come here only if new configuration exist but the running
	# unix is not newunix. Either we could not figure out the current
	# unix or it is running standard unix. Since we have the desired
	# configuration files. So ....
	#
	if [  $currentunix = $stdunix ]
	then
		exit 0
	fi
	#
	# Consider the case that, we found the current unix, and current 
	# configuration file exist, but new configuration fles exist too.
	# 
	if [ -f $masterd/$currentsysgen -a -f $masterd/$currentkernel ]
	then
		# 
		# Only if current configuration files are newer than new 
		# configuration files then overwrite new configuration file
		# by current configuration files, otherwise do nothing.
		#
		xx=`/usr/pkg/bin/dates -f $masterd/$newsysgen`
		set $xx
		newdate=$2
		xx=`/usr/pkg/bin/dates -f $masterd/$currentsysgen`
		set $xx
		curdate=$2
		if [ $curdate -gt $newdate ]
		then
			mv $masterd/$newsysgen $masterd/$newsysgen.bu
			mv $masterd/$newkernel $masterd/$newkernel.bu
			cp $masterd/$currentsysgen $masterd/$newsysgen
			cp $masterd/$currentkernel $masterd/$newkernel
		fi
	fi
else
	#
	# If either kernel.rxxxx_<suffix> or sysgen.rxxxx_<suffix> do not exist,
	# then create them from the config files of the currently running 
	# system. If -s not specified, then *_sys config files will be
	# created.
	#
	# Make sure the current config files are still around
	#
	if [ ! -f $masterd/$currentsysgen -o ! -f $masterd/$currentkernel ]
	then
		#
		# Config files for the currently running system have
		# been deleted. If this is the *std config files, then
		# return an error.
		#
		if [ $currentunix = $stdunix ]
		then
			# Unfortunately, the config files we want are the *std
			# files. This system has a problem.
			#
			echo "$0: Configuration files $currentsysgen and $currentkernel"
			echo "for the currently running system have been deleted."
			echo "Thus, $newsysgen and $currentsygsen cannot be created"
			echo "idmakesys aborted. "
			exit 1
		else
			# Although the config files for the currently running
			# system have been deleted, we can still use the *std
			# files, but print out a warning.
			#
			echo "Configuration files $currentsysgen and $currentkernel"
			echo "for the currently running system have been deleted."
			echo "Thus, $newsysgen and $newkernel will be created"
			echo "using $stdunix"
			currentunix=$stdunix
			currentsysgen=`echo $currentunix | sed 's/unix/sysgen/'`
			currentkernel=`echo $currentunix | sed 's/unix/kernel/'`
			cp $masterd/$currentsysgen $masterd/$newsysgen
			cp $masterd/$currentkernel $masterd/$newkernel
		fi
	else
		# The config files for the currently running system still
		# exist, so copy them into the new config files (if needed).
		if [ $currentsysgen != $newsysgen ]
		then
			cp $masterd/$currentsysgen $masterd/$newsysgen
			cp $masterd/$currentkernel $masterd/$newkernel
		fi
	fi

fi
