username="motion"
groupname="motion"

_setup_user_and_group() {
  getent group $groupname &>/dev/null || groupadd -r $groupname 1>/dev/null
  getent passwd $username &>/dev/null || useradd -r -g $groupname -s "/bin/bash" $username 1>/dev/null

  chown -R $username:$groupname "/etc/motioneye" 1>/dev/null
}

post_install() {
  echo "If motion.service is installed, stop and disable it to ensure proper functionality."
  echo "Execute 'systemctl disable --now motion' in a terminal."
  echo "Then, reload the system daemon with 'systemctl daemon-reload'."
  echo "Finally, enable and start motioneye with 'systemctl enable --now motioneye'."

  cat <<INFO

  The default login credentials for the web interface are:

     username: admin
     password:                               (no password)

INFO
  _setup_user_and_group
}

post_upgrade() {
  echo "To restart the motioneye service and apply the changes, run: 'systemctl restart motioneye'"
  _setup_user_and_group
}

post_remove() {
  if getent passwd $username >/dev/null; then
    userdel $username >/dev/null
  fi
  if getent group $groupname >/dev/null; then
    groupdel $groupname >/dev/null
  fi
}
