#!/bin/sh

export TEXTDOMAIN=earlyoom_scripts
. gettext.sh

events_interface='ru.astralinux.events'
signal='show_earlyoom_notify'

title=$(gettext "Freeing up memory")

EARLYOOM_PID=""
EARLYOOM_UID=""
EARLYOOM_NAME=""
receive_message=false

function read_arg() {
    echo $2 | cut -d'"' -f2
}

dbus-monitor --system "interface=$events_interface, member=$signal" |
while read line; do
    if [[ $line == *"member=$signal"* ]]; then
        count=0
        receive_message=true
    fi
    case $count in
    1)
        EARLYOOM_PID=$(read_arg $line)
        ;;
    2)
        EARLYOOM_UID=$(read_arg $line)
        ;;
    3)
        EARLYOOM_NAME=$(read_arg $line)
        if [ "$receive_message" = true ]; then
            message=$(eval_gettext "Killing process \"\$EARLYOOM_NAME\". To work correctly, it is recommended to close unused applications/browser tabs and enable the use of the swap file (Settings->Swap)")
            notify-send -u normal "$title" "$message" &
        fi
        ;;
    esac
    ((count++))
done

