#!/bin/sh

#     Copyright © 2005 Anton Zinoviev <anton@lml.bas.bg>

#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.

#     If you have not received a copy of the GNU General Public License
#     along with this program, write to the Free Software Foundation, Inc.,
#     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#                        ----------
#                         CONTENTS
#                        ----------
#
# 1. Initialise the variables $fontsets and $charmaps that list all
#    supported fontsets and charmaps.
#
# 2. Function available_fontsets - lists all fontfaces that can be
#    used with $CODESET.
#
# 3. Function available_fontsizes - lists all font sizes that can be
#    used with the $CODESET-$fontface combination.
#
# 4. Functions decode_charmap to decode 'Uni1' to 'Combined - Latin;
#    Slavic Cyrillic; Hebrew; basic Arabic' and encode_charmap for the
#    opposite conversion.
#
# 5. Set $locale.  Extract the strings for the chosen language in $kbdnames.
#
# 6. Compute default values for $CODESET, $FONTFACE, $FONTSIZE, based
#    on the the locale.
#
# 7. Overwrite (some of) the computed default values by the settings
#    in the configuration file (/etc/default/console-setup).
#
# 8. Compute default values for the Debconf questions.
#
# 9. Ask the Debconf questions starting from STATE=1.

set -e

. /usr/share/debconf/confmodule

CONFIGFILE=/etc/default/console-setup

default_codeset=''
default_fontface=''
CHARMAP=''
CODESET=''
FONTFACE=''
FONTSIZE=''


