#!/usr/bin/python3
import sys
import psutil
import time
import os
import subprocess
import signal

from gi.repository import GLib, Gio

signal.signal(signal.SIGTERM, lambda n, f: sys.exit(0))

uid = int(sys.argv[1])
os.seteuid(uid)

bus = Gio.DBusConnection.new_for_address_sync("unix:path=/run/user/%d/bus" % uid, Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT | Gio.DBusConnectionFlags.MESSAGE_BUS_CONNECTION, None, None)

    # 1: Inhibit logging out
    # 2: Inhibit user switching
    # 4: Inhibit suspending the session or computer
    # 8: Inhibit the session being marked as idle

FLAGS = 1 | 2 | 4 | 8

os.system("killall mate-screensaver")
os.system("killall light-locker")
edition = subprocess.getoutput("crudini --get /etc/linuxmint/info DEFAULT EDITION")
edition = edition.lower().replace('"', '')

if edition == "xfce":
    name = "org.freedesktop.PowerManagement"
    path = "/org/freedesktop/PowerManagement/Inhibit"
    iface = "org.freedesktop.PowerManagement.Inhibit"
    args = GLib.Variant("(ss)", ("mintupgrade", "Performing a system upgrade"))
else:
    name = "org.gnome.SessionManager"
    path = "/org/gnome/SessionManager"
    iface = "org.gnome.SessionManager"
    args = GLib.Variant("(susu)", ("mintupgrade", 0, "Performing a system upgrade", FLAGS))

try:
    bus.call_sync(name,
                  path,
                  iface,
                  "Inhibit",
                  args,
                  None,
                  Gio.DBusCallFlags.NONE,
                  2000,
                  None)
except Exception as e:
    print(e)
    exit(1)

while True:
    time.sleep(1)

exit(0)