# Path: debug.py
# def debug(level,text,DEBUG,bindir):
#     try:
#         if bindir == False or level <= 0:
#             print("DEBUG: %s" % (text))
#             return False
#         bindir = os.getcwd()
#         if bindir.endswith("system32"):
#             if os.path.exists(devdir()):
#                 bindir=devdir()
#             else:
#                 print("DEBUG: %s" % (text))
#                 return False
#         
#         logfile = "%s\\client_debug.log" % (bindir)
#         global DEBUGcount
#         global DEBUGfrombefore
#         
#         if level > LOGLEVEL:
#             if not level in LOGLEVELS:
#                 return False
#         
#         timefromboot = round(time.time() - BOOTTIME,3)
#         tempdebuglist = list()
#         if DEBUGcount > 0 and not DEBUGfrombefore == text:
#             debugstringsht1 = "(%s):(d1) %s (repeat: %s)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             debugstringsht2 = "(%s):(d2) %s" % (timefromboot,text)
#             tempdebuglist.append(debugstringsht1)
#             tempdebuglist.append(debugstringsht2)
#             DEBUGcount = 0
#         elif DEBUGcount >= 4096 and DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d3) %s (repeated: %s e2)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             tempdebuglist.append(debugstringsht)
#             DEBUGcount = 0
#         elif DEBUGfrombefore == text:
#             DEBUGcount += 1
#             return
#         elif not DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d4) %s"%(timefromboot,text)
#             tempdebuglist.append(debugstringsht)
#         DEBUGfrombefore = text
#         if len(tempdebuglist) > 0:
#             for entry in tempdebuglist:
#                 print(entry)
#                 debug_cache(entry,'add')
#                 if DEBUG == True:
#                     write_debug(level,entry,timefromboot,logfile)
#     except Exception as e:
#         print("[debug.py] debug failed, exception = '%s'" % (e))
# 
# Path: debug.py
# def devmode():
#     return getmode("DEVMODE")
# 
# Path: debug.py
# def devdir():
#     dir = "E:\\ovpn-client"
#     return dir


import os, subprocess
import hashings
import openvpn
from debug import debug
from debug import devmode
from debug import devdir
def find_signtool(DEBUG):
    debug(1,"[signtool.py] def find_signtool()",DEBUG,True)
    try:
        BIN_DIR = os.getcwd()
        ARCH = openvpn.get_arch(DEBUG)
        
        exe64 = "signtool_w10sdk_x64.exe"
        signtool64 = "%s\\%s" % (BIN_DIR,exe64)
        signtoolhash64 = "e85d79cc617642f585cb9e4ad5dd919b8d15570a291bdd1e69fd38d4f278bc4b6f110329e8fa6948b7d516917cc2bde43532cde33e8c5e66355d0c97cfd7ebc2"
        
        exe32 = "signtool_w10sdk_x86.exe"
        signtool32 = "%s\\%s" % (BIN_DIR,exe32)
        signtoolhash32 = "233304669aefeec9ad5d19bd2dd5bb19ea35ce31da0b3aabe5ab859259608a58725fac5993637c9635e5912138d3eb477773351f0ee81cc3ce756d713163cf31"
        
        if ARCH == "x86_64":
            if os.path.isfile(signtool64):
                signtool = signtool64
                signtoolhash = signtoolhash64
            elif os.path.isfile(signtool32):
                signtool = signtool32
                signtoolhash = signtoolhash32
            else:
                signtool = "%s\\includes\\codesign\\%s" % (devdir(),exe64)
                signtoolhash = signtoolhash64
        else:
            signtool = signtool32
            signtoolhash = signtoolhash32
        
        if not os.path.isfile(signtool):
            debug(1,"[signtool.py] def signtool_verify_files: signtool '%s' not found"%(signtool),DEBUG,True)