fontsets='Arabic-Fixed15
Arabic-Fixed16
Arabic-VGA14
Arabic-VGA16
Arabic-VGA28x16
Arabic-VGA32x16
Arabic-VGA8
Armenian-Fixed13
Armenian-Fixed14
Armenian-Fixed15
Armenian-Fixed16
Armenian-Fixed18
CyrAsia-Fixed13
CyrAsia-Fixed14
CyrAsia-Fixed15
CyrAsia-Fixed16
CyrAsia-Fixed18
CyrAsia-Terminus12x6
CyrAsia-Terminus14
CyrAsia-Terminus16
CyrAsia-Terminus18x10
CyrAsia-Terminus20x10
CyrAsia-Terminus22x11
CyrAsia-Terminus24x12
CyrAsia-Terminus28x14
CyrAsia-Terminus32x16
CyrAsia-TerminusBold14
CyrAsia-TerminusBold16
CyrAsia-TerminusBold18x10
CyrAsia-TerminusBold20x10
CyrAsia-TerminusBold22x11
CyrAsia-TerminusBold24x12
CyrAsia-TerminusBold28x14
CyrAsia-TerminusBold32x16
CyrAsia-TerminusBoldVGA14
CyrAsia-TerminusBoldVGA16
CyrKoi-Fixed13
CyrKoi-Fixed14
CyrKoi-Fixed15
CyrKoi-Fixed16
CyrKoi-Fixed18
CyrKoi-Terminus12x6
CyrKoi-Terminus14
CyrKoi-Terminus16
CyrKoi-Terminus18x10
CyrKoi-Terminus20x10
CyrKoi-Terminus22x11
CyrKoi-Terminus24x12
CyrKoi-Terminus28x14
CyrKoi-Terminus32x16
CyrKoi-TerminusBold14
CyrKoi-TerminusBold16
CyrKoi-TerminusBold18x10
CyrKoi-TerminusBold20x10
CyrKoi-TerminusBold22x11
CyrKoi-TerminusBold24x12
CyrKoi-TerminusBold28x14
CyrKoi-TerminusBold32x16
CyrKoi-TerminusBoldVGA14
CyrKoi-TerminusBoldVGA16
CyrKoi-VGA14
CyrKoi-VGA16
CyrKoi-VGA28x16
CyrKoi-VGA32x16
CyrKoi-VGA8
CyrSlav-Fixed13
CyrSlav-Fixed14
CyrSlav-Fixed15
CyrSlav-Fixed16
CyrSlav-Fixed18
CyrSlav-Terminus12x6
CyrSlav-Terminus14
CyrSlav-Terminus16
CyrSlav-Terminus18x10
CyrSlav-Terminus20x10
CyrSlav-Terminus22x11
CyrSlav-Terminus24x12
CyrSlav-Terminus28x14
CyrSlav-Terminus32x16
CyrSlav-TerminusBold14
CyrSlav-TerminusBold16
CyrSlav-TerminusBold18x10
CyrSlav-TerminusBold20x10
CyrSlav-TerminusBold22x11
CyrSlav-TerminusBold24x12
CyrSlav-TerminusBold28x14
CyrSlav-TerminusBold32x16
CyrSlav-TerminusBoldVGA14
CyrSlav-TerminusBoldVGA16
CyrSlav-VGA14
CyrSlav-VGA16
CyrSlav-VGA28x16
CyrSlav-VGA32x16
CyrSlav-VGA8
Ethiopian-Fixed15
Ethiopian-Fixed16
Ethiopian-Fixed18
Ethiopian-Goha12
Ethiopian-Goha14
Ethiopian-Goha16
Ethiopian-GohaClassic12
Ethiopian-GohaClassic14
Ethiopian-GohaClassic16
FullCyrAsia-Fixed13
FullCyrAsia-Fixed14
FullCyrAsia-Fixed15
FullCyrAsia-Fixed16
FullCyrAsia-Fixed18
FullCyrAsia-Terminus12x6
FullCyrAsia-Terminus14
FullCyrAsia-Terminus16
FullCyrAsia-Terminus18x10
FullCyrAsia-Terminus20x10
FullCyrAsia-Terminus22x11
FullCyrAsia-Terminus24x12
FullCyrAsia-Terminus28x14
FullCyrAsia-Terminus32x16
FullCyrAsia-TerminusBold14
FullCyrAsia-TerminusBold16
FullCyrAsia-TerminusBold18x10
FullCyrAsia-TerminusBold20x10
FullCyrAsia-TerminusBold22x11
FullCyrAsia-TerminusBold24x12
FullCyrAsia-TerminusBold28x14
FullCyrAsia-TerminusBold32x16
FullCyrAsia-TerminusBoldVGA14
FullCyrAsia-TerminusBoldVGA16
FullCyrSlav-Fixed13
FullCyrSlav-Fixed14
FullCyrSlav-Fixed15
FullCyrSlav-Fixed16
FullCyrSlav-Fixed18
FullCyrSlav-Terminus12x6
FullCyrSlav-Terminus14
FullCyrSlav-Terminus16
FullCyrSlav-Terminus18x10
FullCyrSlav-Terminus20x10
FullCyrSlav-Terminus22x11
FullCyrSlav-Terminus24x12
FullCyrSlav-Terminus28x14
FullCyrSlav-Terminus32x16
FullCyrSlav-TerminusBold14
FullCyrSlav-TerminusBold16
FullCyrSlav-TerminusBold18x10
FullCyrSlav-TerminusBold20x10
FullCyrSlav-TerminusBold22x11
FullCyrSlav-TerminusBold24x12
FullCyrSlav-TerminusBold28x14
FullCyrSlav-TerminusBold32x16
FullCyrSlav-TerminusBoldVGA14
FullCyrSlav-TerminusBoldVGA16
FullCyrSlav-VGA14
FullCyrSlav-VGA16
FullCyrSlav-VGA28x16
FullCyrSlav-VGA32x16
FullCyrSlav-VGA8
FullGreek-Fixed13
FullGreek-Fixed14
FullGreek-Fixed15
FullGreek-Fixed16
FullGreek-Fixed18
FullGreek-Terminus12x6
FullGreek-Terminus14
FullGreek-Terminus16
FullGreek-Terminus18x10
FullGreek-Terminus20x10
FullGreek-Terminus22x11
FullGreek-Terminus24x12
FullGreek-Terminus28x14
FullGreek-Terminus32x16
FullGreek-TerminusBold14
FullGreek-TerminusBold16
FullGreek-TerminusBold18x10
FullGreek-TerminusBold20x10
FullGreek-TerminusBold22x11
FullGreek-TerminusBold24x12
FullGreek-TerminusBold28x14
FullGreek-TerminusBold32x16
FullGreek-TerminusBoldVGA14
FullGreek-TerminusBoldVGA16
FullGreek-VGA14
FullGreek-VGA16
FullGreek-VGA28x16
FullGreek-VGA32x16
FullGreek-VGA8
Georgian-Fixed13
Georgian-Fixed14
Georgian-Fixed15
Georgian-Fixed16
Georgian-Fixed18
Greek-Fixed13
Greek-Fixed14
Greek-Fixed15
Greek-Fixed16
Greek-Fixed18
Greek-Terminus12x6
Greek-Terminus14
Greek-Terminus16
Greek-Terminus18x10
Greek-Terminus20x10
Greek-Terminus22x11
Greek-Terminus24x12
Greek-Terminus28x14
Greek-Terminus32x16
Greek-TerminusBold14
Greek-TerminusBold16
Greek-TerminusBold18x10
Greek-TerminusBold20x10
Greek-TerminusBold22x11
Greek-TerminusBold24x12
Greek-TerminusBold28x14
Greek-TerminusBold32x16
Greek-TerminusBoldVGA14
Greek-TerminusBoldVGA16
Greek-VGA14
Greek-VGA16
Greek-VGA28x16
Greek-VGA32x16
Greek-VGA8
Hebrew-Fixed13
Hebrew-Fixed14
Hebrew-Fixed15
Hebrew-Fixed16
Hebrew-Fixed18
Hebrew-Terminus12x6
Hebrew-Terminus14
Hebrew-Terminus16
Hebrew-Terminus18x10
Hebrew-Terminus20x10
Hebrew-Terminus22x11
Hebrew-Terminus24x12
Hebrew-Terminus28x14
Hebrew-Terminus32x16
Hebrew-TerminusBold14
Hebrew-TerminusBold16
Hebrew-TerminusBold18x10
Hebrew-TerminusBold20x10
Hebrew-TerminusBold22x11
Hebrew-TerminusBold24x12
Hebrew-TerminusBold28x14
Hebrew-TerminusBold32x16
Hebrew-TerminusBoldVGA14
Hebrew-TerminusBoldVGA16
Hebrew-VGA14
Hebrew-VGA16
Hebrew-VGA28x16
Hebrew-VGA32x16
Hebrew-VGA8
Lao-Fixed14
Lao-Fixed15
Lao-Fixed16
Lat15-Fixed13
Lat15-Fixed14
Lat15-Fixed15
Lat15-Fixed16
Lat15-Fixed18
Lat15-Terminus12x6
Lat15-Terminus14
Lat15-Terminus16
Lat15-Terminus18x10
Lat15-Terminus20x10
Lat15-Terminus22x11
Lat15-Terminus24x12
Lat15-Terminus28x14
Lat15-Terminus32x16
Lat15-TerminusBold14
Lat15-TerminusBold16
Lat15-TerminusBold18x10
Lat15-TerminusBold20x10
Lat15-TerminusBold22x11
Lat15-TerminusBold24x12
Lat15-TerminusBold28x14
Lat15-TerminusBold32x16
Lat15-TerminusBoldVGA14
Lat15-TerminusBoldVGA16
Lat15-VGA14
Lat15-VGA16
Lat15-VGA28x16
Lat15-VGA32x16
Lat15-VGA8
Lat2-Fixed13
Lat2-Fixed14
Lat2-Fixed15
Lat2-Fixed16
Lat2-Fixed18
Lat2-Terminus12x6
Lat2-Terminus14
Lat2-Terminus16
Lat2-Terminus18x10
Lat2-Terminus20x10
Lat2-Terminus22x11
Lat2-Terminus24x12
Lat2-Terminus28x14
Lat2-Terminus32x16
Lat2-TerminusBold14
Lat2-TerminusBold16
Lat2-TerminusBold18x10
Lat2-TerminusBold20x10
Lat2-TerminusBold22x11
Lat2-TerminusBold24x12
Lat2-TerminusBold28x14
Lat2-TerminusBold32x16
Lat2-TerminusBoldVGA14
Lat2-TerminusBoldVGA16
Lat2-VGA14
Lat2-VGA16
Lat2-VGA28x16
Lat2-VGA32x16
Lat2-VGA8
Lat38-Fixed13
Lat38-Fixed14
Lat38-Fixed15
Lat38-Fixed16
Lat38-Fixed18
Lat38-Terminus12x6
Lat38-Terminus14
Lat38-Terminus16
Lat38-Terminus18x10
Lat38-Terminus20x10
Lat38-Terminus22x11
Lat38-Terminus24x12
Lat38-Terminus28x14
Lat38-Terminus32x16
Lat38-TerminusBold14
Lat38-TerminusBold16
Lat38-TerminusBold18x10
Lat38-TerminusBold20x10
Lat38-TerminusBold22x11
Lat38-TerminusBold24x12
Lat38-TerminusBold28x14
Lat38-TerminusBold32x16
Lat38-TerminusBoldVGA14
Lat38-TerminusBoldVGA16
Lat38-VGA14
Lat38-VGA16
Lat38-VGA28x16
Lat38-VGA32x16
Lat38-VGA8
Lat7-Fixed13
Lat7-Fixed14
Lat7-Fixed15
Lat7-Fixed16
Lat7-Fixed18
Lat7-Terminus12x6
Lat7-Terminus14
Lat7-Terminus16
Lat7-Terminus18x10
Lat7-Terminus20x10
Lat7-Terminus22x11
Lat7-Terminus24x12
Lat7-Terminus28x14
Lat7-Terminus32x16
Lat7-TerminusBold14
Lat7-TerminusBold16
Lat7-TerminusBold18x10
Lat7-TerminusBold20x10
Lat7-TerminusBold22x11
Lat7-TerminusBold24x12
Lat7-TerminusBold28x14
Lat7-TerminusBold32x16
Lat7-TerminusBoldVGA14
Lat7-TerminusBoldVGA16
Lat7-VGA14
Lat7-VGA16
Lat7-VGA28x16
Lat7-VGA32x16
Lat7-VGA8
Thai-Fixed13
Thai-Fixed14
Thai-Fixed15
Thai-Fixed16
Thai-Fixed18
Uni1-Fixed15
Uni1-Fixed16
Uni1-VGA14
Uni1-VGA16
Uni1-VGA28x16
Uni1-VGA32x16
Uni1-VGA8
Uni2-Fixed13
Uni2-Fixed14
Uni2-Fixed15
Uni2-Fixed16
Uni2-Fixed18
Uni2-Terminus12x6
Uni2-Terminus14
Uni2-Terminus16
Uni2-Terminus18x10
Uni2-Terminus20x10
Uni2-Terminus22x11
Uni2-Terminus24x12
Uni2-Terminus28x14
Uni2-Terminus32x16
Uni2-TerminusBold14
Uni2-TerminusBold16
Uni2-TerminusBold18x10
Uni2-TerminusBold20x10
Uni2-TerminusBold22x11
Uni2-TerminusBold24x12
Uni2-TerminusBold28x14
Uni2-TerminusBold32x16
Uni2-TerminusBoldVGA14
Uni2-TerminusBoldVGA16
Uni2-VGA14
Uni2-VGA16
Uni2-VGA28x16
Uni2-VGA32x16
Uni2-VGA8
Uni3-Fixed13
Uni3-Fixed14
Uni3-Fixed15
Uni3-Fixed16
Uni3-Fixed18
Uni3-Terminus12x6
Uni3-Terminus14
Uni3-Terminus16
Uni3-Terminus18x10
Uni3-Terminus20x10
Uni3-Terminus22x11
Uni3-Terminus24x12
Uni3-Terminus28x14
Uni3-Terminus32x16
Uni3-TerminusBold14
Uni3-TerminusBold16
Uni3-TerminusBold18x10
Uni3-TerminusBold20x10
Uni3-TerminusBold22x11
Uni3-TerminusBold24x12
Uni3-TerminusBold28x14
Uni3-TerminusBold32x16
Uni3-TerminusBoldVGA14
Uni3-TerminusBoldVGA16
Vietnamese-Fixed13
Vietnamese-Fixed14
Vietnamese-Fixed15
Vietnamese-Fixed16
Vietnamese-Fixed18
Vietnamese-Terminus12x6
Vietnamese-Terminus14
Vietnamese-Terminus16
Vietnamese-Terminus18x10
Vietnamese-Terminus20x10
Vietnamese-Terminus22x11
Vietnamese-Terminus24x12
Vietnamese-Terminus28x14
Vietnamese-Terminus32x16
Vietnamese-TerminusBold14
Vietnamese-TerminusBold16
Vietnamese-TerminusBold18x10
Vietnamese-TerminusBold20x10
Vietnamese-TerminusBold22x11
Vietnamese-TerminusBold24x12
Vietnamese-TerminusBold28x14
Vietnamese-TerminusBold32x16
Vietnamese-TerminusBoldVGA14
Vietnamese-TerminusBoldVGA16
'
# fontsets='Arabic-Fixed15
# Arabic-Fixed16
# Arabic-VGA14
# ... 
# Vietnamese-Fixed18
# '

