#!/usr/bin/env python3
# -*- coding: UTF-8 -*-


import wx
import gettext
import pwd
import grp
import os

def drop_privileges(uid_name='nobody', gid_name='nogroup'):
    if os.getuid() != 0:
        # We're not root so, like, whatever dude
        return

    # Get the uid/gid from the name
    running_uid = pwd.getpwnam(uid_name).pw_uid
    running_gid = grp.getgrnam(gid_name).gr_gid

    # Remove group privileges
    os.setgroups([])

    # Try setting the new uid/gid
    os.setgid(running_gid)
    os.setuid(running_uid)

    # Ensure a very conservative umask
    old_umask = os.umask(0o77)


from UpsDisplayFrame import UpsDisplayFrame

if __name__ == "__main__":
    gettext.install("upsdisplay") # replace with the appropriate catalog name

    UpsDisplay = wx.App()

    # The program starts with root priveleges (weston/wayland doesn't like starting as non-root)
    # This drops the priveleges so we don't run unsafely.
    drop_privileges('ups', 'ups')

    upsdisplayFrame = UpsDisplayFrame(None, wx.ID_ANY, "")
    UpsDisplay.SetTopWindow(upsdisplayFrame)
    upsdisplayFrame.Show()
    UpsDisplay.MainLoop()

