# Wizard Kit: New system setup

import os
import sys

# Init
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from functions.activation import *
from functions.browsers import *
from functions.cleanup import *
from functions.info import *
from functions.product_keys import *
from functions.setup import *
from functions.sw_diags import *
init_global_vars()
os.system('title {}: New System Setup'.format(KIT_NAME_FULL))
set_log_file('New System Setup.log')

if __name__ == '__main__':
  other_results = {
    'Error': {
      'BIOSKeyNotFoundError':     'BIOS key not found',
      'CalledProcessError':       'Unknown Error',
      'FileNotFoundError':        'File not found',
      'GenericError':             'Unknown Error',
      'Not4KAlignedError':        'False',
      'SecureBootDisabledError':  'Disabled',
    },
    'Warning': {
      'GenericRepair':            'Repaired',
      'NoProfilesError':          'No profiles found',
      'NotInstalledError':        'Not installed',
      'OSInstalledLegacyError':   'OS installed Legacy',
      'SecureBootNotAvailError':  'Not available',
      'SecureBootUnknownError':   'Unknown',
      'UnsupportedOSError':       'Unsupported OS',
    }}
  try:
    stay_awake()
    clear_screen()

    # Check installed OS
    if os_is_unsupported(show_alert=True):
      print_warning('OS version not supported by this script')
      if not ask('Continue anyway? (NOT RECOMMENDED)'):
        abort()

    # Select AV software
    # NOTE: Truth tuple is (ESET, ESET_PUPS, MSE)
    av_options = [
      {'Name': 'ESET NOD32',
        'Truths': (True, True, False),},
      {'Name': 'ESET NOD32 (no PUP/PUW scans)',
        'Truths': (True, False, False),},
      {'Name': 'Microsoft Security Essentials',
        'Disabled': global_vars['OS']['Version'] not in ['7'],
        'Truths': (False, False, True),},
      ]
    actions = [
      {'Name': 'None', 'Letter': 'N'},
      {'Name': 'Quit', 'Letter': 'Q'},
      ]
    selection = menu_select(
      'Please select an option to install',
      main_entries=av_options,
      action_entries=actions)
    if selection.isnumeric():
      index = int(selection) - 1
      answer_eset, answer_pups, answer_mse = av_options[index]['Truths']
    elif selection == 'Q':
      abort()
    else:
      answer_eset = False
      answer_pups = False
      answer_mse = False

    # Install LibreOffice?
    answer_libreoffice = ask('Install LibreOffice?')

    # Install software
    print_info('Installing Programs')
    install_vcredists()
    if answer_eset:
      install_eset_nod32_av(scan_pups=answer_pups)
    if answer_libreoffice:
      try_and_print(message='LibreOffice...',
        function=install_libreoffice, cs='done',
        quickstart=True,
        register_mso_types=True,
        use_mso_formats=True,
        vcredist=False,
        )
    result = try_and_print(
      message='Ninite bundle...',
      function=install_ninite_bundle, cs='Started',
      mse=answer_mse, libreoffice=answer_libreoffice,
      other_results=other_results)
    try:
      for proc in result['Out']:
        # Wait for all processes to finish
        print_standard('Waiting for installations to finish...')
        proc.wait()
    except KeyboardInterrupt:
      pass

    # Scan for supported browsers
    print_info('Scanning for browsers')
    scan_for_browsers(skip_ie=True)

    # Install extensions
    print_info('Installing Extensions')
    try_and_print(message='Classic Shell skin...',
      function=install_classicstart_skin,
      other_results=other_results)
    try_and_print(message='Google Chrome extensions...',
      function=install_chrome_extensions)
    try_and_print(message='Mozilla Firefox extensions...',
      function=install_firefox_extensions,
      other_results=other_results)

    # Configure software
    print_info('Configuring programs')
    install_adblock()
    print_standard('Please enable uBlock Origin for all browsers.')
    pause()
    if global_vars['OS']['Version'] == '10':
      try_and_print(message='ClassicStart...',
        function=config_classicstart, cs='Done')
      try_and_print(message='Explorer (user)...',
        function=config_explorer_user, cs='Done')

    # Configure system
    print_info('Configuring system')
    if global_vars['OS']['Version'] == '10':
      try_and_print(message='Explorer (system)...',
        function=config_explorer_system, cs='Done')
      try_and_print(message='Disabling telemetry...',
        function=disable_windows_telemetry, cs='Done')
      try_and_print(message='Enabling RegBack...',
        function=enable_regback, cs='Done')
      try_and_print(message='Windows Updates...',
        function=config_windows_updates, cs='Done')
    try_and_print(message='Enabling System Restore...',
      function=enable_system_restore, cs='Done')
    try_and_print(message='Updating Clock...',
      function=update_clock, cs='Done')

    # Restart Explorer
    try_and_print(message='Restarting Explorer...',
      function=restart_explorer, cs='Done')

    # Summary
    print_info('Summary')
    try_and_print(message='Operating System:',
      function=show_os_name, ns='Unknown', silent_function=False)
    try_and_print(message='Activation:',
      function=show_os_activation, ns='Unknown', silent_function=False)
    if (not windows_is_activated()
      and global_vars['OS']['Version'] in ('8', '8.1', '10')):
      try_and_print(message='BIOS Activation:',
        function=activate_with_bios,
        other_results=other_results)
    try_and_print(message='Secure Boot Status:',
      function=check_secure_boot_status, other_results=other_results)
    try_and_print(message='Installed RAM:',
      function=show_installed_ram, ns='Unknown', silent_function=False)
    show_free_space()
    try_and_print(message='Installed Antivirus:',
      function=get_installed_antivirus, ns='Unknown',
      other_results=other_results, print_return=True)
    try_and_print(message='Partitions 4K aligned:',
      function=check_4k_alignment, cs='True',
      other_results=other_results)

    # Play audio, show devices, open Windows updates, and open Activation
    try_and_print(message='Opening Device Manager...',
      function=open_device_manager, cs='Started')
    try_and_print(message='Opening HWiNFO (Sensors)...',
      function=run_hwinfo_sensors, cs='Started', other_results=other_results)
    try_and_print(message='Opening SDI Origin...',
      function=open_snappy_driver_origin, cs='Started')
    try_and_print(message='Opening Windows Updates...',
      function=open_windows_updates, cs='Started')
    if not windows_is_activated():
      try_and_print(message='Opening Windows Activation...',
        function=open_windows_activation, cs='Started')
    sleep(3)
    try_and_print(message='Running XMPlay...',
      function=run_xmplay, cs='Started', other_results=other_results)
    try:
      check_secure_boot_status(show_alert=True)
    except:
      # Only trying to open alert message boxes
      pass

    # Done
    print_standard('\nDone.')
    pause('Press Enter to exit...')
    exit_script()
  except SystemExit as sys_exit:
    exit_script(sys_exit.code)
  except:
    major_exception()

# vim: sts=2 sw=2 ts=2