charmaps='ARMSCII-8
CP1251
CP1255
CP1256
GEORGIAN-ACADEMY
GEORGIAN-PS
IBM1133
ISIRI-3342
ISO-8859-1
ISO-8859-10
ISO-8859-11
ISO-8859-13
ISO-8859-14
ISO-8859-15
ISO-8859-16
ISO-8859-2
ISO-8859-3
ISO-8859-4
ISO-8859-5
ISO-8859-6
ISO-8859-7
ISO-8859-8
ISO-8859-9
KOI8-R
KOI8-U
TIS-620
VISCII
UTF-8'

# charmaps='ARMSCII-8
# CP1251
# CP1255
# ...
# UTF-8'

## CODESETS ##

codesets='guess*Guess optimal character set
Arabic*. Arabic
Armenian*# Armenian
CyrKoi*# Cyrillic - KOI8-R and KOI8-U
CyrAsia*# Cyrillic - non-Slavic languages
FullCyrAsia*. Cyrillic - non-Slavic languages (for blind users)
CyrSlav*# Cyrillic - Slavic languages (also Bosnian and Serbian Latin)
FullCyrSlav*. Cyrillic - Slavic languages (for blind users)
Ethiopian*. Ethiopic
Georgian*# Georgian
Greek*# Greek
FullGreek*. Greek (for blind users)
Hebrew*# Hebrew
Lao*# Lao
Lat15*# Latin1 and Latin5 - western Europe and Turkic languages
Lat2*# Latin2 - central Europe and Romanian
Lat38*# Latin3 and Latin8 - Chichewa; Esperanto; Irish; Maltese and Welsh
Lat7*# Latin7 - Lithuanian; Latvian; Maori and Marshallese
Vietnamese*. Latin - Vietnamese
Thai*# Thai
Uni1*. Combined - Latin; Slavic Cyrillic; Hebrew; basic Arabic
Uni2*. Combined - Latin; Slavic Cyrillic; Greek
Uni3*. Combined - Latin; Slavic and non-Slavic Cyrillic'

