#!/bin/sh

# This file is part of the Savane project
# <http://gna.org/projects/savane/>
#
# $Id: groupadd 6431 2006-11-22 10:14:55Z yeupou $
#
#  Copyright 2005      (c) Michael Casadevall <sonicmctails--ssonicnet.com>
#
# The Savane project is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# The Savane project is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with the Savane project; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# 
#  This Script emulates the groupadd command that is 
#  standard in many UNIX like Operating Systems
#  this script should be placed in /usr/sbin
#  it should be owned by root.admin and chmod 744  
#  
#  
###########

# script version
version="1.0"

#find the shell utils wee need
niutil=`which niutil`
if [ ! -x "$niutil" ];then
 >&2 echo groupadd: unable to find/use niutil	
exit 7
fi
nidump=`which nidump`
if [ ! -x "$nidump" ];then
 >&2 echo groupadd: unable to find/use nidump	
exit 7
fi
cut=`which cut`
if [ ! -x "$cut" ];then
 >&2 echo groupadd: unable to find/use cut	
exit 7
fi
grep=`which grep`
if [ ! -x "$grep" ];then
 >&2 echo groupadd: unable to find/use grep	
exit 7
fi
expr=`which expr`
if [ ! -x "$expr" ];then
 >&2 echo groupadd: unable to find/use expr	
exit 7
fi


#gets a free gid greater than 100
get_free_gid() 
{
continue="no"
number_used="dontknow"
fnumber=101
until [ $continue = "yes" ]; do
	if [ `$nidump group . |$cut -d":" -f3 |$grep -c "^$fnumber$"` -gt 0 ]; then
		number_used=true
	else
		number_used=false
	fi
	if [ $number_used = "true" ]; then
		fnumber=`$expr $fnumber + 1`
	else
		GroupID="$fnumber"
		continue="yes"
	fi
done;
}

#are we root
check_uid() {
    if [ "`whoami`" = root ]
    then
	uID=0
    else
	if [ "$uID" = "" ]
	then
	    uID=-1
	fi
    fi
    export uID
}


usage()
{
        >&2 echo "groupadd [-g GID] group"
        >&2 echo "READ groupadd (8) manpage for more data."
exit $1
}

override=0
getgid=0
GroupID=""
group=""
#case the options and prams
while [ $# -ne 0 ]
do
    case "$1" in
        --help)
            usage 0
            ;;
        --version)
           >&2 echo "groupadd: version $version, by Chris Roberts "
           >&2 echo "groupadd: (c) 2002 Chris Roberts <chris@osxgnu.org> "
            exit 0
            ;;
        -g)
	    getgid=1
	    shift
            GroupID="$1"
            ;;
         -o)
           override=1
           ;;
         -*)
            >&2 echo "groupadd: ERROR Unknown option: $1"
            usage 1
            ;;
        *)
 	    group="$1"
            ;;
    esac
    shift
done 


if [ $getgid -ne 0 ]
then
	if [ -z "$GroupID" ]
	then
              >&2 echo "groupadd: ERROR MISSING VALUE: -g requires a GID"
              usage 1
        fi
fi

if [ -z "$group" ]
then
    >&2 echo "groupadd: ERROR MISSING VALUE: requires a group"
    usage 1
fi

check_uid
if [ $uID != 0 ]
then
	>&2 echo groupadd: you must be root
	exit 7
fi


#routine that actually adds the group

#if no GID passed get one
if [ -z $GroupID ]; then 
  #if GID was passed test it
 
     if [ `$nidump group . |$grep -c "^$group:"` -eq 0 ]; then
	get_free_gid
     else
        >&2 echo  "groupadd: ERROR Group '$group' already exists"
        exit 1
     fi
else 
if [ $override -ne 1 ];then
    if [ `$nidump group . |$cut -d":" -f3 |$grep -c "^$GroupID"` -gt 0 ]; then
        >&2 echo "groupadd: ERROR GID '$GroupID' is in use" 
        exit 7
    fi
fi
     if [ `$nidump group . |$grep -c "^$group:"` -ne 0 ]; then

        >&2 echo  "groupadd: ERROR Group '$group' already exists"
        exit 1
     fi

fi

#make the group
$niutil -create . /groups/$group
$niutil -createprop . /groups/$group name $group
$niutil -createprop . /groups/$group gid $GroupID
$niutil -createprop . /groups/$group passwd \*


