#!/bin/sh
#
# Copyright 2006-2008 VMware, Inc.  All rights reserved.
#
# Common VMware ESX constants and functions.
#

productName="VMware ESX"
buildType=release

vmware_etc_dir=/etc/vmware
vmware_lib_dir=/usr/lib/vmware
vmkmod_dir=$vmware_lib_dir/vmkmod
modloader=/usr/sbin/vmkload_mod
vmkdump_dir=/root
shutdowndir="$vmware_etc_dir/shutdown"
esxconf=$vmware_etc_dir/esx.conf

isTroubleMode() {
   grep trouble /proc/cmdline > /dev/null
}

isFirstRun() {
   ! grep -q '/cos/oneTimeRun = "1"'  $esxconf
}

setFirstRun() {
   echo '/cos/oneTimeRun = "1"' >> $esxconf
}

# Wrapper around vmk_loadmod to load modules
# Syntax 
#  do_module_file [-m alias] [-e] modulename [arguments to module loader]
do_module_file() {
    usage="do_module_file [-m alias] [-e] module [args]"

    modalias=""
    modexport=""
    OPTIND=1

    while getopts m:e arg 
    do
	case "$arg" in
	    "m") 
		modalias="$OPTARG";;
	    "e")
		modexport="-e";;
	    *) 	
		echo $usage
		exit 1;;
	esac
    done

    shift `expr $OPTIND - 1`

    if [ $# -lt 1 ]; then
	echo $usage
	exit 1
    fi

    module=$1

    # remove module name from parameter list
    shift 1

    # check if the module is loaded already.  If we have an alias
    # name, check for that instead of the module name.
    if [ ! -z $modalias ]; then
	check_for=$modalias
	# get into command line format
	$modalias="-m $modalias"
    else
	check_for=$module
    fi

    if [ `$modloader -b | grep -wc $check_for` -ne 0 ]; then
        action "   VMkernel module $check_for already loaded, skipping" /bin/true
        return
    fi 

    action "    Loading VMkernel module $module" \
	$modloader $modalias $modexport $vmkmod_dir/"$module" "$@"
}
