# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2013, 2014, 2016 Synacor, Inc.
#
# 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,
# version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with this program.
# If not, see <https://www.gnu.org/licenses/>.
# ***** 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=$(/usr/sbin/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
}
