#!/bin/sh
# postinst script for chillcraftos-splash

set -e

update_cmdline() {
  local cmdline="$1"
  [ -e "$cmdline" ] || return

  sed -i "$cmdline" \
    -e '/\bquiet\b/! s/$/ quiet/' \
    -e '/\bsplash\b/! s/$/ splash/' \
    -e '/plymouth.ignore-serial-consoles/! s/$/ plymouth.ignore-serial-consoles/'
}

case "$1" in
  configure)
    if [ -z "$2" ] || grep -qw splash /proc/cmdline; then
      command -v plymouth-set-default-theme >/dev/null && plymouth-set-default-theme -R chillcraftos
      if systemctl get-default | grep -q graphical; then
        FW_LOC="$(/usr/lib/raspberrypi-sys-mods/get_fw_loc)"
        CMDLINE="$FW_LOC/cmdline.txt"
        update_cmdline "$CMDLINE"
      fi
    fi
    ;;
  abort-upgrade|abort-remove|abort-deconfigure)
    ;;
  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
    ;;
esac

exit 0
