#!/bin/bash
# Samba-TNG                                      add_user_to_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
	
    touch add_member_to_unixgroup_tmp
    # ------------- add_member_to_unixgroup_tmp --------------- #
    echo "dn: cn="$1", "$groupsuffix > add_member_to_unixgroup_tmp
    echo "changetype: modify" >> add_member_to_unixgroup_tmp
    echo "add: memberUid" >> add_member_to_unixgroup_tmp
    echo "memberUid: "$2 >> add_member_to_unixgroup_tmp
    # ------------- add_member_to_unixgroup_tmp --------------- #

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

    rm add_member_to_unixgroup_tmp

    exit 0

else

    echo "User already in Group"!
    exit 1

fi

exit 0

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