# 05-autoinst
# Auto-Install
# If file "/autoinst.conf" is found on CD media or PXE server, then the values
# in that file are used to answer prompts from the first boot scripts, thus 
# installing without user interaction.
# We prompt for a confirmation and wait for an explicit selection before
# proceeding.

# Read the Security Configuration values
SECFILE=/etc/slossec.conf
[ -f "$SECFILE" ] && source "$SECFILE"
# are we auto-firstbooting?
if [ -e /etc/autoinst.conf ] ; then
   AUTOINST=/etc/autoinst.conf
   # source the auto-install config values to make them available to installer
   source $AUTOINST
   log "First-boot 05-autoinst found file $AUTOINST"

   # Detect if running on KVM
   VIRT=
   FORCE_SELECT_MODE="${FIRSTBOOT_RESUME:-0}"
   if [ "$FORCE_SELECT_MODE" == "1" ] ; then
      log "Interrupted firstboot detected. Forcing mode selection screen."
   fi
   if [ -e /tmp/virt ] ; then
      VIRT=$(cat /tmp/virt)
   else
      # Fallback detection using dmidecode
      a=$(dmidecode -s system-product-name 2>/dev/null)
      case $a in
         KVM|*i440FX*|*QEMU*|*Q35*)  VIRT="KVM" ;;
      esac
   fi

   # For KVM: show 30-second countdown — timeout/OK auto-selects, Cancel shows the menu
   # For non-KVM: go straight to the menu (no timeout)
   # Skip auto-selection if resuming from interrupted firstboot, show menu instead
   if [ "$VIRT" == "KVM" ] && [ "$FORCE_SELECT_MODE" != "1" ] && [ -n "$AUTOINST" ] ; then
      log "KVM detected, auto-selecting Auto configuration after 30 seconds"
      $DIALOG \
        --backtitle "$BT" \
        --title "Firstboot Mode" \
        --pause "\n KVM detected.\n\n Auto configuration will start in 30 seconds.\n\n Press OK to start now, or Cancel to choose manually.\n" \
        $H1 $W1 30
      k=$?
      if [ "$k" == "0" ] ; then
         # Timeout expired or OK pressed — skip menu, use auto
         a="1"
      fi
      # Cancel pressed (k!=0) — fall through to the menu below
   fi

   # Show menu: always for non-KVM, or for KVM only if user pressed Cancel above
   if [ "$FORCE_SELECT_MODE" == "1" ] || [ "$VIRT" != "KVM" ] || [ "$k" != "0" ] ; then
      $DIALOG \
        --clear \
        --nocancel \
        --visit-items \
        --timeout 0 \
        --default-item "1" \
        --backtitle "$BT" \
        --title "Firstboot Mode" \
        --menu "\nSelect setup mode:\n\n[Tab: Switch fields | Enter: Accept | Arrows: Navigate]\n" $H1 $W1 2 \
        1 "Auto configuration" \
        2 "Manual configuration" \
        2>$tempfile
      k=$?
      if [ "$k" == "0" ] ; then
         a=$(cat "$tempfile")
      else
         a=
      fi
   fi

   # user selected auto firstboot
   if [ "$a" == "1" ] ; then
     log "Continuing with auto-first-boot. Source $AUTOINST. Applying DHCP IP and default password policy."

   # user selected manual firstboot
   elif [ "$a" == "2" ] ; then
     log "Disabling auto-first-boot."
     unset AUTOINST
     TO="0" # do not timeout of prompts if not auto-first-booting
     log "Manual configuration selected. Manual configuration mode enabled."
     sleep 1

   # if none above, exit firstboot
   else
     FBExit
   fi

fi