db_capb backup

which () {
    local IFS
    IFS=:
    for i in $PATH; do
	if [ -f "$i/$1" -a -x "$i/$1" ]; then
	    echo "$i/$1"
	    return 0
	fi
    done
    return 1
}

available_fontfaces () {
    local prefix
    case "$CODESET" in
        guess) prefix='[^-]*-' ;;
        *) prefix="$CODESET"- ;;
    esac
    echo "$fontsets" | sort | \
	grep "^$prefix" | \
	sed -e "s/^$prefix//" \
	    -e 's/[0-9][0-9]*x[0-9][0-9]*$//' \
	    -e 's/[0-9]*$//' | \
	sort -n | \
	uniq
}

available_fontsizes () {
    local prefix
    case "$CODESET" in
        guess) prefix='[^-]*-' ;;
        *) prefix="$CODESET"- ;;
    esac
    case "$fontface" in
        guess) prefix="$prefix[^0-9]*" ;;
        *) prefix="$prefix$fontface" ;; # this includes fontface=''
    esac
    echo "$fontsets" \
	| grep "^$prefix[0-9]" \
	| sed -e "s/^$prefix//" \
        | sort -n | uniq \
        | while read x; do
              decode_fontsize "$x"
          done \
}

db_metaget console-setup/framebuffer_only description
framebuffer_only="$RET"
decode_fontsize () {
    local a b fbonly
    case "$1" in
        8x*)
            echo 8x${1#*x}
            ;;
        *x8)
            echo 8x${1%x*}
            ;;
        *x*)
            a=${1#*x}
            b=${1%x*}
            if [ "$a" -lt "$b" ]; then
                echo ${a}x${b} "($framebuffer_only)"
            else
                echo ${b}x${a} "($framebuffer_only)"
            fi
            ;;
        *)
            echo 8x$1
            ;;
    esac
}

