#!/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: idcheckmajor,v 1.3.2.1 92/06/24 13:34:42 marker Exp $
#
# Name      : idcheckmajor
# Purpose   : Before installing a driver check if the the major number
#             specified in Master file is already in use or not.
# Author    : Tripty Dhar 

root=/
masterd=/usr/src/uts/mips/master.d
boot=/usr/src/uts/mips/bootarea_i
mips=/usr/src/uts/mips
idbin=/etc/conf/bin

trap '' 1 2 3 9 15
# Make sure there is at least one argument

# Set default values
sflg=0
#suffix=sys
mflg=0
model=`uname -t`
case $model in
	m2000-6|m2000-8)
		suffix=ijc_local
		;;
	*)
		suffix=local
		;;
esac
USAGE="USAGE: $0 [ -s suffix ] -m major_number"

if [ $# -lt 2 ]
then
	echo $USAGE
	exit 1
fi
# Parse the command line
while getopts s:m: option
do
	case "$option"
	in
		s) sflg=1
		   suffix=$OPTARG;;
		m) mflg=1
		   MAJOR=$OPTARG;;
		\?) echo ${USAGE}
		   exit 1;;
	esac
done

# Make sure there is at least one argument accept -s
if [ $sflg = 1 -a $# -lt 4 ]
then
	echo ${USAGE}
	exit 1
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 ]
then
	echo "$0: $newsysgen not found"
	exit 1
fi
vi ${masterd}/Makefile << ! > /dev/null 2>&1
/MASTERS
ma
/include
mb
:'a,'bw!/tmp/trp
:q!
!
list=`cat /tmp/trp`
rm /tmp/trp
for i in ${list}
do
	if [ -f ${masterd}/$i ]
	then
		line=`grep -v "^\*" ${masterd}/$i|sed -e "/^[ 	]*$/d"|sed -n 1p` 
		line=${line:=0}
		if [ "$line" != 0 ]
		then
			set $line
			if [ ! -z $3 -a $3 != '-' ]
			then
				majors=`echo $3|sed "s,\,,\ ,g"`
				for j in $majors
				do
					if [ $MAJOR = $j ]
					then
						echo "$0: The major number $MAJOR is used in $i"
						exit 1
					fi
				done
			fi
		fi
	fi
done
exit 0
