#!/bin/bash

LOGIN=$1
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ini="$current_dir/scripts.ini"
ldap_password=$(awk '/ldap_password/ {print $2}' $ini)
dc=$(awk '/dc/ {print $2}' $ini)
cn="$(awk '/cn/ {print $2}' $ini),$dc"
users_ou="$(awk '/users_ou/ {print $2}' $ini),$dc"

ldiftmp="/tmp/ldapuserremove.ldif"
ldiftemplate="$current_dir/user_delete.ldif"


if [ ! -f $ldiftemplate ]
then
	echo "Template file not found in $ldiftemplate"
	exit 0
fi

if [ -z "$LOGIN" ]
then
	echo "empty username. usage: remove_user 'username'"
	exit 0
fi

TEXT=$(cat $ldiftemplate)
TEXT=${TEXT//\{LOGIN\}/$LOGIN}
TEXT=${TEXT//\{PEOPLE_OU\}/$users_ou}

echo "$TEXT" > $ldiftmp

ldapmodify -D $cn -w $ldap_password -f $ldiftmp > /dev/null 2>&1

if [ $? -gt 0 ]
then
	echo 'Error'
else
	echo 'Success!'
fi
rm $ldiftmp

