#!/bin/bash
#
# chkconfig: 34 96 4
# description: prepare omgbridge

### BEGIN INIT INFO
# Provides: omgserverview
# Required-Start: $network $local_fs $remote_fs 
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop networkbridge
# Description: enable Fujitsu ServerView
### END INIT INFO

echo "$0 start: $(date)" >> /tmp/sequence

#
# some inits
#

# color => new RH6.0 bootup
# verbose => old-style bootup
# anything else => new style bootup without ANSI colors or positioning
BOOTUP=color
# column to start "[  OK  ]" label in 
RES_COL=60
# terminal sequence to move to that column. You could change this
# to something like "tput hpa ${RES_COL}" if your terminal supports it
MOVE_TO_COL="echo -en \\033[${RES_COL}G"
# terminal sequence to set color to a 'success' color (currently: green)
SETCOLOR_SUCCESS="echo -en \\033[0;32m"
# terminal sequence to set color to a 'failure' color (currently: red)
SETCOLOR_FAILURE="echo -en \\033[0;31m"
# terminal sequence to set color to a 'warning' color (currently: yellow)
SETCOLOR_WARNING="echo -en \\033[0;33m"
# terminal sequence to reset to the default color.
SETCOLOR_NORMAL="echo -en \\033[0;39m"

echo_success() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $"  OK  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 0
}
echo_failure() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo -n $"FAILED"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}

echo_info() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $" INFO "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}

echo_warning() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo -n $" WARN "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}


touser () {
    echo "$@"
}
touserOk (){
    echo -n "$@"
	echo_success
	echo
}
touserFail (){
    echo -n "$@"
	echo_failure
	echo
}
touserWarn (){
    echo -n "$@"
	echo_warning
	echo
}
touserInfo (){
    echo -n "$@"
	echo_info
	echo
}

svstatus () {
    /sbin/chkconfig --list eecd
    /sbin/chkconfig --list eecd_mods_src
    /sbin/chkconfig --list ipmievd
    /sbin/chkconfig --list srvmagt
    /sbin/chkconfig --list srvmagt_scs
}


svenable () {
	/sbin/chkconfig eecd on
	/sbin/chkconfig eecd_mods_src on
	/sbin/chkconfig ipmievd on
	/sbin/chkconfig srvmagt on
	/sbin/chkconfig srvmagt_scs on
}

svdisable () {
	/sbin/chkconfig eecd off
	/sbin/chkconfig eecd_mods_src off
	/sbin/chkconfig ipmievd off
	/sbin/chkconfig srvmagt off
	/sbin/chkconfig srvmagt_scs off
}


start () {
#	hwvendor=$(/usr/bin/omgsysteminfo --filter=hw.host.vendor --val)
#	hwproduct=$(/usr/bin/omgsysteminfo --filter=hw.host.product --val)
        sernumstring=$(/usr/bin/i2cinfo -l | grep  -e "OPL\|OPEXT" | head -n 1 | cut -d "," -f 5)
        if [ -z $sernumstring ]; then
                let sernum=0
        else
                let sernum=$sernumstring
        fi
#	if [ "$hwproduct" == "PRIMERGY RX1330 M1" ] ; then
	#Enable serverview on all QuadPri with serial > 500
	if (( ${sernum} >= 500 )) ; then
		echo "is Fujitsu RX1330 M1 enable ServerView"
		svenable
		/sbin/chkconfig omgserverview off
		/usr/bin/omgsetwebui old
	else
		echo "disable ServerView"
		svdisable
		/sbin/chkconfig omgserverview off
		/usr/bin/omgsetwebui new
	fi
}

stop () {
    echo ""
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	svstatus
	;;
    restart|force-reload)
	stop
	start
	;;
	enable)
	svenable
	;;
	disable)
	svdisable
	;;
    try-restart|condrestart)
	if status $prog > /dev/null; then
	    stop
	    start
	fi
	;;
    reload)
	exit 3
	;;
    *)
	echo $"Usage: $0 {start|stop|status|enable|disable}"
	exit 2
esac
