#!/bin/bash

uid="$1"
userfile="/etc/openldap/users.d/$uid.ldif"

if [ -z "$uid" ]; then
    echo "Need a user ID (uid)!"
    exit 1
fi


if [ ! -f "$userfile" ]; then
    ldapsearch -x "uid=${uid}" > "$userfile"
    echo "$userfile" had to be created -- please validate before re-running this script.
    exit 2
fi

read -sp 'cn=root,dc=aninix,dc=net Password: ' rootdnpw
ldapdelete -D 'cn=root,dc=aninix,dc=net' -w "$rootdnpw" -H ldap://127.0.0.1 "uid=$uid,ou=People,dc=aninix,dc=net" && \
    ldapadd -D 'cn=root,dc=aninix,dc=net' -w "$rootdnpw" -H ldap://127.0.0.1 -f "$userfile"

exit $?