# Path: debug.py
# def debug(level,text,DEBUG,bindir):
#     try:
#         if bindir == False or level <= 0:
#             print("DEBUG: %s" % (text))
#             return False
#         bindir = os.getcwd()
#         if bindir.endswith("system32"):
#             if os.path.exists(devdir()):
#                 bindir=devdir()
#             else:
#                 print("DEBUG: %s" % (text))
#                 return False
#         
#         logfile = "%s\\client_debug.log" % (bindir)
#         global DEBUGcount
#         global DEBUGfrombefore
#         
#         if level > LOGLEVEL:
#             if not level in LOGLEVELS:
#                 return False
#         
#         timefromboot = round(time.time() - BOOTTIME,3)
#         tempdebuglist = list()
#         if DEBUGcount > 0 and not DEBUGfrombefore == text:
#             debugstringsht1 = "(%s):(d1) %s (repeat: %s)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             debugstringsht2 = "(%s):(d2) %s" % (timefromboot,text)
#             tempdebuglist.append(debugstringsht1)
#             tempdebuglist.append(debugstringsht2)
#             DEBUGcount = 0
#         elif DEBUGcount >= 4096 and DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d3) %s (repeated: %s e2)" % (timefromboot, DEBUGfrombefore,DEBUGcount)
#             tempdebuglist.append(debugstringsht)
#             DEBUGcount = 0
#         elif DEBUGfrombefore == text:
#             DEBUGcount += 1
#             return
#         elif not DEBUGfrombefore == text:
#             debugstringsht = "(%s):(d4) %s"%(timefromboot,text)
#             tempdebuglist.append(debugstringsht)
#         DEBUGfrombefore = text
#         if len(tempdebuglist) > 0:
#             for entry in tempdebuglist:
#                 print(entry)
#                 debug_cache(entry,'add')
#                 if DEBUG == True:
#                     write_debug(level,entry,timefromboot,logfile)
#     except Exception as e:
#         print("[debug.py] debug failed, exception = '%s'" % (e))
# 
# Path: debug.py
# def devmode():
#     return getmode("DEVMODE")
# 
# Path: debug.py
# def devdir():
#     dir = "E:\\ovpn-client"
#     return dir


# Path: signtool.py
import os, subprocess
import hashings
import openvpn
from debug import debug
from debug import devmode
from debug import devdir

def find_signtool(DEBUG):
    debug(1,"[signtool.py] def find_signtool()",DEBUG,True)
    try:
        BIN_DIR = os.getcwd()
        ARCH = openvpn.get_arch(DEBUG)
        
        exe64 = "signtool_w10sdk_x64.exe"
        signtool64 = "%s\\%s" % (BIN_DIR,exe64)
        signtoolhash64 = "e85d79cc617642f585cb9e4ad5dd919b8d15570a291bdd1e69fd38d4f278bc4b6f110329e8fa6948b7d516917cc2bde43532cde33e8c5e66355d0c97cfd7ebc2"
        
        exe32 = "signtool_w10sdk_x86.exe"
        signtool32 = "%s\\%s" % (BIN_DIR,exe32)
        signtoolhash32 = "233304669aefeec9ad5d19bd2dd5bb19ea35ce31da0b3aabe5ab859259608a58725fac5993637c9635e5912138d3eb477773351f0ee81cc3ce756d713163cf31"
        
        if ARCH == "x86_64":
            if os.path.isfile(signtool64):
                signtool = signtool64
                signtoolhash = signtoolhash64
            elif os.path.isfile(signtool32):
                signtool = signtool32
                signtoolhash = signtoolhash32
            else:
                signtool = "%s\\includes\\codesign\\%s" % (devdir(),exe64)
                signtoolhash = signtoolhash64
        else:
            signtool = signtool32
            signtoolhash = signtoolhash32
        
        if not os.path.isfile(signtool):
            debug(1,"[signtool.py] def signtool_verify_files: signtool '%s' not found"%(signtool),DEBUG,True)

            signtool = "%s\\includes\\codesign\\%s" % (devdir(),exe32)