# "Aaddgrpes", a shell script to add group entries to the master
#              password and group files.

# Copyright 1986 Intel Corporation.

# 03/25/86  gll  Initial entry for iDIS R2.0.  My intent is for this
#                to become a 'C' program.

# First, gather the passed parameters.

Groupname=$1	shift
Grouptype=$1	shift
gid=$1		shift
mgr=$1		shift
Startgid=$1	shift
Lastgid=$1	shift

comment=$1

# There is only a home path if this is an iDISGROUP.

if [ $Grouptype = "iDISGROUP" ]
then shift
     Home=$1
fi

# See if master node can be accessed.

cat $MASNODEP/etc/systemid > /dev/null

if [ $? -ne 0 ]
then echo $gid; exit 3
fi

# See if desired group name already exists.

puniq=`grep -c $Groupname $MASNODEP/etc/passwd`
guniq=`grep -c $Groupname $MASNODEP/etc/group`

if [ "$puniq" != "0" -o "$guniq" != "0" ]
then echo $gid; exit 1
fi

# Generate new gid number if necessary.

if [ "$gid" = "automatic" ]
then gid=`$iDISBIN/gid $Startgid`
     if [ $? -ne 0 ]
     then echo $gid; exit 6
     fi
fi

# Make sure the desired gid isn't already in use.

guniq=`$iDISBIN/Uselect -s : 0 2 $gid < $MASNODEP/etc/group`

if [ "$guniq" != "" ]
then echo $gid; exit 2
fi

# Make sure group name is correct format.

nmsiz=`expr $Groupname : '[a-zA-Z]*'`

if [ $nmsiz -lt 3 ]
then echo $gid; exit 4
fi

# Make sure gid is all numeric.

gidsz=`expr $gid : '.*'`
ndigits=`expr $gid : '[0-9]*'`

if [ $gidsz -ne $ndigits ]
then echo $gid; exit 5
fi

# Make sure gid is in legal range.

if [ $gid -lt $Startgid -o $gid -gt $Lastgid ]
then echo $gid; exit 5
fi

# Make entries to the master group and passwd files.

echo "$Groupname:$Grouptype::$gid:$comment:$Home:" >> $MASNODEP/etc/passwd
echo "$Groupname::$gid:$mgr," >> $MASNODEP/etc/group

# Everything is ok, so echo gid

echo "$gid"

