#! /usr/bin/env bash

set -eo pipefail

usage() {
cat <<USAGE_END
usage:
	$0 <user_id> <common_name>

	Set a user's common name attribute in LDAP.  This is displayed as their "full name" in Nextcloud.

parameters:
	<user_id>: Specify the user ID that this users uses to log in.
	<common_name>: Provide the new common name to assign to this user (their full name).
USAGE_END
}

if [ "$#" -ne 2 ]; then
	usage
	exit 1
fi

USERID="$1"
CN="$2"

echo "Changing common name of $USERID to: $CN"

sudo ldapmodify -H ldapi:/// -Y EXTERNAL <<LDIF_END
dn: uid=$USERID,ou=users,dc=thisbox
changeType: modify
replace: cn
cn: $CN
-
LDIF_END

echo "Change completed, synchronizing Nextcloud"

# Force Nextcloud to refresh the user now
sudo podman exec --user www-data nextcloud-freedombox /var/www/html/occ ldap:check-user --update "uid=$USERID,ou=users,dc=thisbox" --update
