#!/bin/bash
#
# (C) Copyright 2002-2011 Hewlett-Packard Development Company, L.P.
#
# init file for hp Rack agent
#
# processname: cmarackd
NAME="Rack Infrastructure Info Srv"
PNAME="cpqriisd"
PFLAGS="-F"

# Routine to check for 100series server
is_supported_100_series() {
    if [ -z "$SUPPORTED_100_SERIES" ]; then
        SUPPORTED_100_SERIES=1
        if [ -z "$(which dmidecode)" ]; then
            cmaerr "Error: Could not find dmidecode utility, unable to identify system"
            return $SUPPORTED_100_SERIES
        fi
        PRODNAME=`dmidecode | grep "Product Name" | cut -d" " -f4 | head -n1`
        GENERATION=`dmidecode | grep "Product Name" | cut -d" " -f5 | head -n1`
        #Remove the Server Type Tag from Product Name
        PRODNUM=`echo $PRODNAME | sed s/[a-zA-Z]//g`
        if [ "$PRODNUM" -ge 100 -a "$PRODNUM" -lt 200 ] ; then  # TRUE only if the server is 100 series
            if [ "$GENERATION" = "G5" -o "$GENERATION" \> "G5" ]; then  # TRUE only if the server is G5 or higher
                SUPPORTED_100_SERIES=0
            fi
        fi
    fi
    return $SUPPORTED_100_SERIES
}

if [ "x$CMAINCLUDE" = "x" ]; then
   if [ -d /opt/hp/hp-snmp-agents ]; then
        CMAINCLUDE=/opt/hp/hp-snmp-agents/server/etc/cmad
   else
        CMAINCLUDE=/opt/compaq/foundation/etc/cmad
   fi
fi

is_26_kernel() {
    [ "$(uname -r | cut -d. -f1-2)" = "2.6" ]
}

is_OS_VMware4x() {
    RET_STAT=1
    test -x /usr/bin/vmware && is_26_kernel
    if [ $? -eq 0 ]; then
        vmware -v | grep ESX > /dev/null
        if [ $? -eq 0 ]; then
            uname -a | grep ESX > /dev/null
            if [ $? -eq 0 ]; then
                RET_STAT=0
            fi
        fi
    fi
    return $RET_STAT
}

# Do not check for hpilo module on 100series servers
if ! is_supported_100_series ; then
         if is_OS_VMware4x; then
                RUNNING=`/usr/sbin/vmkload_mod -l | fgrep hpilo | cut -d" " -f1`
                if [ "$RUNNING" != "hpilo" ]; then
                     STARTADDON="checkformodule hpilo /etc/init.d/hp-ilo"	
                fi
        else
                STARTADDON="checkformodule hpilo /etc/init.d/hp-ilo"
        fi
fi
STOPADDON=remove_shm

remove_shm()
{
   rc=0
   SHMID=$(/usr/bin/ipcs -m | grep 0x0000bbbd | cut -d ' ' -f 2)
   if [ "$SHMID" != "" ]; then 
      ipcrm shm $SHMID 2> /dev/null > /dev/null
   fi 
   return $rc
}

. $CMAINCLUDE
