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

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

. ./prefs

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

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

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

myuser=`$ldapsearch -D $binddn -w $bindpassword -LLL ntuid="$2" rid|grep rid|awk '{print $2}'`
mygroup=`$ldapsearch -D $binddn -w $bindpassword -LLL ntuid="$1" rid|grep rid|awk '{print $2}'`
myuserdec=`$perl -e "print hex(\"$myuser\")"`
mygroupdec=`$perl -e "print hex(\"$mygroup\")"`
useringroups=`$ldapsearch -D $binddn -w $bindpassword -LLL sambamember=*"$2"*|grep rid:|awk '{print $2}'`

if [ "$myuser" = "" ]; then
	echo "User does not exist!"
	exit 1
fi

if [ "$mygroup" = "" ]; then
	echo "Group does not exist"
	exit 1
fi

ingroup=`echo "$useringroups" |grep $mygroup`

if [ "$ingroup" == "" ]; then

	$rpcclient -S . -U $rpcuser%$rpcpassword -c "addgroupmem \"$1\" \"$2\"" -d0
        exit 0
else
	echo "User already in Group"!
	exit 1
fi

exit 0

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