#!/bin/sh -e
#
# Helper script to update system menus.
#
# Copyright (C) 2008  Alexey Tourbin <at@altlinux.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) any later version.

# Run as user?
if [ "${UID:-`id -u`}" -gt 0 ]; then
	exec /usr/bin/update-menus "$@"
fi

# Update /etc/menu-methods/lang.h.
if [ ! -s /etc/menu-methods/lang.h ] ||
   [ /etc/sysconfig/i18n -nt /etc/menu-methods/lang.h ]; then

	# Default language.
	unset LC_ALL LC_MESSAGES LANG LANGUAGE SUPPORTED
	if [ -s /etc/sysconfig/i18n ]; then
		. /etc/sysconfig/i18n
	fi
	lang=${LC_ALL:-${LC_MESSAGES:-${LANG:-"C"}}}

	# Supported langugages.
	langs=${SUPPORTED:-${LANGUAGE}}
	[ -n "$langs" ] || langs=`rpm --eval %_install_langs`
	if [ "$langs" = "all" ]; then
		langs=$(if cd /usr/share/locale &&
			   export LC_ALL=C &&
			   set -- */LC_MESSAGES/menu-messages.mo &&
			   [ -f "$1" ]; then
				colon=
				for l; do
					l=${l%%/*}
					echo -n "$colon$l"
					colon=:
				done
			else
				echo "$lang"
			fi)
	fi

	# Write /etc/menu-methods/lang.h.
	cat >/etc/menu-methods/lang.h <<__EOF__
# Generated by $0.
function lang()="$lang"
function languages()="$langs"
__EOF__

fi

# Update menus.
exec /usr/bin/update-menus "$@"
