59 lines
1.8 KiB
Bash
Executable File
59 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
|
|
env_file="${1:-compose/.env}"
|
|
if [[ ! -r "$env_file" ]]; then
|
|
printf 'Environment file not found: %s\n' "$env_file" >&2
|
|
exit 1
|
|
fi
|
|
if [[ ! -t 0 ]]; then
|
|
printf 'Run this script from an interactive terminal.\n' >&2
|
|
exit 1
|
|
fi
|
|
|
|
current_username="$(awk -F= '$1 == "ADMIN_INITIAL_USERNAME" {sub(/^[^=]*=/, ""); print; exit}' "$env_file")"
|
|
read -r -p "Initial admin username [${current_username:-localadmin}]: " username
|
|
username="${username:-${current_username:-localadmin}}"
|
|
if [[ ! "$username" =~ ^[A-Za-z0-9_.-]{3,80}$ ]]; then
|
|
printf 'Username must be 3-80 characters using letters, numbers, dot, underscore, or hyphen.\n' >&2
|
|
exit 1
|
|
fi
|
|
|
|
read -r -s -p 'Initial admin password (at least 16 letters/numbers/_/-): ' password
|
|
printf '\n'
|
|
read -r -s -p 'Confirm initial admin password: ' password_confirmation
|
|
printf '\n'
|
|
if [[ "$password" != "$password_confirmation" ]]; then
|
|
printf 'Passwords do not match.\n' >&2
|
|
exit 1
|
|
fi
|
|
if [[ ${#password} -lt 16 || ! "$password" =~ ^[A-Za-z0-9_-]+$ ]]; then
|
|
printf 'Password must be at least 16 characters using only letters, numbers, underscore, or hyphen.\n' >&2
|
|
exit 1
|
|
fi
|
|
|
|
set_value() {
|
|
local key="$1"
|
|
local value="$2"
|
|
local temporary_file
|
|
temporary_file="$(mktemp "${env_file}.tmp.XXXXXX")"
|
|
awk -v key="$key" -v value="$value" '
|
|
BEGIN { prefix = key "="; found = 0 }
|
|
index($0, prefix) == 1 {
|
|
if (!found) print prefix value
|
|
found = 1
|
|
next
|
|
}
|
|
{ print }
|
|
END { if (!found) print prefix value }
|
|
' "$env_file" > "$temporary_file"
|
|
chmod 600 "$temporary_file"
|
|
mv -f "$temporary_file" "$env_file"
|
|
}
|
|
|
|
set_value ADMIN_INITIAL_USERNAME "$username"
|
|
set_value ADMIN_INITIAL_PASSWORD "$password"
|
|
unset password password_confirmation
|
|
chmod 600 "$env_file"
|
|
printf 'Initial admin credentials stored without displaying the password.\n'
|