#! /bin/sh -e
 
. /usr/share/debconf/confmodule

LOG=/var/log/messages

# Pass the language/locale information needed by base-config into
# /target/root/dbootstrap_settings.
# This exists only to support installing woody (skolelinux, etc)

db_get debian-installer/locale
LOCALE="$RET"

db_get debian-installer/language
LANGLIST="$RET"

db_get localechooser/supported-locales
EXTRAS="$(echo "$RET" | sed 's/,//g')"

if [ ! -d /target/root ] ; then
    if mkdir /target/root ; then
        :
    else
        echo "error: unable to create /target/root"
    fi
fi

(
    echo "# Inserted by languagechooser."
    echo "LANG_INST=\"$LOCALE\""
    echo "LANGUAGE_INST=\"$LANGLIST\""
) >> /target/root/dbootstrap_settings

# Set global locale and language, and make sure the glibc locale is
# generated.  This task is also done in termwrap and base-config, and
# should probably be removed from there when this part work.
(
    echo "LANG=\"$LOCALE\""
    echo "LANGUAGE=\"$LANGLIST\""
) >> /target/etc/environment
# If the locale isn't already valid, append it to locale.gen
gen=
if chroot /target/ /usr/sbin/validlocale $LOCALE \
    >> /target/etc/locale.gen 2>> $LOG ; then
    : # Nothing to do
else
    # New locale added to locale.gen, generate it
    gen=1
fi
for loc in $EXTRAS; do
    if [ "$loc" = "$LOCALE" ]; then
        continue
    fi
    if chroot /target/ /usr/sbin/validlocale $loc \
        >> /target/etc/locale.gen 2>> $LOG ; then
        : # Nothing to do
    else
        # New locale added to locale.gen, generate it
        gen=1
    fi
done
if [ "$gen" ]; then
    chroot /target /usr/sbin/locale-gen --keep-existing >> $LOG 2>&1
fi
