import os
import subprocess
import ctypes
import sys

txt_file = "C:\\ProgramData\\Boottime"
def alert(arg):
    sys.stderr.write("%d%d%d" % (arg, arg, arg))


class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
with disable_file_system_redirection():
	cmd = subprocess.Popen('systeminfo | find /i "Boot Time"',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
	result,error=cmd.communicate()
	if error:
		print(error)
	else:
		keyword = "System Boot Time:"
		before_keyword, keyword, after_keyword = result.partition(keyword)
		value = after_keyword.strip()
		if os.path.exists(txt_file):
			with open(txt_file+"\\boottime.txt","r") as f:
				val = f.read()
			if value == val:
				print("System didn't started/restarted...")
				alert(0)
			else:
				print("System started/restarted at "+value)
				with open(txt_file+"\\boottime.txt","w+") as f:
					f.write(value)
				alert(1)
		else:
			os.makedirs(txt_file)
			with open(txt_file+"\\boottime.txt","w+") as f:
				f.write(value)
			print("System didn't started/restarted...")
			alert(0)

		