#!/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 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


if (os.path.exists('/var/lib/whonix/do_once/whonixsetup.done') or
   os.path.exists('/var/cache/whonix-setup-wizard/status-files/whonixsetup.done') or
   os.path.exists('/usr/share/whonix-setup-wizard/status-files/whonixsetup.skip') or
   os.path.exists('/var/cache/setup-dist/status-files/setup-dist.done') or
   os.path.exists('/usr/share/setup-dist/status-files/setup-dist.skip')
):
   print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: setup-dist done file exists. Exiting.')
   sys.exit()

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

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: Qubes-Whonix-Gateway detected, therefore continuing.')
    else:
        print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: Qubes-Whonix-Gateway not detected. Therefore do not start setup-wizard-dist by default. Exiting.')
        sys.exit()

if os.path.exists('/usr/share/anon-gw-base-files/gateway'):
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: 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: 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: Non-Qubes Kicksecure version 17.3.5.2 or above detected.')
else:
    print('/usr/libexec/setup-wizard-dist/setup-dist_check_for_start: Neither Whonix-Gateway, Whonix-Workstation, nor high enough Non-Qubes Kicksecure version detected. Exiting.')
    sys.exit()

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

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)
