import os
import subprocess

def log_status(message):
    print("[STATUS] {}".format(message))
    if hasattr(sys.stdout, 'flush'):
        sys.stdout.flush()

def launch_ocs_systray():
    ocs_systray_path = r"C:\Program Files\OCS Inventory Agent\OcsSystray.exe"
    log_status("Checking for OCS Systray at {}".format(ocs_systray_path))
    
    if os.path.exists(ocs_systray_path):
        try:
            log_status("File exists: True")
            log_status("Launching OCS Systray using 'start' command...")

            # Use os.devnull for older Python versions
            with open(os.devnull, 'w') as devnull:
                subprocess.Popen('start "" "{}"'.format(ocs_systray_path), 
                                 shell=True, stdout=devnull, stderr=devnull)

            log_status("OcsSystray launched successfully. Script can now exit.")
        except Exception as e:
            log_status("Error launching OcsSystray: {}".format(e))
    else:
        log_status("OcsSystray.exe not found at specified path.")

# Main script execution
log_status("Script started.")
launch_ocs_systray()
log_status("Script completed.")