#!/bin/bash

## Copyright (C) 2024 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## This script only exists because adduser can't prompt the user for a
## username.

set -o errexit
set -o nounset
set -o errtrace
set -o pipefail

## Code duplication. Copied from helper-scripts/usr/sbin/pwchange.
if [ "$(id -u)" != "0" ]; then
  printf "%s\n" "$0: ERROR: This must be run as root (sudo)!" >&2
  exit 1
fi

if ! user_list_str="$(/usr/libexec/helper-scripts/get-user-list)"; then
  printf "%s\n" "$0: ERROR: Failed to get user list!" >&2
  exit 1
fi
readarray -t user_list <<< "$user_list_str"

printf "%s\n" "Users present on the system:" >&2
for user_entry in "${user_list[@]}"; do
  printf "%s\n" "   ${user_entry}" >&2
done

read -rp 'Enter a username for the new user: ' user_name
adduser "${user_name}"
