#!/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.
# 

# 
# Common variables and functions for VAMI management
# 

export VAMI_BIN=/opt/vmware/bin:/opt/vmware/share/vami
export PATH=$VAMI_BIN:$PATH

#
# Print a string to stderr
#
stderr()
{
    /bin/echo -e "$*" 1>&2
}

#
# general logging function
#
# $1 == log level:
#   emerg       system is unusable
#   alert       action must be taken immediately
#   crit        critical conditions
#   err         error conditions
#   warning     warning conditions
#   notice      normal, but significant, condition
#   info        informational message
#   debug       debug-level message
# 
# $2-* == any additional arguments to logger, and the message
#
vami_log()
{
    level=$1
    shift
    #
    # ${BASH_LINENO[0]} works in later versions of bash, but not in the
    # initial Debian version in the appliance (2.05b). 
    #
    # This is the proper way to print out the line number of the script
    # that called us.
    #
    ver=`expr "$BASH_VERSION" : "\(.\)"`
    if [ $ver -le 2 ]
    then
        logger -p local7.$level -- `basename $0`: $*
    else
        logger -p local7.$level -- `basename $0`, line ${BASH_LINENO[0]}: $*
    fi
}

#
# checks to see if a file can be accessed in the ways desired:
#    -r    readable
#    -w    writable
#    -x    executable (or searchable, for directories
#    -f    existence
#
# $1 == modes
# $2 == filename
#
# Returns 0 if successful, non-zero if access is not permitted
#
vami_check_access()
{
    export TEXTDOMAINDIR=/opt/vmware/lib/locale
    export TEXTDOMAIN=vami_common

    #
    # $* is used instead of $1, so if someone passes in '-r -w filename'
    # it will still work. 
    #
    if vami_access $*
    then
        return 0
    else
        vami_log alert Inaccessible file: vami_access $* failed.
        stderr `gettext "Inaccessible file:"` vami_access $* `gettext "failed."`
        return 1
    fi
}

#
# Return the supported locales as a string of space-separated names.
#
# For the time being, hard code the list of supported locale names. In the
# future, we should include the php-cli package in the base, and write
# interface routines to parse the php messages file. This would also
# allow shell scripts to be localized, if we wanted to do that.
#
# This function, for example, could be implemented in php by:
#
#  <?php
#  include 'vami_common.php';
#  foreach ($messages as $key => $l)
#  {
#    printf("%s ", $key);
#  }
#  ?>
#
supported_locales()
{
    echo en ja
}
supported_locale_descriptions()
{
    echo -e en\\t\\t for English
    echo -e ja\\t\\t for Japanese
}
locale_supported()
{
    l=$1
    set -- `supported_locales`
    for i
    do
        if [ "$i" = "$l" ]
        then
            return 0
        fi
    done
    return 1
}              


#
# Update /etc/issue with the current network configuration
#
update_etc_issue()
{
    #we don't update etc issue anymore this is a dummy function
    # it is being called from other scripts        
    return
}

