#!/usr/bin/env bash
# 
# Copyright 2008-2011 VMware, Inc.  All rights reserved. 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 

# 
# Set the locale of the appliance.
#
# Currently this is only used to control the language used in the
# management GUI.
#
VAMI_BIN=/opt/vmware/share/vami
CONF_FILE=/opt/vmware/etc/appliance_config
. $VAMI_BIN/vami_common

export TEXTDOMAINDIR=/opt/vmware/lib/locale
export TEXTDOMAIN=vami_set_locale


update_config()
{
    if vami_check_access -rw $CONF_FILE
    then
        tfile=/tmp/`basename $CONF_FILE`.orig

        cp $CONF_FILE $tfile
        sed 's/\(^export APPLIANCE_LOCALE=\).*/\1'$1/ $tfile > $CONF_FILE
    else
        exit 1
    fi
}


#
# if we were given an argument, see if it's a locale we know about. If
# it is, use it
#
loc=$1

if [ "$loc" = "" ]
then
    echo
    echo `gettext "Please set the language for this appliance\'s management interface."`
    echo `gettext "Choices for language are:"`
    echo 
    supported_locale_descriptions
    echo
    echo -e '(en) ? \c'

    read loc
    if [ "$loc" = "" ]
    then
        loc="en"
    fi
fi

if locale_supported "$loc"
then
    update_config "$loc"
    exit 0
else
    echo `gettext "Unknown locale"` \""$loc"\"
    exit 1
fi
