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

# --------------------------------------------------------------------
# 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 [ "$mygroup" = "" ]; then
	echo "Group does not exist!"
	exit 1
fi

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

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

if [ "$ingroup" == "" ]; then
	echo "User not in Group!"
	exit 1
else
	echo $myuserdec
	echo $mygroupdec	

	$rpcclient -S . -U $rpcuser%$rpcpassword -c "delgroupmem \"$mygroupdec\" \"$myuserdec\"" 
fi

exit 0

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