#!/usr/bin/python3

from tkinter import Tk
from os import listdir
from sys import argv
from sys import exit
from time import sleep

if len(argv) != 2:
    print("Invalid number of arguments.")
    exit(1)

processToTrack = argv[1]
pid = None

sleep(1)

# Bind Tkinter to the root object
root = Tk()

root.attributes('-fullscreen', True)
root.configure(background='black')
root.configure(cursor='none')


def proc_pid_is_valid(pid_to_check):
    try:
        foundNameLine = False
        with open('/proc/{}/status'.format(pid_to_check), mode='rb') as fd:
            for line in fd:
                content = line.decode()
                if "Name:" in content:
                    foundNameLine = True
                    processName = content.split(":")[1].strip()
    except Exception as e:
        pass

    if foundNameLine:
        if processToTrack in processName:
            return True

    return False


def find_pid():
    global pid
    processName = ""
    for dirname in listdir('/proc'):
        if dirname == 'curproc':
            continue
        if proc_pid_is_valid(dirname) is True:
            pid = dirname
            break
    if pid is None:
        exit()


def check_found_pid():
    if proc_pid_is_valid(pid) is False:
        exit()
    root.after(250, check_found_pid)  # reschedule event in 0.25s


find_pid()
root.after(250, check_found_pid)
root.mainloop()
