name: "Start installer on tty1"
stages:
  initramfs:
    # Start installer if we get the install-mode cmdline and NOT the install-mode-interactive cmdline as we can have both
    # and we dont want to trigger both
    # Works in livecd or uki install mode
    # TODO: Drop nodepair.enable keyword in Kairos 4.0.0
    - if: |
        (grep -q "install-mode" /proc/cmdline || grep -q "nodepair.enable" /proc/cmdline ) && \
        ([ -f /run/cos/live_mode ] || [ -f /run/cos/uki_install_mode ]) && \
        ( [ -e "/sbin/systemctl" ] || [ -e "/usr/bin/systemctl" ] || [ -e "/usr/sbin/systemctl" ] || [ -e "/usr/bin/systemctl" ] )
      commands:
        - systemctl disable getty@tty1
        - systemctl stop getty@tty1
        - systemctl mask getty@tty1
        - systemctl enable kairos
        - systemctl enable kairos-webui
    # Starts installer on boot for openRC based systems
    # TODO: Drop nodepair.enable keyword in Kairos 4.0.0
    - if: |
        (grep -q "install-mode" /proc/cmdline || grep -q "nodepair.enable" /proc/cmdline ) && \
        ([ -f /run/cos/live_mode ] || [ -f /run/cos/uki_install_mode ]) && \
        [ -f "/sbin/openrc" ]
      commands:
        - sed -i -e 's/tty1.*//g' /etc/inittab
        - echo "tty1::respawn:/usr/bin/kairos-agent install tty1" >> /etc/inittab
    # Starts interactive installer on boot for systemd based systems in livecd or uki install mode
    # TODO: Drop interactive-install keyword in Kairos 4.0.0
    - if: |
        (grep -q "interactive-install" /proc/cmdline || grep -q "install-mode-interactive" /proc/cmdline) && \
        ([ -f /run/cos/live_mode ] || [ -f /run/cos/uki_install_mode ]) && \
        ( [ -e "/sbin/systemctl" ] || [ -e "/usr/bin/systemctl" ] || [ -e "/usr/sbin/systemctl" ] || [ -e "/usr/bin/systemctl" ] )
      commands:
        - systemctl stop kairos
        - systemctl disable kairos
        - systemctl disable getty@tty1
        - systemctl stop getty@tty1
        - systemctl mask getty@tty1
        - systemctl enable kairos-interactive
    # Starts installer on boot for openRC based systems
    # TODO: Drop interactive-install keyword in Kairos 4.0.0
    - if: |
        (grep -q "interactive-install" /proc/cmdline || grep -q "install-mode-interactive" /proc/cmdline) && \
        ([ -f /run/cos/live_mode ] || [ -f /run/cos/uki_install_mode ]) && \
        [ -f "/sbin/openrc" ]
      commands:
        - sed -i -e 's/tty1.*//g' /etc/inittab
        - echo "tty1::respawn:/usr/bin/kairos-agent interactive-install --shell tty1" >> /etc/inittab
  boot:
    - if: |
        ([ -f /run/cos/live_mode ] || [ -f /run/cos/uki_install_mode ]) && \
        [ -f "/sbin/openrc" ]
      commands:
        - rc-service kairos-webui start
    - if: |
        ([ -f /run/cos/live_mode ] || [ -f /run/cos/uki_install_mode ]) && \
        ( [ -e "/sbin/systemctl" ] || [ -e "/usr/bin/systemctl" ] || [ -e "/usr/sbin/systemctl" ] || [ -e "/usr/bin/systemctl" ] )
      commands:
        - systemctl start kairos-webui
