#!/bin/sh

# Copyright 2018 Harald Sitter <sitter@kde.org>
#
# 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) version 3 or any later version
# accepted by the membership of KDE e.V. (or its successor approved
# by the membership of KDE e.V.), which shall act as a proxy
# defined in Section 14 of version 3 of the license.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# calamares currently has subpar l10n capabilites.
# this helper is ment to aid in getting very primitive language support
# of the ground by using a helper from ubuntu's language-selector-common
# to retrieve a list of packages required for the chosen system language
# and then manually installig it.
# https://github.com/calamares/calamares/issues/924

set -ex

. /etc/default/locale

echo $LANG
echo $LC_TIME
without_ext=$(echo $LANG | cut -d. -f1)

# Calamares technically should mangle locale.gen, but it's broken
# https://github.com/calamares/calamares/issues/940
# To unbreak this call locale-gen manually with LC_TIME. This may be the same
# as LANG, which gets set up by lang-pack-en-base
# (installed by check-language-support).
# But since the pack may be any language and network might not be available
# we set up both the LANG and LC_TIME so that both are definitely set up
# regardless of network availablity or calamares bugs.
# NB: this is a special way of calling it unique to Ubuntu which will mangle
#   the config and enable it in one go
/usr/sbin/locale-gen --keep-existing "$LANG"
/usr/sbin/locale-gen --keep-existing "$LC_TIME"

apt-get update || true
missing=$(check-language-support --language="$without_ext")
apt-get install -y $missing || true
