# 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))


import os, subprocess, datetime
import release_version
from debug import debug
    lines.append('      <Interval>PT1M</Interval>')
    lines.append('      <Count>3</Count>')
    lines.append('    </RestartOnFailure>')
    lines.append('  </Settings>')
    lines.append('  <Actions Context="Author">')
    lines.append('    <Exec>')
    lines.append('      <Command>"%s\%s"</Command>' % (BIN_DIR,release_version.setup_data()["exename"]))
    lines.append('      <WorkingDirectory>%s</WorkingDirectory>' % (BIN_DIR))
    lines.append('    </Exec>')
    lines.append('  </Actions>')
    lines.append('</Task>')

    XMLFILE = "%s\\autostart.xml" % (BIN_DIR)
    remove_xml(DEBUG,XMLFILE)
    ind = open(XMLFILE, "wt")
    for line in lines:
        ind.write("%s\n"%(line))
    ind.close()
    if not os.path.exists(XMLFILE):
        return False

    if os.path.exists(XMLFILE):
        string = 'schtasks.exe /Create /XML "%s" /TN "%s"' % (XMLFILE,release_version.version_data()["NAME"])
        try:
            exitcode = subprocess.check_call("%s" % (string),shell=True)
            remove_xml(DEBUG,XMLFILE)
            if exitcode == 0:
                debug(1,"[schedule_task.py] def cb_switch_autostart: enable Ok",DEBUG,True)
                return True
            else:

# 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: schedule_task.py
import os, subprocess, datetime
import release_version
from debug import debug

    lines.append('      <Interval>PT1M</Interval>')
    lines.append('      <Count>3</Count>')
    lines.append('    </RestartOnFailure>')
    lines.append('  </Settings>')
    lines.append('  <Actions Context="Author">')
    lines.append('    <Exec>')
    lines.append('      <Command>"%s\%s"</Command>' % (BIN_DIR,release_version.setup_data()["exename"]))
    lines.append('      <WorkingDirectory>%s</WorkingDirectory>' % (BIN_DIR))
    lines.append('    </Exec>')
    lines.append('  </Actions>')
    lines.append('</Task>')

    XMLFILE = "%s\\autostart.xml" % (BIN_DIR)
    remove_xml(DEBUG,XMLFILE)
    ind = open(XMLFILE, "wt")
    for line in lines:
        ind.write("%s\n"%(line))
    ind.close()
    if not os.path.exists(XMLFILE):
        return False

    if os.path.exists(XMLFILE):
        string = 'schtasks.exe /Create /XML "%s" /TN "%s"' % (XMLFILE,release_version.version_data()["NAME"])
        try:
            exitcode = subprocess.check_call("%s" % (string),shell=True)
            remove_xml(DEBUG,XMLFILE)
            if exitcode == 0:
                debug(1,"[schedule_task.py] def cb_switch_autostart: enable Ok",DEBUG,True)
                return True
            else:

                debug(1,"[schedule_task.py] def cb_switch_autostart: enable fail, exitcode = '%s'" % (exitcode),DEBUG,True)