#!/bin/bash

## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

# shellcheck source=./use_leaprun.sh
source /usr/libexec/helper-scripts/use_leaprun.sh

check_package_manager_running_helper() {
   if [ -f "/run/package_manager_lock" ]; then
      check_apt_get_exit_code="2"
      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 1
   fi

   local fuser_exit_code
   fuser_exit_code=0
   ## use_leaprun and leaprun_exe come from use_leaprun.sh
   if [ "${use_leaprun}" = 'yes' ]; then
      "${leaprun_exe}" apt-check-lock-files &>/dev/null || { fuser_exit_code="$?" ; true; };
   else
      sudo --non-interactive fuser /var/lib/dpkg/lock /var/cache/apt/archives/lock &>/dev/null || { fuser_exit_code="$?" ; true; };
   fi

   ## 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

   true "fuser_exit_code: $fuser_exit_code"
   if [ "$fuser_exit_code" = "0" ]; then
      true "package manager is running"
      check_apt_get_exit_code="1"
   else
      true "no package manager is running"
      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 managers 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 managers 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 manager currently running."
      package_manager_waiting_msg_x="$package_manager_waiting_msg"
   fi

   return "$check_apt_get_exit_code"
}
