#!/bin/sh
#
# microcode_ctl:	Update Microcode for IA32
# chkconfig:		2345 01 99
# description:		Update Microcode for IA32 family processors
#
# $Id$

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

# Not supported in vserver
if is_yes "$VSERVER"; then
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/microcode_ctl ]; then
		msg_already_running "IA32 Microcode Update"
		return
	fi

	msg_starting "IA32 Microcode Update"
	_modprobe microcode
	daemon /usr/sbin/microcode_ctl -qf /lib/firmware/microcode.dat -d /dev/cpu/microcode
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/microcode_ctl
	rmmod microcode 2>/dev/null
}

stop() {
	if [ ! -f /var/lock/subsys/microcode_ctl ]; then
		msg_not_running "IA32 Microcode Update"
		return
	fi

	rm -f /var/lock/subsys/microcode_ctl
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload|force-reload)
	stop
	start
	;;
  status)
	if [ -f /var/lock/subsys/microcode_ctl ]; then
		nls "IA32 Microcode Update has been loaded"
		exit 0
	else
		msg_not_running "IA32 Microcode Update"
		exit 3
	fi
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit $RETVAL
