#!/bin/sh

set -e

PREREQ=""

prereqs()
{
	echo "${PREREQ}"
}

case "${1}" in
	prereqs)
		prereqs
		exit 0
		;;
esac

. /usr/share/initramfs-tools/hook-functions

THEME="$(/usr/sbin/plymouth-set-default-theme || true)"
THEMES="/usr/share/plymouth/themes"

if [ -n "${THEME}" ]
then
	THEME_NAME="${THEME}"
	THEME="${THEMES}/${THEME}/${THEME}.plymouth"
else
	exit 0
fi

PLUGIN_PATH="$(plymouth --get-splash-plugin-path)"

case "${THEME_NAME}" in
	text|details|tribar)
		PLUGINS="text.so details.so"
		;;

	*)
		PLUGINS="text.so details.so label.so"
		;;
esac

MODULE="${PLUGIN_PATH}/$(sed -n 's/^ModuleName=\(.*\)/\1/p' ${THEME}).so"

if [ ! -e "$MODULE" ]
then
	echo "W: plymouth module ($MODULE) missing, skipping plymouth."
	exit 0
fi

# copy plugin and images for current theme
copy_exec "${MODULE}"
mkdir -p "${DESTDIR}/${THEMES}"
cp -r "${THEMES}/${THEME_NAME}" "${DESTDIR}/${THEMES}"

# copy binaries and base plugins
copy_exec /bin/plymouth
copy_exec /sbin/plymouthd

for PLUGIN in ${PLUGINS}
do
	if [ -f ${PLUGIN_PATH}/${PLUGIN} ]
	then
		copy_exec ${PLUGIN_PATH}/${PLUGIN}
	else
		echo "W: plymouth: The plugin ${PLUGIN} is missing, the selected theme might not work as expected."
		echo "W: plymouth: You might want to install the plymouth-themes package to fix this."
	fi
done

# copy base themes and logo
cp -a "${THEMES}/details" "${DESTDIR}/${THEMES}"
cp -a "${THEMES}/text" "${DESTDIR}/${THEMES}"

if [ -f /etc/os-release ]
then
	cp /etc/os-release "${DESTDIR}/etc"
fi

case "${THEME_NAME}" in
	text|details)

		;;

	*)
		cp /usr/share/plymouth/debian-logo.png "${DESTDIR}/usr/share/plymouth"

		# fontconfig
		mkdir -p "${DESTDIR}/etc/fonts/conf.d"
		cp -a /etc/fonts/fonts.conf "${DESTDIR}/etc/fonts"
		cp -rL /etc/fonts/conf.d/60-latin.conf "${DESTDIR}/etc/fonts/conf.d"
		mkdir -p "${DESTDIR}/var/cache/fontconfig"

		# fonts-dejavu
		if [ -e /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf ]
		then
			# jessie
			mkdir -p "${DESTDIR}/usr/share/fonts/truetype/dejavu"
			cp -a /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf "${DESTDIR}/usr/share/fonts/truetype/dejavu"
			cp -a /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf  "${DESTDIR}/usr/share/fonts/truetype/dejavu"
		else
			# wheezy
			mkdir -p "${DESTDIR}/usr/share/fonts/truetype/ttf-dejavu"
			cp -a /usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf "${DESTDIR}/usr/share/fonts/truetype/ttf-dejavu"
			cp -a /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf  "${DESTDIR}/usr/share/fonts/truetype/ttf-dejavu"
		fi

		# pango
		if ls /usr/lib/arm-linux-gnueabihf/pango/1* > /dev/null 2>&1
		then
			# wheezy
			PANGO_VERSION="$(ls /usr/lib/arm-linux-gnueabihf/pango)"

			mkdir -p "${DESTDIR}/usr/lib/arm-linux-gnueabihf/pango/${PANGO_VERSION}/module-files.d"
			mkdir -p "${DESTDIR}/usr/lib/arm-linux-gnueabihf/pango/${PANGO_VERSION}/modules"
			cp /usr/lib/arm-linux-gnueabihf/pango/${PANGO_VERSION}/module-files.d/libpango1.0-0.modules ${DESTDIR}/usr/lib/arm-linux-gnueabihf/pango/${PANGO_VERSION}/module-files.d/
			copy_exec /usr/lib/arm-linux-gnueabihf/pango/${PANGO_VERSION}/modules/pango-basic-fc.so
		else
			# jessie
			copy_exec /usr/lib/arm-linux-gnueabihf/libpango-1.0.so.0
		fi

		# add drm modules
		copy_modules_dir kernel/drivers/gpu/drm mga r128 savage sis tdfx via
		;;
esac

# copy renderers
copy_exec /usr/lib/arm-linux-gnueabihf/plymouth/renderers/frame-buffer.so
copy_exec /usr/lib/arm-linux-gnueabihf/plymouth/renderers/drm.so

# copy config files
mkdir -p "${DESTDIR}/etc/plymouth"

if [ -r /etc/plymouth/plymouthd.conf ]
then
	cp -a /etc/plymouth/plymouthd.conf "${DESTDIR}/etc/plymouth/"
fi

cp -a /usr/share/plymouth/plymouthd.defaults "${DESTDIR}/usr/share/plymouth/"

# temporarily include dummy root account lookup (#691598)
if ! grep -qs '^root:' "${DESTDIR}/etc/passwd"
then
	echo "root:x:0:0:root:/root:/bin/sh" >> "${DESTDIR}/etc/passwd"
fi

if ! grep -qs '^passwd: files' "${DESTDIR}/etc/nsswitch.conf"
then
	echo "passwd: files" >> "${DESTDIR}/etc/nsswitch.conf"
fi

for _LIBRARY in /lib/arm-linux-gnueabihf/libnss_files*
do
	if [ -e "${_LIBRARY}" ]
	then
		copy_exec ${_LIBRARY} /lib
	fi
done
