#!/usr/bin/python3 -su

## Copyright (C) 2014 troubadour <trobador@riseup.net>
## Copyright (C) 2014 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## Decides to autostart setup-wizard-dist or not.
## - Do not start in live mode (ISO live mode or grub-live).
## - Do not start in old, existing versions.
## - If starting at all, only start at first boot.
## - Do not start when a done or skip file exists.
## - Do not start in Kicksecure-Qubes.
## - Do not start in Qubes-Whonix-Workstation.
## - Do not start in a Qubes Template.
## - Start in Kicksecure (non-Qubes) in version 17.3.5.2 and above.
## - Start in Qubes-Whonix-Gateway.
## - Start in Non-Qubes-Whonix-Gateway.
## - Start in Non-Qubes-Whonix-Workstation.

import sys
import os
import time
import subprocess


def setup_wizard_dist_start():
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: EXECUTING: /usr/libexec/setup-wizard-dist/setup-wizard-dist')
    command = '/usr/libexec/setup-wizard-dist/setup-wizard-dist'
    subprocess.call(command, shell=True)


def check_live_mode_script():
    script="/usr/libexec/helper-scripts/live-mode.sh"

    if not os.access(script, os.X_OK):
        print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: ERROR: "/usr/libexec/helper-scripts/live-mode.sh" not execrable.')
        return False

    result = subprocess.run([script],
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            text=True)

    if result.returncode != 0:
        print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: ERROR: non-zero exit code by "/usr/libexec/helper-scripts/live-mode.sh".')
        return False

    #if "live_status_detected_live_mode_environment_machine='grub-live'" in result.stdout:
        #return True
    #if "live_status_detected_live_mode_environment_machine='iso-live'" in result.stdout:
        #return True
    if "live_status_detected='true'" in result.stdout:
        return True

    return False


print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: START')


paths = [
   '/etc/systemcheck.d/autostart',
   '/usr/local/etc/systemcheck.d/autostart',
]

found = [p for p in paths if os.path.exists(p)]

if found:
   print(f"/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: sytemcheck autostart file exists (found: {found}). Running setup_wizard_dist_start.")
   ## Developers-only feature.
   setup_wizard_dist_start()
   sys.exit(0)


paths = [
   '/var/lib/whonix/do_once/whonixsetup.done',
   '/var/cache/whonix-setup-wizard/status-files/whonixsetup.done',
   '/usr/share/whonix-setup-wizard/status-files/whonixsetup.skip',
   '/var/cache/setup-dist/status-files/setup-dist.done',
   '/usr/share/setup-dist/status-files/setup-dist.skip',
]

found = [p for p in paths if os.path.exists(p)]

if found:
   print(f"/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: setup-dist done file exists (found: {found}). Exiting.")
   sys.exit(0)

if os.path.exists('/run/qubes/this-is-templatevm'):
   print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: File /run/qubes/this-is-templatevm exists. Exiting.')
   sys.exit(0)

if os.path.exists('/usr/share/qubes/marker-vm'):
    ## Qubes detected.
    if os.path.exists('/usr/share/anon-gw-base-files/gateway'):
        print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: Qubes-Whonix-Gateway detected, therefore continuing.')
    else:
        print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: Qubes-Whonix-Gateway not detected. Therefore do not start setup-wizard-dist by default. Exiting.')
        sys.exit(0)

if os.path.exists('/usr/share/anon-gw-base-files/gateway'):
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: Whonix-Gateway detected.')
elif os.path.exists('/usr/share/anon-ws-base-files/workstation'):
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: Whonix-Workstation detected.')
elif os.path.exists('/var/lib/kicksecure/status-files/kicksetup-setup-wizard-dist-first-start-maybe'):
    ## File /var/lib/kicksecure/status-files/kicksetup-setup-wizard-dist-first-start-maybe is created by a chroot-script and
    ## exists since Non-Qubes Kicksecure version 17.3.5.2.
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: Non-Qubes Kicksecure version 17.3.5.2 or above detected.')
else:
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: Neither Whonix-Gateway, Whonix-Workstation, nor high enough Non-Qubes Kicksecure version detected. Exiting.')
    sys.exit(0)

if check_live_mode_script():
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: INFO: System is booted in "LIVE Mode - USER Session" using grub-live. Exiting.')
    sys.exit(0)

setup_wizard_dist_start()

print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: END')
