#!/bin/sh
#
# vmware-autoconfig: Autoconfig vmware
#
# chkconfig: 235 18 98
# description:  Checks if vmware is configured for the running kernel. \
#               If not, it will (try) to automatically configure vmware \
#               and build the vmware modules.
#
#
# Urs Beyerle, PSI
#

# definitions
RETVAL=0
declare -r   LOG_FILE=/var/log/vmware-autoconfig.log
declare -r   MY_KERNEL=$( uname -r )
declare -r   prog="vmware-autoconfig"

# vmware version
if test -x /usr/bin/vmware-modconfig ; then
  vers=10
elif test -x /usr/bin/vmware-config.pl ; then
  vers=1
else
  exit 1
fi

# source function library.
. /etc/rc.d/init.d/functions

success_and_exit() { 
    rm -f /etc/vmware/not_configured
    success
    echo
    exit 0
}

start() {

    echo -n $"Starting $prog: "

    # check if vmware is configured
    if [ ! -e /etc/vmware/not_configured ]; then
	if [ -e /lib/modules/${MY_KERNEL}/misc/vmmon.ko ] && \
           [ -e /lib/modules/${MY_KERNEL}/misc/vmnet.ko ]; then
	    success_and_exit
	fi
    fi
    
    # vmware not configured or no kernel modules found	
    if test $vers -ge 10 ; then
      /usr/bin/vmware-modconfig --console --install-all >$LOG_FILE 2>&1
      if [ "$?" = "0" ]; then
	/etc/init.d/vmware stop >>$LOG_FILE 2>&1
	success_and_exit
      else
        failure; echo; exit 1
      fi
    else
      /usr/bin/vmware-config.pl -default >$LOG_FILE 2>&1
      if [ "$?" = "0" ]; then
	/etc/init.d/vmware stop >>$LOG_FILE 2>&1
	success_and_exit
      else		
	failure; echo
	/usr/bin/vmware-config.pl -default -compile >>$LOG_FILE 2>&1
	if [ "$?" = "0" ]; then
	    /etc/init.d/vmware stop >>$LOG_FILE 2>&1
	    success_and_exit
	else
	    failure; echo; exit 1
	fi
      fi
    fi
}

stop() {
    /bin/true
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        ;;
    reload)
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|condrestart|reload}"
        ;;
esac
exit $RETVAL
