#!/bin/bash
#
# chkconfig: 34 96 4
# description: card check on rx200 S8

### BEGIN INIT INFO
# Provides: omgcheckhw
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Should-Start:
# Should-Stop:
# Short-Description: checks for QuadE1-Card on RX200 S8
# Description: omgcheckhw
### 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
}

start () {
	hwvendor=$(/usr/bin/omgsysteminfo --filter=hw.host.vendor --val)
	hwproduct=$(/usr/bin/omgsysteminfo --filter=hw.host.product --val)
	if [ "$hwproduct" == "PRIMERGY RX200 S8" ] ; then
		touserInfo "RX200 S8 found"
		licinfo -d | grep -q "OPL4"
		hwrequired=$?
		if [ $hwrequired -eq 0 ] ; then
			touserInfo "card is needed"
			lspci -n | grep -q "1011:f004"
			cardfound=$?
			if [ $cardfound -eq 0 ] ; then
				touserOk "card found"
				rm -f /data/rebootdone
			else
				touserWarn "card not found"
				if [ -e /data/rebootdone ] ; then
					touserFail "reboot already done; something wrong with card"
				else
					touserWarn "rebooting system"
					touch /data/rebootdone
					reboot
				fi
			fi
		fi
	else
		echo "was anderes"
		/sbin/chkconfig omgcheckhw off
	fi
}

stop () {
    echo ""
}

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