#!/bin/bash
### BEGIN INIT INFO
# Provides:            plymouth
# Required-Start:      udev
# Should-Start:
# Required-Stop:
# X-Start-Before:      uevent
# Should-Stop:
# Default-Start:       3 4 5
# Default-Stop:
# Short-Description:   Show Plymouth Boot Screen
### END INIT INFO

. /.initrd/initenv
. /etc/init.d/template
. initrd-sh-functions

start() {
	[ -z "${NOSPLASH-}" ] && [ -z "${RDSHELL-}" ] ||
		return 0

	# first trigger graphics subsystem
	udevadm trigger --action=add --attr-match=class=0x030000 > /dev/null 2>&1
	# first trigger graphics and tty subsystem
	udevadm trigger --action=add \
		--subsystem-match=graphics \
		--subsystem-match=drm \
		--subsystem-match=tty \
		--subsystem-match=acpi \
		> /dev/null 2>&1

	udevadm settle --timeout=60 2>&1

	local pidfile=/run/plymouth/pid

	mkdir -m 0755 /run/plymouth 2>/dev/null ||:
	: > /run/systemd/plymouth

	start_daemon --lockfile "$LOCKFILE" -- plymouthd --mode=boot --tty=/dev/tty1 --pid-file=$pidfile ||
		RETVAL=$?

	[ "$RETVAL" = 0 ] ||
		return $RETVAL

	pid="$(cat "$pidfile")"

	while [ -e "/proc/$pid" ] && ! plymouth --ping 2>/dev/null; do
		sleep 1
	done

	omit_pid "$pid"
	plymouth show-splash
}

stop() {
	[ -z "${NOSPLASH-}" ] ||
		return 0

	plymouth --newroot="$rootmnt" ||
		RETVAL=$?

	return $RETVAL
}

switch "${1-}"