encode_fontsize () {
    echo $1 | sed -e 's/[\, ;].*//'
}

decode_codeset () {
    echo "$codesets" | \
	sed -e 's/^/_/' | \
	grep -F "_$**" | \
	sed -e 's/^_//' | \
	sed 's/.*[*]//'
}

encode_codeset () {
    echo "$codesets" | \
	grep -F "*$*" | \
	sed 's/[*].*//'
}

kernel=unknown
if which uname >/dev/null; then
    case "`uname`" in
        *Linux*) kernel=linux ;;
        *FreeBSD*) kernel=freebsd ;;
    esac
fi

if which locale 2>/dev/null >/dev/null; then
    eval `locale`
fi

if [ "$LC_CTYPE"  -a "$LC_CTYPE" != C ]; then
    locale=$LC_CTYPE
elif db_get debian-installer/locale && [ "$RET" ]; then
    locale="$RET"
else
    locale=C
fi

if [ "$locale" = C ]; then
    CHARMAP=ISO-8859-15
    charmap_priority=high
elif which locale 2>/dev/null >/dev/null; then
    CHARMAP=`locale charmap`
else
    CHARMAP=unknown
fi
if echo "$charmaps" | grep "$CHARMAP" >/dev/null; then
    charmap_priority=low
else
    CHARMAP=UTF-8
    charmap_priority=high
