##################################################################################
# File...: /root/.profile
# Status : Local Slackware ARM addition - not required for x86.
# Purpose: Within the Slackware Installer, display the initial setup instructions
#          when the user activates the local consoles.  By default /etc/{issue,motd}
#          will be displayed only over the serial console, but if the user doesn't
#          have a serial adaptor (only installing via the console), they'll never
#          see these initial setup instructions.
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 30-May-2021
##################################################################################
# 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.

export PATH=/usr/sbin:$PATH

# Only clear the screen if we're on a tty where we had to press ENTER
# to activate the console.
# This clearing of the screen reduces the confusion when a bunch of typically
# superfluous info suddenly becomes larger.
# There's no need to do this over a serial console though:
tty > /dev/null && clear

# Determine the Hardware Model:
export HWM=$( slk-hwm-discover )

# Set sensible fonts for each Hardware Model:
case "${HWM}" in
#   "Raspberry Pi 400"*)
#     setfont /usr/share/kbd/consolefonts/ter-v18n.psf.gz > /dev/null 2>&1
#    ;;
   "Parallels"*)
     setfont /usr/share/kbd/consolefonts/ter-122b.psf.gz > /dev/null 2>&1
     ;;
   "KVM"*|"QEMU"*|"Apple Virt"*|"VMware"*|"Parallels"*)
     setfont /usr/share/kbd/consolefonts/ter-728b.psf.gz > /dev/null 2>&1
    ;;
   *) setfont /usr/share/kbd/consolefonts/ter-732b.psf.gz > /dev/null 2>&1
    ;;
esac

# Display the instructions once whenever any of the local consoles are activated:
# "not a tty" is reported for either the virtual console OR serial console if no 'console=' is set
# (which is the standard for Slackware), so instead of we want to force the keyboard selector
# we'll use a Kernel cmdline token.
# This is used with the RPi Kernel fork on the RPi5 where we need to set
# console=serial0,115200 console=tty
# in order to receive output to the virtual console, and still want to start the keymap
# selector automatically.
# Note: on machines (such as the RPi5 with the RPi vendor fork Kernel), the only
# cmdline token that matches is the 'startkbselector' since the console is unset
# (tty reports "not a tty"). This means that the code below will pop up the keymap
# selector over ttyAMA0 if you login there *first*, but otherwise will pop it up on
# the virtual console as usual.
# This could be fixed if we match on every possible combination of serial port devices
# (or just /dev/ttyS[0-9]* or /dev/ttyAMA[0-9]* but it'll possibly break on others later
# which use different addresses). The fix is to login over virtual console first,
# as once set, will set a flag to prevent the selector from running again.
( tty | grep -Eq '^/dev/tty[0-9]' ) || ( grep -qw startkbselector /proc/cmdline ) && {

   # Offer to load a key map once on the local console:
   # (this code was copied from /etc/rc.d/rc.S)
   [ ! -f /.keymapset ] && {
      clear
      echo
      echo
      echo "<OPTION TO LOAD SUPPORT FOR NON-US KEYBOARD>"
      echo
      echo "If you are not using a US keyboard, you may now load a different"
      echo "keyboard map. To select a different keyboard map, please enter 1"
      echo "now. To continue using the US map, just hit enter."
      echo
      echo -n "Enter 1 to select a keyboard map: "
      read ONE
      if [ "$ONE" = "1" ]; then
         /usr/lib/setup/SeTkeymap
      fi
      clear
      # Set flag to bypass on any other local activations:
      touch /.keymapset ;} ;}

# For all shells, display the getting started information.
# For virtual consoles and the serial/UART we'll get the info anyway,
# but not for SSH. This way we have the info on all channels.
clear
cat /etc/{issue,motd}
