detect_package_manager() {
  pm=""

  # check for package managers in order of preference
  if command -v -- yay >/dev/null 2>&1; then
    pm="yay"
  elif command -v -- pacman >/dev/null 2>&1; then
    pm="pacman"
  elif command -v -- apt >/dev/null 2>&1; then
    pm="apt"
  elif command -v -- dnf >/dev/null 2>&1; then
    pm="dnf"
  elif command -v -- zypper >/dev/null 2>&1; then
    pm="zypper"
  elif command -v -- brew >/dev/null 2>&1; then
    pm="brew"
  elif command -v -- snap >/dev/null 2>&1; then
    pm="snap"
  elif command -v -- flatpak >/dev/null 2>&1; then
    pm="flatpak"
  else
    pm="unknown"
  fi

  echo "${pm}"
}

detect_package_manager
