#!/bin/bash

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

check_meta_packages() {
   local meta_package_wanted_list meta_package_installed

   local if_you_know_what_you_are_doing_msg see_also_link
   if_you_know_what_you_are_doing_msg="$(if_you_know_what_you_are_doing_funct "$FUNCNAME")"
   see_also_link="<a href=${PROJECT_HOMEPAGE}/wiki/Debian_Packages>${PROJECT_HOMEPAGE}/wiki/Debian_Packages</a>"

   if [ "$qubes_detected" = "true" ]; then
      meta_package_wanted_list="<code>qubes-whonix-gateway</code> or <code>qubes-whonix-workstation</code>"
      if [ "$vm_lower_case_short" = "gateway" ]; then
         meta_package_wanted_list="\
whonix-gateway-qubes-gui-lxqt \
whonix-gateway-qubes-server"
      elif [ "$vm_lower_case_short" = "workstation" ]; then
         meta_package_wanted_list="\
whonix-workstation-qubes-gui-lxqt \
whonix-workstation-qubes-server"
      elif [ "$vm_lower_case_short" = "machine" ]; then
         meta_package_wanted_list="\
kicksecure-qubes-gui-lxqt \
kicksecure-qubes-server"
      fi
   else
      if [ "$vm_lower_case_short" = "gateway" ]; then
         meta_package_wanted_list="\
whonix-gateway-vm-gui-lxqt \
whonix-gateway-vm-server \
whonix-gateway-baremetal-gui-lxqt \
whonix-gateway-baremetal-server"
      elif [ "$vm_lower_case_short" = "workstation" ]; then
         meta_package_wanted_list="\
whonix-workstation-vm-gui-lxqt \
whonix-workstation-vm-server \
whonix-workstation-baremetal-gui-lxqt \
whonix-workstation-baremetal-server"
      elif [ "$vm_lower_case_short" = "machine" ]; then
         meta_package_wanted_list="\
kicksecure-vm-gui-lxqt \
kicksecure-vm-server \
kicksecure-baremetal-gui-lxqt \
kicksecure-baremetal-server"
      fi
   fi

   ## Fallback if neither gateway nor workstation was detected.
   if [ "$meta_package_wanted_list" = "" ]; then
      meta_package_wanted_list="\
whonix-gateway-vm-gui-lxqt \
whonix-gateway-vm-server \
whonix-gateway-baremetal-gui-lxqt \
whonix-gateway-baremetal-server \
whonix-workstation-vm-gui-lxqt \
whonix-workstation-vm-server \
whonix-workstation-baremetal-gui-lxqt \
whonix-workstation-baremetal-server"
   fi

   for meta_package_wanted_item in $meta_package_wanted_list ; do
      if dpkg --status "$meta_package_wanted_item" &> /dev/null ; then
         meta_package_installed="$meta_package_wanted_item"
         local MSG="<p>$PROJECT_NAME Meta Packages Test Result: Meta package <code>$meta_package_installed</code> installed, ok.</p>"
         if [ "$verbose" -ge "1" ]; then
            $output_x ${output_opts[@]} --messagex --typex "info" --message "$MSG"
            $output_cli ${output_opts[@]} --messagecli --typecli "info" --message "$MSG"
         fi
      fi
   done

   if [ ! "$meta_package_installed" = "" ]; then
      return 0
   fi

   local MSG="<p>$PROJECT_NAME Meta Packages Test Result: No meta package is installed.<br />
Did you accidentally uninstall it?<br />
<br />
Make sure any of the following meta packages is installed.<br />
<br />
<code>$meta_package_wanted_list</code>
<br />
<br />See also: $see_also_link
<br />
<br />$if_you_know_what_you_are_doing_msg</p>"

   $output_x ${output_opts[@]} --messagex --typex "warning" --message "$MSG"
   $output_cli ${output_opts[@]} --messagecli --typecli "warning" --message "$MSG"
}

check_unwanted_packages() {
   local unwanted_package unwanted_packages_list unwanted_packages_counter dpkg_query_result first
   unwanted_packages_counter="0"
   for unwanted_package in $systemcheck_unwanted_package ; do
      dpkg_query_result="$(dpkg-query --show --showformat='${Status}\n' "$unwanted_package" 2>/dev/null)" || true
      ## dpkg_query_result example:
      ## when installed           : install ok installed
      ## when not fully installed : install ok unpacked
      ## when removed             : deinstall ok config-files
      ## when purged, no stdout
      read -r first _ <<< "$dpkg_query_result"
      if [ "install" = "$first" ]; then
         unwanted_packages_counter="$(( $unwanted_packages_counter + 1 ))"
         unwanted_packages_list+="$unwanted_package "
      fi
   done

   if [ "$unwanted_packages_counter" = "0" ]; then
      local MSG="<p>$PROJECT_NAME Unwanted Packages Test Result: None found.</p>"
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "info" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "info" --message "$MSG"
      fi
      return 0
   fi

   local if_you_know_what_you_are_doing_msg see_also_link
   if_you_know_what_you_are_doing_msg="$(if_you_know_what_you_are_doing_funct "$FUNCNAME")"
   see_also_link="<a href=${PROJECT_HOMEPAGE}/wiki/Debian_Packages>${PROJECT_HOMEPAGE}/wiki/Debian_Packages</a>"

   local packages_help_msg
   packages_help_msg="<br></br>1. Open a terminal. ($persistent_changes_start_menu_instructions_first_part Terminal)
<br></br>2. Purge. <blockquote><code>sudo apt-get purge $unwanted_packages_list</code></blockquote>"

   if [ "$qubes_detected" = "true" ]; then
      if [ "$qubes_vm_persistence" = "full" ]; then
         ## Running in either StandaloneVM or TemplateVM.
         ## Above existing packages_help_msg is fine.
         true
      else
         ## TemplateBasedVM
         packages_help_msg="<br></br>1. Open a terminal. ($persistent_changes_start_menu_instructions_first_part Terminal)
<br></br>2. Purge. <blockquote><code>sudo apt-get purge $unwanted_packages_list</code></blockquote>
3. Shutdown your TemplateVM. (dom0 -> Qubes VM Manager -> right click '<code>$qubes_base_template</code>' -> Shutdown VM)
<br></br>4. Shutdown and restart this TemplateBased <code>$qubes_vm_type</code>. (dom0 -> Qubes VM Manager -> right click '<code>$qubes_name_of_vm</code>' -> Shutdown VM)"
      fi
   fi

   local MSG="<p>$PROJECT_NAME Unwanted Packages Test Result: $unwanted_packages_counter unwanted package(s) installed. \
It is recommended that you remove them from $update_where.
$packages_help_msg
<br />See also: $see_also_link
<br />$if_you_know_what_you_are_doing_msg</p>"
   $output_x ${output_opts[@]} --messagex --typex "warning" --message "$MSG"
   $output_cli ${output_opts[@]} --messagecli --typecli "warning" --message "$MSG"
}

check_packages() {
   systemcheck_run_function check_meta_packages
   systemcheck_run_function check_unwanted_packages
}
