# Delete its mail account from the Core's LDAP (only if it's this app's)
LDAP_BASE="$(sed -n 's/^LDAP_SEARCH_BASE=ou=people,//p' /federated/apps/mail/.env)"
LDAP_ADMIN_PW="$(cat /federated/apps/ldap/.ldap.secret)"
MAIL_DN="uid=coreapp-{{NAME}},ou=people,$LDAP_BASE"
if docker exec ldap ldapsearch -LLL -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" \
    -w "$LDAP_ADMIN_PW" -b "$MAIL_DN" -s base description 2>/dev/null \
    | grep -qx "description: coreapps: {{NAME}}"; then
  docker exec ldap ldapdelete -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" \
    -w "$LDAP_ADMIN_PW" "$MAIL_DN"
fi
unset LDAP_BASE LDAP_ADMIN_PW MAIL_DN