fi

case "$locale" in
    *KOI8*|*koi8*)
	CODESET=CyrKoi
	codeset_priority=low
	;;
    aa_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    af_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    am_*)
	CODESET=Ethiopian
	codeset_priority=low
	;;
    an_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    ar_*)
	CODESET=Arabic
	codeset_priority=low
	;;
    ast_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    az_*)
	CODESET=Uni3
	codeset_priority=low
	;;
    be_*)
	CODESET=CyrSlav
	codeset_priority=low
	;;
    bg_*)
	CODESET=CyrSlav
	codeset_priority=low
	;;
    bn_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    br_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    bs_*)
	CODESET=Uni3
	codeset_priority=low
	;;
    byn_*)
	CODESET=Ethiopian
	codeset_priority=low
	;;
    ca_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    cs_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    cy_*)
	CODESET=Lat38
	codeset_priority=low
	;;
    da_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    de_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    el_*)
	CODESET=Greek
	codeset_priority=low
	;;
    en_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    eo|eo.*|eo_*|eo\@*)
	CODESET=Lat38
	codeset_priority=low
	;;
    es_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    et_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    eu_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    fa_*)
	CODESET=Arabic
	codeset_priority=low
	;;
    fi_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    fo_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    fr_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    ga_*)
	CODESET=Lat38
	codeset_priority=low
	;;
    gd_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    gez_*)
	CODESET=Ethiopian
	codeset_priority=low
	;;
    gl_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    gu_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    gv_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    he_*)
	CODESET=Hebrew
	codeset_priority=low
	;;
    hi_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    hr_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    hu_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    hy_*)
	CODESET=Armenian
	codeset_priority=low
	;;
    id_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    is_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    it_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    iw_*)
	CODESET=Hebrew
	codeset_priority=low
	;;
    ja_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    ka_*)
	CODESET=Georgian
	codeset_priority=low
	;;
    kl_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    kn_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    ko_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    kw_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    lg_*)
	CODESET=Lat15 # TODO: This must be ISO-8859-10 (Latin6)
	codeset_priority=low
	;;
    lo_*)
	CODESET=Lao
	codeset_priority=low
	;;
    lt_*)
	CODESET=Lat7
	codeset_priority=low
	;;
    lv_*)
	CODESET=Lat7
	codeset_priority=low
	;;
    ky_*)
	CODESET=CyrAsia
	codeset_priority=low
	;;
    mi_*)
	CODESET=Lat7
	codeset_priority=low
	;;
    mk_*)
	CODESET=CyrSlav
	codeset_priority=low
	;;
    ml_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    mn_*)
	CODESET=CyrAsia
	codeset_priority=low
	;;
    mr_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    ms_*)
	CODESET=Lat15 # TODO: Is this correct? (Malay language)
	codeset_priority=low
	;;
    mt_*)
	CODESET=Lat38
	codeset_priority=low
	;;
    nb_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    ne_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    nl_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    nn_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    oc_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    om_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    pa_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    pl_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    pt_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    ro_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    ru_*)
	CODESET=CyrSlav
	codeset_priority=low
	;;
    se_*)
	CODESET=Uni1
	codeset_priority=low
	;;
    sid_*)
	CODESET=Lat15 # TODO: Is this correct? (Sidama language)
	codeset_priority=low
	;;
    sk_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    sl_*)
	CODESET=Lat2
	codeset_priority=low
	;;
    so_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    sq_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    sr_*)
	CODESET=CyrSlav
	codeset_priority=low
	;;
    st_*)
	CODESET=Lat15 # TODO: Is this correct? (Sotho language)
	codeset_priority=low
	;;
    sv_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    ta_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    te_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    tg_*)
	CODESET=CyrAsia
	codeset_priority=low
	;;
    th_*)
	CODESET=Thai
	codeset_priority=low
	;;
    ti_*)
	CODESET=Ethiopian
	codeset_priority=low
	;;
    tig_*)
	CODESET=Ethiopian
	codeset_priority=low
	;;
    tl_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    tr_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    tt_*)
	CODESET=Lat15 # TODO: Is this correct? (Tatar language)
	codeset_priority=low
	;;
    uk_*)
	CODESET=CyrSlav
	codeset_priority=low
	;;
    ur_*)
	CODESET=Arabic
	codeset_priority=low
	;;
    uz_*)
	CODESET=Lat15 # TODO: Is this correct? (Uzbek language)
	codeset_priority=low
	;;
    uz_*\@cyrillic)
	CODESET=CyrAsia
	codeset_priority=low
	;;
    vi_*)
	CODESET=Vietnamese
	codeset_priority=low
	;;
    wa_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    xh_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    yi_*)
	CODESET=Hebrew
	codeset_priority=low
	;;
    zh_*)
	CODESET=unsupported
	codeset_priority=low
	;;
    zu_*)
	CODESET=Lat15
	codeset_priority=low
	;;
    *)
	CODESET=guess
        if [ "$CHARMAP" = UTF-8 ]; then
	    codeset_priority=high
        else
            # for 8-bit encodings we can guess good codeset
	    codeset_priority=low
        fi
	;;
