install.fairie/home/dot_local/bin/executable_add-user

140 lines
4.8 KiB
Text
Raw Normal View History

2023-06-22 21:39:22 -07:00
#!/usr/bin/env bash
# @file ~/.local/bin/add-user
# @brief Add a user and a group with the same name on either Linux or macOS
# @description
# This script is utilized by other scripts to ensure that there is both a user and group
# named by the single argument that this executable accepts. It checks whether or not
# there is already a user / group with the name present on the system before running
# any code. On macOS, it assigns the user an ID that equal to the maximum user ID present
# on the system plus one.
#
# This script was generated with ChatGPT.
# Check if the script is being run as root
if [[ $EUID -ne 0 ]]; then
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg error "This script must be run as root"
else
echo -e "\e[91mThis script must be run as root\e[0m"
fi
exit 1
2023-06-22 21:39:22 -07:00
fi
# Check if the correct number of arguments is provided
if [[ $# -ne 1 ]]; then
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "Usage: $0 <group>"
else
echo -e "\e[93mUsage: $0 <group>\e[0m"
fi
2023-06-22 21:39:22 -07:00
exit 1
fi
GROUP=$1
# Check if the operating system is macOS
if [[ "$(uname)" == "Darwin" ]]; then
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "Creating group and user ${GROUP} on macOS..."
else
echo -e "\e[96mCreating group and user ${GROUP} on macOS...\e[0m"
fi
2023-06-22 21:39:22 -07:00
# Check if the group already exists
if ! sudo dscl . read /Groups/"${GROUP}" >/dev/null 2>&1; then
# Create the group
sudo dscl . create /Groups/"${GROUP}"
# Ensure the group has a GID
if [[ "$(sudo dscl . read /Groups/"$group" gid 2>&1)" == *"No such key"* ]]; then
2023-11-26 22:11:08 -08:00
MAX_ID_GROUP="$(dscl . -list /Groups gid | awk '{print $2}' | sort -ug | tail -1)"
GROUP_ID="$((MAX_ID_GROUP+1))"
2023-06-22 21:39:22 -07:00
sudo dscl . create /Groups/"${GROUP}" gid "$GROUP_ID"
fi
else
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "Group ${GROUP} already exists."
else
echo -e "\e[93mGroup ${GROUP} already exists\e[0m"
fi
2023-06-22 21:39:22 -07:00
fi
# Check if the user already exists
if ! sudo dscl . read /Users/"${GROUP}" >/dev/null 2>&1; then
# Create the user
sudo dscl . create /Users/"${GROUP}"
# Ensure the user has a PrimaryGroupID
if [[ "$(sudo dscl . read /Users/"${GROUP}" PrimaryGroupID 2>&1)" == *"No such key"* ]]; then
sudo dscl . create /Users/"${GROUP}" PrimaryGroupID 20
fi
# Ensure the user has a UniqueID
if [[ "$(sudo dscl . read /Users/"${GROUP}" UniqueID 2>&1)" == *"No such key"* ]]; then
2023-11-26 22:11:08 -08:00
MAX_ID_USER="$(dscl . -list /Users UniqueID | sort -nr -k 2 | head -1 | grep -oE "[0-9]+$")"
2023-06-22 21:39:22 -07:00
USER_ID="$((MAX_ID_USER+1))"
sudo dscl . create /Users/"${GROUP}" UniqueID "$USER_ID"
fi
# Add the user to the group
sudo dseditgroup -o edit -t user -a "${GROUP}" "${GROUP}"
# Add the current user to the group
sudo dseditgroup -o edit -t user -a "${USER}" "${GROUP}"
else
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "User ${GROUP} already exists"
else
echo -e "\e[93mUser ${GROUP} already exists\e[0m"
fi
2023-06-22 21:39:22 -07:00
fi
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "Group and user ${GROUP} created successfully on macOS"
else
echo -e "\e[92mGroup and user ${GROUP} created successfully on macOS\e[0m"
fi
2023-06-22 21:39:22 -07:00
exit 0
elif [[ "$(uname)" == "Linux" ]]; then
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "Creating group and user ${GROUP} on Linux..."
else
echo -e "\e[96mCreating group and user ${GROUP} on Linux...\e[0m"
fi
2023-06-22 21:39:22 -07:00
# Check if the group already exists
if ! grep -qE "^${GROUP}:" /etc/group; then
# Create the group
groupadd "${GROUP}"
else
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "Group ${GROUP} already exists"
else
echo -e "\e[93mGroup ${GROUP} already exists\e[0m"
fi
2023-06-22 21:39:22 -07:00
fi
# Check if the user already exists
if ! id -u "${GROUP}" >/dev/null 2>&1; then
# Create the user and assign it to the group
useradd -g "${GROUP}" "${GROUP}"
else
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg info "User ${GROUP} already exists"
else
echo -e "\e[93mUser ${GROUP} already exists\e[0m"
fi
2023-06-22 21:39:22 -07:00
fi
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg success "Group and user ${GROUP} created successfully on Linux"
else
echo -e "\e[92mGroup and user ${GROUP} created successfully on Linux\e[0m"
fi
2023-06-22 21:39:22 -07:00
exit 0
fi
# If the operating system is neither macOS nor Linux, display an error message
2023-07-14 22:28:48 -07:00
if command -v logg > /dev/null; then
logg error "Unsupported operating system"
else
echo -e "\e[91mUnsupported operating system\e[0m"
fi
2023-06-22 21:39:22 -07:00
exit 1