#!/bin/sh

# This script can be installed to the same directory where the script
# that starts plymouth is installed, because it will start after it. It
# is, however, much better for performance to install in the directory
# that contains the first scripts to be started, usually 'init-top'.
# Installation options:
#
#   1. As a distro/vendor script, put it in directory:
#     (a) '/usr/share/initramfs-tools/scripts/init-top' -- faster boot;
#
#     (b) '/usr/share/initramfs-tools/scripts/init-premount' -- slower boot;
#
#   2. As an admin script, put it in directory:
#     (a) '/etc/initramfs-tools/scripts/init-top' -- faster boot;
#
#     (b) '/etc/initramfs-tools/scripts/init-premount' -- slower boot;

PREREQ="udev keymap plymouth"

prereqs()
{
  echo "$PREREQ"
}

case $1 in
  # get pre-requisites
  prereqs)
    prereqs
    exit 0
    ;;
esac


# Import the script helper functions
. /scripts/functions
_log_msg "Begin: Processing custom console font ...\n"

# Any error messages stored in this file are available after boot
FONT_LOG=/run/initramfs/font.log


# Set the console font from a custom font file.
# Env. var. 'FONT_FILE' is expected to contain the filename.
set_console_font()
{
  log_begin_msg "Setting font to '${FONT_FILE}'"
  if setfont "$FONT_FILE" >>${FONT_LOG} 2>&1; then
    log_end_msg
  else
    log_failure_msg "Unable to set font!"
  fi
}


# Find the name of the most recent custom font file installed
# in the initramfs and load it if possible.
FONT_FILE=$(ls -1t /etc/console-setup/cached_*.psf /etc/console-setup/cached_*.psf.gz 2>/dev/null | head -n 1)

if [ -x /bin/setfont ] && [ -n "$FONT_FILE" ] && [ -r "$FONT_FILE" ]; then
  # Plymouth controls the display of the graphical boot splash.
  # When the splash is showing, it is impossible to set the font.
  if [ -x /bin/plymouth ] && plymouth --ping; then
    _log_msg "Plymouth daemon is running in the background.\n"
    plymouth hide-splash >>${FONT_LOG} 2>&1 || echo "rc(plymouth hide-splash)=$?" >>${FONT_LOG} 2>&1
    set_console_font
    plymouth show-splash >>${FONT_LOG} 2>&1 || echo "rc(plymouth show-splash)=$?" >>${FONT_LOG} 2>&1
  else
    _log_msg "Plymouth daemon is not running in the background.\n"
    set_console_font
  fi
fi


log_end_msg
