#!/bin/bash
###################################################################################
# File.......: /usr/lib/setup/armedslack-setconsole
# Called from: /usr/lib/setup/SeTconfig
#              This is post package installation, and during the configuration
#              routines.
# Purpose....: Configure the console= setting for the Linux Kernel
# Version....: 1.00
# Date.......: 22-Nov-2021
# Author.....: Stuart Winter <mozes@slackware.com>
###################################################################################
#
# Copyright 2021  Stuart Winter, Donostia, Spain.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# This is the code within /usr/lib/setup/SeTconfig:
# Here, we call each script in /var/log/setup. Two arguments are provided:
# 1 -- the target prefix (normally /, but ${T_PX} from the bootdisk)
# 2 -- the name of the root device.
#( cd $T_PX
#  if [ -x var/log/setup/$SCRIPT ]; then
#  ./var/log/setup/$SCRIPT $T_PX $ROOT_DEVICE
#  fi

# The U-Boot configuration file for the 'sysboot' feature.
# This is contained within /boot.
ARMEDSLACK_EXTLINUXCONF=extlinux/extlinux.conf

# Sanity checks:
[ ! -f /proc/device-tree/model ] && exit 0
# No extlinux config present?  This may be because we're on ARM32
# or the user's doing something custom.
[ ! -s ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF} ] && exit 0
# Don't offer this if you're not installing from the virtual console:
( tty | grep -Eq '^/dev/tty[0-9]' ) || exit 0

export DEVTYPE=$( strings /proc/device-tree/model 2>/dev/null )

# Determine whether there's an existing console setting.
# In AArch64 we tend not to ship with console= settings, but this
# may vary by Hardware Model.
# The user may have also pre-configured it.
# If there's a setting, exit silently.
egrep -q '(^|[^#])APPEND .*console=' ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF} && exit 0

dialog \
   --backtitle "Console Setting" \
   --defaultno \
   --title "CONFIGURE CONSOLE SETTINGS" --yesno \
"\nYou may now configure the console settings of your ${DEVTYPE}\n

\nIf you do not plan to use the UART ('Serial Console'), you may
wish to direct all Kernel and Operating System output to
the virtual consoles (HDMI monitor/LCD panel).
This setting precludes the use of the UART.\n
If you have no intention of using the UART, you should select 'Yes'.\n
\nIf you will use the UART for debugging/developing or remote
access, you should select 'No'.
This is the safest setting and enables console access on both the
video and UART, but the boot messages will be split between the
virtual console and UART.  Usually this isn't a problem for most
users.\n
\nThese settings can be changed later within the boot loader configuration: /boot/${ARMEDSLACK_EXTLINUXCONF}
\n\nRecommendation is 'No'" 22 80

[ $? = 1 ] && exit 0

# Set the console according to the Hardware Model:
case $DEVTYPE in
   "Pine64 RockPro64"*|"Pine64 Pinebook Pro"*)
      HWMCONSOLE=tty1 ;;
#   "Raspberry Pi 4"*)
#   HWMCONSOLE=tty1 ;;
# is it tty1?
esac

# If we have a console setting, configure it within the boot loader
# configuration:
[ ! -z "${HWMCONSOLE}" ] && \
   sed -i '/#/!s/APPEND.*/& console='"${HWMCONSOLE}"'/' ${T_PX}/boot/${ARMEDSLACK_EXTLINUXCONF}
