#!/bin/sh

set -eu

puavo_locale=$(puavo-conf puavo.l10n.locale)

{
  if [ -n "$puavo_locale" ]; then
    puavo_language_short_form=$(echo "$puavo_locale" | cut -c 1-2)
    echo "LANG=${puavo_locale}"
    echo "LANGUAGE=${puavo_language_short_form}"

    puavo_dconf_profile="user-${puavo_language_short_form}"
    if [ -e "/etc/dconf/profile/${puavo_dconf_profile}" ]; then
      echo "DCONF_PROFILE=${puavo_dconf_profile}"
    fi
  fi

  # XXX Remove this once we drop support for our old event/xrandr-based
  # XXX display configuration system.  We do not want gnome-shell/mutter
  # XXX to mess up xrandr-settings in login screen, when this old system
  # XXX is used.
  if [ "$(puavo-conf puavo.graphics.display_server)" = 'xorg' \
       -a '(' -n "$(puavo-conf puavo.xrandr.args | jq -r '.[]')" \
              -o -n "$(puavo-conf puavo.xrandr.apply_presets)" ')' \
       -a "$(jq '.monitors_xml != null' /etc/puavo/device.json)" != 'true' ]; then
    echo MUTTER_ALLOW_CONFIGURATION=temporary
  fi
} > /etc/default/locale.tmp

if [ -s /etc/default/locale.tmp ]; then
  mv /etc/default/locale.tmp /etc/default/locale
else
  rm -f /etc/default/locale.tmp
fi
