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

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

. ./prefs

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

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

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

myuser=`getent passwd|grep -w "$2"`
mygroup=`getent group|grep -w "$1"`

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

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

ingroup=`$ldapsearch -D $binddn -w $bindpassword cn="$1" -b $groupsuffix memberUid|grep memberUid|grep $2`
if [ "$ingroup" = "" ]; then
	echo "User not in Group!"
	exit 1
else
	touch remove_member_from_unixgroup_tmp
	# ----------- remove_member_from_unixgroup_tmp ------------ #
	echo "dn: cn="$1", "$groupsuffix > remove_member_from_unixgroup_tmp
	echo "changetype: modify" >> remove_member_from_unixgroup_tmp
	echo "delete: memberUid" >> remove_member_from_unixgroup_tmp
	echo "memberUid: "$2 >> remove_member_from_unixgroup_tmp
	# ----------- remove_member_from_unixgroup_tmp ------------ #

	$ldapmodify -D $binddn -w $bindpassword -f remove_member_from_unixgroup_tmp

	rm remove_member_from_unixgroup_tmp

	exit 0
fi

exit 0

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