#!/bin/bash
# Samba-TNG                                             add_unix_group
# --------------------------------------------------------------------

# --------------------------------------------------------------------
# config here

. ./prefs

# --------------------------------------------------------------------
# print usage

if [ "$1" == "" ]; then
        echo "Usage: $(basename $0) <group>"
        exit 1
fi

# --------------------------------------------------------------------
# do some error checking

groupexist=`getent group|grep "$1"`

nextridhex=`$ldapsearch -D $binddn -w $bindpassword id=root nextrid -LLL|grep nextrid|awk '{print $2}'`
nextriddec=`$perl -e "print hex(\"$nextridhex\")"`

if test "$nextridhex" = ""; then
    echo "error: cannot get next rid! (nextridhex)"
    echo "is the ldap server alive ?"
    exit 1
fi

if test "$nextriddec" = ""; then
    echo "error: cannot get next rid! (nextriddec)"
    echo "is the ldap server alive ?"
    exit 1
fi

if test "$nextriddec" = "0"; then
    echo "error: nextriddec renturned 0 !"
    echo "possibly a typo in nextriddec= ?"
    exit 1
fi
if [ "$groupexist" = "" ]; then

# --------------------------------------------------------------------
# begin

touch addunixgroup_temp.ldif

# ---------------------- addunixuser_temp.ldif -----------------------
echo "dn: cn="$1","$groupsuffix >>addunixgroup_temp.ldif
echo "cn: "$1 >>addunixgroup_temp.ldif
echo "objectclass: posixGroup">>addunixgroup_temp.ldif
echo "objectclass: top" >>addunixgroup_temp.ldif
echo "gidNumber: "$nextriddec >>addunixgroup_temp.ldif
# ---------------------- addunixuser_temp.ldif -----------------------

cat addunixgroup_temp.ldif
$ldapadd -D $binddn -w $bindpassword -f addunixgroup_temp.ldif

rm addunixgroup_temp.ldif

else
	echo "Group already exists!"
	exit 1
fi
exit 0

# --------------------------------------------------------------------
