#!/bin/sh
#
# Lintian requires a UTF-8 locale in order to properly do man page tests.
# Generate one at installation time so that we're guaranteed to have one.

set -eu

locale_dir=/var/lib/lintian/locale

gen_locale() {
    echo 'Generating en_US.UTF-8 locale for internal Lintian use....'
    mkdir -p "$locale_dir"
    if ! localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias \
                --quiet "$locale_dir"/en_US.UTF-8 ; then
        rm -rf "$locale_dir"
        exit 1
    fi
}

if [ "$1" = "configure" ]; then
    if [ ! -f "$locale_dir/en_US.UTF-8/LC_CTYPE" ] &&
	[ -f /usr/share/locale/locale.alias ] &&
	[ ! -d /usr/lib/locale/C.UTF-8/ ] ; then

	# handle upgrades from the previous, incorrect, directory:
	rm -rf "$locale_dir"
	gen_locale
    elif [ -d "$locale_dir" -a -d /usr/lib/locale/C.UTF-8/ ] ; then
        # handle upgrades into a system with the libc C.UTF-8
        rm -rf "$locale_dir"
    fi
fi

if [ "$1" = "triggered" ]; then
    # Remove our locale directory in all cases:
    # If locales is removed, locales-all should provide us the locale
    # we want.
    # If locales is upgraded, we should still regenerate our locale.
    # If the libc provided C.UTF-8 appeared, we do not need our locale
    # anymore.
    rm -rf "$locale_dir"

    if [ -f /usr/share/locale/locale.alias ] &&
	[ ! -d /usr/lib/locale/C.UTF-8/ ] ; then

	gen_locale
    fi
fi

#DEBHELPER#
