#!/bin/bash

set -e

# https://github.com/evilsocket/opensnitch/issues/647
wa_grpcio_647()
{
    badversion="1.30.2-3build6"
    source /etc/os-release
    if [ "$ID" = "linuxmint" -o "$ID" = "ubuntu" -o "$ID" = "pop" -o "$ID" = "elementary" -o "$ID" = "zorin" ]; then
        v=$(dpkg-query -W -f '${Version}' python3-grpcio)
        if [ "$v" = "$badversion" ]; then
            echo
            echo
            echo "@@@@@@@@@@@@@@@@@@@ WARNING @@@@@@@@@@@@@@@@@@@@"
            echo "    invalid python3-grpcio version installed"
            echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
            echo
            echo "Installed python3-grpcio package ($badversion) has a bug which makes opensnitch UI unresponsive."
            echo
            echo "Launch opensnitch-ui, and if it consumes 100% of the CPU, try this:"
            echo "~ $ sudo apt install python3-pip"
            echo "~ $ pip3 install grpcio==1.41.0"
            echo "~ $ pip3 install protobuf==3.20.0"
            echo
            echo "More information:"
            echo " - https://bugs.launchpad.net/ubuntu/+source/grpc/+bug/1971114"
            echo " - https://github.com/evilsocket/opensnitch/issues/647"
            echo " - https://github.com/evilsocket/opensnitch/issues/1214#issuecomment-2518864350"
            echo
            echo
        fi
    fi
}

autostart_by_default()
{
    deskfile=/etc/xdg/autostart/opensnitch_ui.desktop
    if [ -d /etc/xdg/autostart -a ! -h  $deskfile -a ! -f $deskfile ]; then
        ln -s /usr/share/applications/opensnitch_ui.desktop /etc/xdg/autostart/
    fi
}

autostart_by_default

if command -v gtk-update-icon-cache >/dev/null && test -f /usr/share/icons/hicolor/index.theme ; then
    gtk-update-icon-cache --quiet /usr/share/icons/hicolor/
fi

wa_grpcio_647

#DEBHELPER#
