#!/bin/bash

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

#set -x

check_network_interfaces() {
   local if_you_know_what_you_are_doing_msg
   if_you_know_what_you_are_doing_msg="$(if_you_know_what_you_are_doing_funct "$FUNCNAME")"

   if test -f /run/qubes/this-is-templatevm ; then
      systemcheck_external_network_found="unknown"
      ## Qubes Templates are non-networked as per Qubes default.
      return 0
   fi

   local network_ip_data

   ## {{{ Kicksecure

   if [ "$vm_lower_case_short" = "machine" ]; then
      if /usr/libexec/helper-scripts/check-network-access; then
         systemcheck_external_network_found=true
         local MSG="<p>check network interfaces Result: External network interface found."
         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

      systemcheck_external_network_found=false
      local MSG="\
<p>check network interfaces Result: No external network interface found.<br />
<br />
Recommendation:<br />
https://www.kicksecure.com/wiki/Troubleshooting<br />
<br />
Debugging information:<br />
Command<br />
<code>ip -o addr show scope global</code><br />
did not show any output<br />
<br />
If this error happens only during upgrading or is transient this error can be safely ignored.
<br />
$if_you_know_what_you_are_doing_msg</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
      return 0
   fi

   ## }}}

   ## {{{ Whonix

   local failed eth0_failed eth1_failed

   failed=false
   if ! leaprun eth0-carrier &>/dev/null ; then
      eth0_failed=true
      failed=true
   fi

   if [ "$vm_lower_case_short" = "gateway" ]; then
      if ! leaprun eth1-carrier &>/dev/null ; then
         eth1_failed=true
         failed=true
      fi
   fi

   if [ "$failed" = "false" ]; then
      systemcheck_external_network_found=true
      local MSG="<p>check network interfaces Result: 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
      return 0
   fi

   if [ "$eth0_failed" = "true" ]; then
      systemcheck_external_network_found=false
      local MSG="<p>check network interfaces Result: network interface <code>eth0</code> not up!<br />
<br />
Recommendation:<br />
Try to manually start $PROJECT_NAME networking.
<blockquote>
<code>sudo systemctl restart networking</code>
</blockquote>
Or reboot.<br />
<br />
Debugging information:<br />
Command<br />
<code>cat /sys/class/net/eth0/carrier</code><br />
failed.<br />
<br />
If this error happens only during upgrading or is transient this error can be safely ignored.
<br />
$if_you_know_what_you_are_doing_msg</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
      return 0
   fi

   if [ "$eth1_failed" = "true" ]; then
      if [ "$qubes_detected" = "true" ]; then
         local qubes_extra_message="This might happen because you did not check mark the 'provides network' box for this VM?"
      fi

      local MSG="<p>check network interfaces Result: network interface <code>eth1</code> not up!

$qubes_extra_message

$if_you_know_what_you_are_doing_msg

Debugging information:
Command
<code>cat /sys/class/net/eth1/carrier</code>
failed.</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
      return 0
   fi

   ### }}}
}
