#! /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

# CODESET
[ "$CODESET" != guess ] || CODESET=''
if [ -z "$CODESET" ]; then
    case "$CHARMAP" in
        UTF-8)            CODESET=Uni2;;
        ARMSCII-8)        CODESET=Armenian ;;
        CP1251)           CODESET=CyrSlav ;;
        CP1255)           CODESET=Hebrew ;;
        CP1256)           CODESET=Arabic ;;
        GEORGIAN-ACADEMY) CODESET=Georgian ;;
        GEORGIAN-PS)      CODESET=Georgian ;;
        IBM1133)          CODESET=Lao ;;
        ISIRI-3342)       CODESET=Arabic ;;
        ISO-8859-1)       CODESET=Lat15 ;;
        ISO-8859-2)       CODESET=Lat2 ;;
        ISO-8859-3)       CODESET=Lat38 ;;
        ISO-8859-4)       CODESET=Lat7 ;; # sometimes Lat15
        ISO-8859-5)       CODESET=CyrSlav ;;
        ISO-8859-6)       CODESET=Arabic ;;
        ISO-8859-7)       CODESET=Greek ;;
        ISO-8859-8)       CODESET=Hebrew ;;
        ISO-8859-9)       CODESET=Lat15 ;;
        ISO-8859-10)      CODESET=Lat15 ;;
        ISO-8859-11)      CODESET=Thai ;;
        ISO-8859-13)      CODESET=Lat7 ;;
        ISO-8859-14)      CODESET=Lat38 ;;
        ISO-8859-15)      CODESET=Lat15 ;;
        ISO-8859-16)      CODESET=Lat2 ;;
        KOI8-R)           CODESET=CyrKoi ;;
        KOI8-U)           CODESET=CyrKoi ;;
        TIS-620)          CODESET=Thai ;;
        VISCII)           CODESET=Vietnamese ;;
        *)	            ;;
    esac
fi

# FONTSIZE
if [ -z "$FONTSIZE" -o "$FONTSIZE" = guess ]; then
    FONTSIZE=16
fi
case "$FONTSIZE" in
    8x*)
        FONTSIZE=${FONTSIZE#*x}
        ;;
    *x8)
        FONTSIZE=${FONTSIZE%x*}
        ;;
    *x*)
        a=${FONTSIZE%x*}
        b=${FONTSIZE#*x}
        if [ "$a" -lt "$b" ]; then
            FONTSIZE=${b}x${a}
        fi
        ;;
esac

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

[ -c /dev/tty1 ] || mknod /dev/tty1 c 4 1

[ -w /dev/tty1 ] || continue

if [ "$CHARMAP" = UTF-8 ] || [ -z "$ACM$CHARMAP" ]; then
	printf '\033%%G' >/dev/tty1
else
	printf '\033%%@' >/dev/tty1
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" ] || [ -f "$FONT.gz" ]; then
	if type consolechars >/dev/null 2>&1; then
		eval consolechars -v --tty=/dev/tty1 -f "$FONT" $verbose
	elif type setfont >/dev/null 2>&1; then
		eval setfont -v -C /dev/tty1 "$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" ] || [ -f "$ACM.gz" ]; then
	if type consolechars >/dev/null 2>&1; then
		eval consolechars -v --tty=/dev/tty1 --acm "$ACM" \
			$verbose
	elif type setfont >/dev/null 2>&1; then
		eval setfont -v -C "/dev/tty1" -m "$ACM" $verbose
	fi
fi

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

if [ -f "/etc/console-setup/cached_${CHARMAP}_del.kmap.gz" ] && type loadkeys >/dev/null; then
	eval loadkeys "/etc/console-setup/cached_${CHARMAP}_del.kmap.gz" $verbose
fi

exit 0
