-sys-user_next_uid() {
- local euid;
- for (( euid = 101; euid <= 999; euid++ )); do
- [[ -z $(egetent passwd "${euid}") ]] && break
- done
- if (( "${euid}" == 999 )); then
- die "out of available UIDs!"
- else
- echo "${euid}"
- fi
+sys-user_getuid() {
+ # Output the real UID of the given user, or the empty string if the
+ # user does not exist on the system.
+ [[ $# -eq 1 ]] || die "usage: sys-user_getuid <username>"
+ echo $(id --real --user "${1}")
+}
+
+sys-user_getname() {
+ # Output the username associated with the given UID, or the empty string
+ # if the given UID is still available.
+ [[ $# -eq 1 ]] || die "usage: sys-user_getname <uid>"
+ echo $(egetent passwd "${1}" | cut -f1 -d':')
+}
+
+sys-user_create() {
+ # Create the user whose information is contained in the following
+ # variables:
+ #
+ # * SYS_USER_NAME
+ # * SYS_USER_UID
+ # * SYS_USER_SHELL
+ # * SYS_USER_HOME
+ # * SYS_USER_GROUPS
+ #
+ # We don't create a group with the same name; that should be the
+ # job of the matching sys-group package.
+ useradd --no-user-group \
+ ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \
+ ${SYS_USER_GROUPS:+--groups }"${SYS_USER_GROUPS}" \
+ --shell "${SYS_USER_SHELL}" \
+ --home-dir "${SYS_USER_HOME}" \
+ "${SYS_USER_NAME}"
+}
+
+
+sys-user_modify() {
+ # Modify the existing user named $SYS_USER_NAME to match the values
+ # contained in the following variables:
+ #
+ # * SYS_USER_UID
+ # * SYS_USER_SHELL
+ # * SYS_USER_HOME
+ # * SYS_USER_GROUPS
+ #
+ usermod ${SYS_USER_UID:+--uid }"${SYS_USER_UID}" \
+ ${SYS_USER_GROUPS:+--append --groups }"${SYS_USER_GROUPS}" \
+ --shell "${SYS_USER_SHELL}" \
+ --home-dir "${SYS_USER_HOME}" \
+ "${SYS_USER_NAME}"