#!/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 ## Will be replaced by assignment for $fontsets:

# fontsets='Arabic-Fixed15
# Arabic-Fixed16
# Arabic-VGA14
# ... 
# Vietnamese-Fixed18
# '

## CHARMAPS ## Will be replaced by assignment for $charmaps:

# 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
CyrSlav*# Cyrillic - Slavic languages (also Bosnian and Serbian Latin)
Ethiopian*. Ethiopic
Georgian*# Georgian
Greek*# Greek
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

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" | \
	grep -F "$**" | \
	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 type 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 type 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=Ethiopic
	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

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
