#! /bin/sh
# A crude much-simplified clone of setupcon for use in the initramfs.

OPTION=FRAMEBUFFER
PREREQ="framebuffer"

prereqs () {
	echo "$PREREQ"
}

case $1 in
prereqs)
	prereqs
	exit 0
	;;
esac

[ -r /etc/default/console-setup ] || exit 0
. /etc/default/console-setup

[ "$ACTIVE_CONSOLES" ] || exit 0

if [ "$VERBOSE_OUTPUT" = yes ]; then
	verbose=
else
	verbose='>/dev/null 2>&1'
fi

for i in 1 2 3 4 5 6; do
	[ -c /dev/tty$i ] || mknod /dev/tty$i c 4 $i
done

for console in $ACTIVE_CONSOLES; do
	[ -w $console ] || continue

	if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM$CHARMAP" ]; then
		printf '\033%%G' >$console
	else
		printf '\033%%@' >$console
	fi

	if [ "$FONT" ]; then
		FONT="/etc/console-setup/${FONT##*/}"
		FONT="${FONT%.gz}"
	else
		FONT="/etc/console-setup/$CODESET-$FONTFACE$FONTSIZE.psf"
	fi
	if [ -f "$FONT" ]; then
		if type consolechars >/dev/null 2>&1; then
			eval consolechars -v --tty=$console -f "$FONT" $verbose
		elif type setfont >/dev/null 2>&1; then
			eval setfont -v -C $console "$FONT" $verbose
		fi
	fi

	if [ "$ACM" ]; then
		ACM="/etc/console-setup/${ACM##*/}"
		ACM="${ACM%.gz}"
	else
		ACM="/etc/console-setup/$CHARMAP.acm"
	fi
	if [ -f "$ACM" ]; then
		if type consolechars >/dev/null 2>&1; then
			eval consolechars -v --tty=$console --acm "$ACM" \
				$verbose
		elif type setfont >/dev/null 2>&1; then
			eval setfont -v -C "$console" -m "$ACM" $verbose
		fi
	fi

	if type kbd_mode >/dev/null 2>&1; then
		if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM" ]; then
			kbd_mode -u <$console
		else
			kbd_mode -a <$console
		fi
	fi
done

if [ -f /etc/console-setup/cached.kmap.gz ] && type loadkeys >/dev/null; then
	eval loadkeys /etc/console-setup/cached.kmap.gz $verbose
fi

exit 0