esac
if [ "$CODESET" = unsupported ]; then
    CODESET=guess
fi

case "$CODESET" in
    Arabic)
	FONTFACE=VGA
	;;
    Armenian)
	FONTFACE=Fixed
	;;
    CyrAsia)
	FONTFACE=Fixed
	;;
    CyrKoi)
	FONTFACE=VGA
	;;
    CyrSlav)
	FONTFACE=Fixed
	;;
    Ethiopian)
	FONTFACE=Goha
	;;
    Georgian)
	FONTFACE=Fixed
	;;
    Greek)
	FONTFACE=Fixed
	;;
    Hebrew)
	FONTFACE=Fixed
	;;
    Lao)
	FONTFACE=Fixed
	;;
    Lat15)
	FONTFACE=Fixed
	;;
    Lat2)
	FONTFACE=Fixed
	;;
    Lat38)
	FONTFACE=Fixed
	;;
    Lat7)
	FONTFACE=Fixed
	;;
    Thai)
	FONTFACE=Fixed
	;;
    Uni1)
	FONTFACE=VGA
	;;
    Uni2)
	FONTFACE=Fixed
	;;
    Uni3)
	FONTFACE=Fixed
	;;
    Vietnamese)
	FONTFACE=Fixed
	;;
    *)
	FONTFACE=Fixed
	;;
esac

if lsmod | grep -q speakup; then
    case "$CODESET" in
	CyrAsia|CyrSlav|Greek)
	    CODESET="Full$CODESET"
	    ;;
    esac
fi

FONTSIZE=16

### READ THE DEFAULTS ##################################################

# Load the config file, if it exists.
if [ -e $CONFIGFILE ]; then
    . $CONFIGFILE || true
fi

# Compute default values for the Debconf questions

if [ "$CODESET" ]; then
    default_codeset="$(decode_codeset "$CODESET")"
fi

if [ "$FONTFACE" = guess ]; then
    db_metaget console-setup/guess_font description
    default_fontface="$RET"
elif [ "$FONTFACE" ]; then
    default_fontface="$FONTFACE"
else
    db_metaget console-setup/use_system_font description
    default_fontface="$RET"
