#!/bin/bash

# Usage instructions
Usage () {
   echo "Usage instructions: "
   echo "  $0 [--help|<vmx-path-name]"
   echo "  The command walks through the inventory and checks whether each virtual"
   echo "  machine in the inventory matches the set of rules on configuration "
   echo "  that is indicated in the rules file on the host: /etc/vmware/configrules"
   echo ""
   echo " Supported arguments: "
   echo "    --help : Prints usage instructions"
   echo "    <vmx-path-name> : Indicates a specific configuration file that should be checked"
}

# Check whether a particular config file is valid.
CheckConfig () {
   file="$1"
   if [ -e "$file" ]
   then
       /usr/lib/vmware/bin/configrulesTool checkvm "$file"
   else
       echo "Skipping virtual machine at $file since it is not accessible..."
   fi
}


# Walk the inventory and call CheckConfig on each entry
CheckInventory () {
    vmware-cmd -l | while read file; do
       CheckConfig "$file"
    done
}

# Argument parsing and dispatch
if [ -n "$1" ]
then
   if [ "$1" == "--help" ]
   then
       Usage
       exit 0
   else
       CheckConfig "$1"
       exit 0
   fi
else
   CheckInventory
fi