# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2004, 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 
#
# zmshutil is meant to be sourced by other scripts.  If run
# it will do nothing.  It will complain if it can't find
# local config program.
#

#
# Find and set local config variables.
#
zmsetvars() {
    if [ "x$1" = "x-f" ]; then
        shift;
    else
        if [ x${zmsetvars} = "xtrue" ]; then
            return
        fi
    fi
    
    zmlocalconfig="/opt/zimbra/bin/zmlocalconfig"
    if [ ! -x "${zmlocalconfig}" ]; then 
        echo Error: can not find zmlocalconfig program
        exit 1
    fi
    
    if ! eval `${zmlocalconfig} -q -m export`; then
        echo Error: executing: ${zmlocalconfig} -q -m export
        exit 1
    fi
    
    export zmsetvars='true'
}

#
# Check if a conditional expression is true.
#
zmassert() { 
    if [ $@ ]; then
    	return;
    fi
    echo Error: assertion "'$@'" failed && exit 1
}

#
# Get available system memory in KB.
#
zmsysmemkb() {
    if [ -f /proc/meminfo ]; then
        memkb=`awk '/^MemTotal.*kB$/ { print $2; }' /proc/meminfo`
    else
        memkb=$(sysctl hw.memsize 2>/dev/null | awk -F: '{ print $2 / 1024; }');
    fi
    if ! echo $memkb | grep '^[0-9]*$' > /dev/null; then
        memkb=524288
    fi
    echo $memkb
}