fi

if [ "$FONTSIZE" = guess -a "$FONTFACE" != guess ]; then
    # this combination is currently unsupported
    FONTSIZE=16
fi
if [ "$FONTFACE" ]; then
    FONTSIZE=${FONTSIZE:-16}
fi

# Store the default values into debconf db.  Workaround #352697.
db_get console-setup/store_defaults_in_debconf_db
if [ "$RET" = true ]; then
    db_set console-setup/charmap47 "$CHARMAP"
    db_set console-setup/codeset47 "$default_codeset"
    db_set console-setup/fontface47 "$default_fontface"
    fontsizetext="`decode_fontsize $FONTSIZE`"
    db_set console-setup/fontsize-text47 "$fontsizetext"
    db_set console-setup/fontsize-fb47 "$fontsizetext"
    db_set console-setup/fontsize "$fontsizetext"

    db_set console-setup/store_defaults_in_debconf_db false
fi

#######################################################################

STATE=1
old_state=0
while :; do
    starting_state=$STATE
    case "$STATE" in
	1)
            # add commas
	    choices=`echo "$charmaps" | sed -e 's/$/,/g' | sort `
	    # remove newlines
	    choices=`echo $choices | sed -e 's/,$//' `
	    db_subst console-setup/charmap47 CHOICES "$choices"
	    db_input $charmap_priority console-setup/charmap47 || true
	    if db_go; then
		STATE=$(($STATE + 1))
	    else
		STATE=$(($STATE - 1))
	    fi
	    ;;
	2)
	    db_input $codeset_priority console-setup/codeset47 || true
	    if db_go; then
		db_get console-setup/codeset47
		CODESET="$(encode_codeset "$RET")"
		db_set console-setup/codesetcode "$CODESET"
		STATE=$(($STATE + 1))
	    else
		STATE=$(($STATE - 1))
	    fi
	    ;;
	3)
	    fontfaces=`available_fontfaces`
	    # add commas
	    choices=`echo "$fontfaces" | sed -e 's/$/,/g'`
	    # remove newlines
	    choices=`echo $choices | sed -e 's/,$//'`
	    db_metaget console-setup/use_system_font description
	    choices="$choices, $RET"
	    db_metaget console-setup/guess_font description
	    choices="$choices, $RET"
	    db_subst console-setup/fontface47 CHOICES "$choices"
	    db_input medium console-setup/fontface47 || true
	    if db_go; then
		STATE=$(($STATE + 1))
	    else
		STATE=$(($STATE - 1))
	    fi
	    db_get console-setup/fontface47
	    fontface=$RET
	    db_metaget console-setup/use_system_font description
	    if [ "$fontface" = "$RET" ]; then
		fontface=''
	    fi
	    db_metaget console-setup/guess_font description
	    if [ "$fontface" = "$RET" ]; then
		fontface=guess
	    fi
	    ;;
	4)
            if [ "$kernel" = freebsd ]; then
	        db_set console-setup/fontsize "$FONTSIZE"
		# skip the question without making Debconf loop
		STATE=$(( $STATE + $STATE - $old_state ))
            else
	        fontsizes=`available_fontsizes`
	        # add commas
	        choices=`echo "$fontsizes" | sed -e 's/$/,/g' `
	        # remove newlines and the last comma
	        choices=`echo $choices | sed -e 's/,$//'`
	        if [ -n "$choices" -a "$FONTSIZE" != guess ]; then
		    if echo "$choices" | grep -q x; then
		        size_template=console-setup/fontsize-fb47
		    else
		        size_template=console-setup/fontsize-text47
		    fi
		    db_subst $size_template CHOICES "$choices"
		    db_input medium $size_template || true
		    if db_go; then
		        STATE=$(($STATE + 1))
		    else
		        STATE=$(($STATE - 1))
		    fi
		    db_get $size_template
		    FONTSIZE=`encode_fontsize "$RET"`
	        else
                    FONTSIZE=${choices:+guess}
		    STATE=$(( $STATE + $STATE - $old_state ))
                fi
	        db_set console-setup/fontsize "$FONTSIZE"
	    fi
	    ;;
	*)
	    break
	    ;;
    esac
    old_state=$starting_state
done

if [ $STATE -eq 0 ]; then
    exit 10
fi

exit 0
