# If GRUB_TERMINAL_OUTPUT isn't manually set (during install)
# Then we will parse the output of grub.platform and determine
# what output mode should be used for BIOS/UEFI
if [ -z "$GRUB_TERMINAL_OUTPUT" ] ; then
	if [ "`kenv grub.platform`" = "pc" ] ; then
		# If booted with BIOS
		GRUB_TERMINAL_OUTPUT=console
	else
		# If booted with UEFI
		GRUB_TERMINAL_OUTPUT=gfxterm
	fi
fi

if [ $(/usr/local/bin/sqlite3 /data/freenas-v1.db "SELECT adv_serialconsole FROM system_advanced") -eq 1 ]; then
        port=$(/usr/local/bin/sqlite3 /data/freenas-v1.db "SELECT adv_serialport FROM system_advanced")
        speed=$(/usr/local/bin/sqlite3 /data/freenas-v1.db "SELECT adv_serialspeed FROM system_advanced")
        unit=$(/usr/sbin/devinfo -u |awk -v port=$port '$1 ~ port { print $2 }' |tr -dc 0-9)
	# From a fresh install with serial enabled its most likely these values have not been hand-set
	# by the user (yet). Let grub auto-detect do its thing in that case.
	if [ -n "$port" -a -n "$speed" -a -n "$unit" ] ; then
		GRUB_SERIAL_COMMAND="serial --port=$port --speed=$speed --unit=$unit"
	fi
        GRUB_TERMINAL_INPUT="console serial"
        GRUB_TERMINAL_OUTPUT="console serial"
        unset port speed unit
fi
