#!/usr/bin/env bash
# Om Shri Ganeshaya Namah | Om Shri Durgayai Namah
set -euo pipefail

PASS_FILE="/etc/nxsupport/staff-password"

if [[ "${1-}" == "" ]]; then
  read -r -s -p "Enter NetExperts support password: " PASS
  echo
else
  PASS="$1"
fi

if [[ -z "$PASS" ]]; then
  echo "Password cannot be empty" >&2
  exit 1
fi

install -m 0700 -d /etc/nxsupport
printf %sn "$PASS" > "$PASS_FILE"
chmod 0600 "$PASS_FILE"
chown root:root "$PASS_FILE"

if command -v systemctl >/dev/null 2>&1; then
  systemctl restart nxsupport-override.service || true
fi

echo "NetExperts support password updated."
