#!/bin/bash

check_package_manager_running_helper() {
   if [ -f "/run/package_manager_lock" ]; then
      check_apt_get_exit_code="/run/package_manager_lock exists."
      package_manager_waiting_msg="Lock file \
/run/package_manager_lock exists. Waiting for it to be removed..."
      package_manager_waiting_msg_x="$package_manager_waiting_msg"
      return 0
   fi

   local fuser_exit_code
   fuser_exit_code=0
   sudo --non-interactive fuser /var/lib/dpkg/lock /var/cache/apt/archives/lock &>/dev/null || { fuser_exit_code="$?" ; true; };

   ## If a package manager is running:
   ## sudo --non-interactive fuser /var/lib/dpkg/lock /var/cache/apt/archives/lock ; echo $?
   ## /var/lib/dpkg/lock:  15601
   ## /var/cache/apt/archives/lock: 15601
   ## 0
   ##
   ## If no package manager is running:
   ## sudo --non-interactive fuser /var/lib/dpkg/lock /var/cache/apt/archives/lock ; echo $?
   ## 1

   if [ "$fuser_exit_code" = "0" ]; then
      check_apt_get_exit_code="1"
   else
      check_apt_get_exit_code="0"
   fi

   if [ ! "$check_apt_get_exit_code" = "0" ]; then
      ## package_manager_waiting_msg used by cli-only applications.
      package_manager_waiting_msg="A package manager (such as apt-get) is currently running. Waiting for it to finish...

If you are not aware of any package mangers running, exit now, find out if there are any issues with dpkg or apt-get. Run in the terminal for example:
sudo dpkg --audit
sudo dpkg --configure -a
sudo apt-get dist-upgrade

Technical Info:
\"sudo --non-interactive fuser /var/lib/dpkg/lock /var/cache/apt/archives/lock\" exit code: $fuser_exit_code"
      ## package_manager_waiting_msg_x used applications using msgcollector.
      package_manager_waiting_msg_x="<p>A package manager (such as <code>apt-get</code>) is currently running. Waiting for it to finish...
<br></br>
If you are not aware of any package mangers running, exit now, find out if there are any issues with <code>dpkg</code> or <code>apt-get</code>. Run in the terminal for example:
<code>sudo dpkg --audit</code>
<code>sudo dpkg --configure -a</code>
<code>sudo apt-get dist-upgrade</code>
<br></br>
Technical Info:
\"<code>sudo --non-interactive fuser /var/lib/dpkg/lock /var/cache/apt/archives/lock</code>\" exit code: <code>$fuser_exit_code</code></p>"
   else
      package_manager_waiting_msg="No package manger currently running. \
You should not see this message. Please report this bug!"
      package_manager_waiting_msg_x="$package_manager_waiting_msg"
   fi
}
