#!/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: idcheck,v 1.3.2.1 92/06/24 13:34:41 marker Exp $
# v4.51 RISC/OS version of the AT&T idcheck utility.
#
# This is a shell script replacement for the AT&T binary
# executable. The syntax has been modified as follows:
#
#	idcheck [-s <suffix>] -v <vector>/-b <base_address>/-p <driver> 
#
# where
#	-s <suffix> checks using the specified config files. The
#				default is 'sys'. If the config files do not
#				exit, then an error is returned.
#       -v <vector> checks if the given vector is in use. If so it returns
#		    		with exit code 100
# 	-b <base_address> checks if the give base_address is in use. If so
#		                returns with exit code 100
#	-p <driver> checks for the installation status of the
# 				specified driver.

# idcheck returns 
#	0 if driver is not installed.
#	+1 if Driver.o is found in bootarea_i
#	+2 if driver has been added to Makefile
#	+4 if driver has been added to sysgen file
#	+16 if Master file has been added to master.d
#	+32 if io vector has been added to sysgen file
#	100 on an error
#

#	MODIFICATION HISTORY
#	CChan		11/07/90	Initial Coding
# 	TDhar		12/06/90	Added -v and -b options. Catch all
#					interrupts while working
#	TDhar		12/07/90	Display USAGE and return if only
#					-s options is given
#	TDhar		04/10/91	Default suffix is changed to local.
#					Correct bootarea_ii for 6000 series.

# Setup some global variables
root=/
masterd=/usr/src/uts/mips/master.d
#boot=/usr/src/uts/mips/bootarea_i
mips=/usr/src/uts/mips
idbin=/etc/conf/bin
USAGE="USAGE: idcheck [-s <suffix>] -p <dirver>|-v <vector>|-b <base_address>"

trap '' 1 2 3 9 15
# Make sure there is at least one argument
if [ $# -lt 2 ]
then
	echo ${USAGE}
	exit 100
fi

# Set default values
pflg=0
sflg=0
vflg=0
bflg=0
driver=""
#suffix=sys
code=0
model=`uname -t`
case $model in
	m2000-6|m2000-8)
		boot=/usr/src/uts/mips/bootarea_i
		suffix=ijc_local
		;;
	RC62*)
		boot=/usr/src/uts/mips/bootarea_ii
		suffix=local
		;;
	*)
		boot=/usr/src/uts/mips/bootarea_i
		suffix=local
		;;
esac

# Parse the command line
while getopts p:s:v:b: option
do
	case "$option" in
		p) pflg=1
		   driver=$OPTARG;;
		s) sflg=1
		   suffix=$OPTARG;;
		v) vflg=1
		   vctor=$OPTARG;;
		b) bflg=1
		   bse=$OPTARG;;
		*) echo ${USAGE}
		   exit 100;;
	esac
done

# Make sure there is at least one argument accept -s
if [ $sflg = 1 -a $# -lt 4 ]
then
	echo "At least one of the options ( -p | -v | -b ) must be specified"
	echo ${USAGE}
	exit 100
fi
# 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/'`

# The target configuration files must exist
if [ ! -f $masterd/$newsysgen -o ! -f $masterd/$newkernel ]
then
	echo "$0: $newsysgen not found"
	exit 100
fi
#Check for -v and -b options
type=`/etc/conf/bin/idfindsys -b`	
if [ $type = 'AT' ]
then
	type=ATBUS
else
	type=VECTOR
fi
#First check if -v is specified
if [ $vflg = 1 ]
then
	if [ $type = 'AT' ]
	then
		echo "$0: No VECTOR directive in $newsysgen file"
		exit 100
	fi
	z=`grep "^$type:" ${masterd}/${newsysgen}|grep "[ 	]vector=$vctor[ 	]"`
	if [ $? = 0 ]
	then
		echo "$0: The vector $vctor is in use in $newsysgen file"
		exit 100
	fi
fi
#Then check if -b is specified
if [ $bflg = 1 ]
then
	z=`grep "^$type:" ${masterd}/${newsysgen}|grep "base=$bse$"`
	code1=$?
	code2=1
	if [ $code1 != 0 ]
	then
		z=`grep "^$type:" ${masterd}/${newsysgen}|grep "base=$bse[ 	]"`
		code2=$?
	fi
	if [ $code1 = 0 -o $code2 = 0 ]
	then
		echo "$0: The base address $bse is in use in $newsysgen file"
		exit 100
	fi
fi
if [ $pflg = 0 ]
then
	exit 0
fi
# Check if driver.o in bootarea_i
if [ -f $boot/$driver.o ]
then
	code=`expr $code + 1`
fi

# See if driver installed in Makefile 
grep "[ 	]$driver[ 	]" $masterd/Makefile 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
	code=`expr $code + 2`
fi

# See if driver installed in sysgen file
grep "INCLUDE:[ 	]$driver[ 	]*$" $masterd/$newsysgen 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
	code=`expr $code + 4`
fi

# See if driver installed in master.d 
if [ -f $masterd/$driver ]
then
	code=`expr $code + 16`
fi

# See if io vector installed in sysgen file
grep "++$driver++" $masterd/$newsysgen 1>/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
	code=`expr $code + 32`
fi
if [ $code != 0 ]
then
	exit $code
fi
exit 0
