#!/bin/bash

# init file for Compaq Storage peer
#
# processname: cmastorpeerd

PKGBIN=/opt/compaq/storage/bin
PATH=/opt/compaq/storage/bin:$PATH
LOGFILE=/var/spool/compaq/cma.log
NAME="Compaq Storage peer"
PNAME="cmastorpeerd"
PFLAGS=

cmaecho () {
  echo "  " $*
  echo "  " $* >>$LOGFILE 2>&1
}

cmaechon () {
  echo -n "  " $*
  echo -n "  " $* >>$LOGFILE 2>&1
}

vendorversion() {
  # $1 contains the ENV for Vendor
  # $2 contains the ENV for Version
  # Return 0 when succeed
  if [ ! -f /etc/issue ]; then
    echo "/etc/issue does not exist!" 1>&2
    return 1
  fi
  #echo "ARG1=$1"
  #echo "ARG2=$2"
  if [ -z "$1" -o -z "$2" ]; then
     return 2
  fi
  grep "Red *Hat .* 6\.2 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="6.2"
    return 0
  fi
  grep "Red *Hat .* 7\.1 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.1"
    return 0
  fi
  egrep "Red *Hat .* 7\.2 |Red *Hat .* 7\.1\.[0-9]" /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="RedHat"
    eval $2="7.2"
    return 0
  fi
  grep "SuSE .* 7\.0 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="7.0"
    return 0
  fi
  grep "SuSE .* 7\.2 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="7.2"
    return 0
  fi
  grep "SLES-7 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="SuSE"
    eval $2="SLES-7"
    return 0
  fi
  grep "Caldera .* 3\.1 " /etc/issue >/dev/null 2>&1
  if [ "$?" = 0 ]; then
    eval $1="Caldera"
    eval $2="3.1"
    return 0
  fi
  return 9
}

# Identify OS Vendor
VENDOR="Unknown"
vendorversion "VENDOR" "VERSION"
if [ "$VENDOR" != "RedHat" -a "$VENDOR" != "SuSE" ]; then
  echo "ERROR: the Linux OS running on your system is not supported!"
  exit 1
fi


case "$VENDOR" in
  RedHat)
        # source function library
        . /etc/rc.d/init.d/functions
        RETVAL=0
        PATH=$PKGBIN:$PATH
        case "$1" in
          start)
                cmaechon "Starting $NAME ($PNAME): "
                pidlist=`pidof -o $$ $PNAME`
                if [ -z "$pidlist" ]; then
                  $PNAME $PFLAGS >> $LOGFILE 2>&1 &
                  RETVAL=$?
                else
                  RETVAL=1
                fi
                cmaecho
                ;;
          stop)
                cmaechon "Shutting down $NAME ($PNAME): "
                killproc $PNAME  2>/dev/null 
                RETVAL=$?
                cmaecho
                ;;
          restart)
                $0 stop
                $0 start
                RETVAL=$?
                ;;
          status)
                status $PNAME
                RETVAL=$?
                ;;
          *)
                cmaecho "Usage: $PNAME {start|stop|restart|status}"
                exit 1
        esac

        exit $RETVAL
        ;;
  SuSE)
        . /etc/rc.config
        RETVAL=0
        PATH=$PKGBIN:$PATH
        case "$1" in
          start)
                cmaechon "Starting $NAME ($PNAME): "
                pidlist=`pidof -o $$ $PNAME`
                if [ -z "$pidlist" ]; then
                  $PNAME $PFLAGS >> $LOGFILE 2>&1 &
                  RETVAL=$?
                else
                  RETVAL=1
                fi
                cmaecho
                ;;
          stop)
                cmaechon "Shutting down $NAME ($PNAME): "
                killproc $PKGBIN/$PNAME 2> /dev/null
                RETVAL=$?
                cmaecho
                ;;
          restart)
                $0 stop
                $0 start
                RETVAL=$?
                ;;
          status)
                echo -n "Checking for $PNAME: "
                /sbin/checkproc $PKGBIN/$PNAME && echo OK || echo No process
                RETVAL=$?
                ;;
          *)
                cmaecho "Usage: $PNAME {start|stop|restart|status}"
                exit 1
        esac

        exit $RETVAL
        ;;
  Caldera)
        # source function library
        . /etc/rc.d/init.d/functions
        RETVAL=0
        PATH=$PKGBIN:$PATH
        case "$1" in
          start)
                cmaechon "Starting $NAME ($PNAME): "
                pidlist=`pidof -o $$ $PNAME`
                if [ -z "$pidlist" ]; then
                  #ssd -S -n $PNAME -x $PKGBIN/$PNAME -- $PFLAGS >> $LOGFILE 2>&1
                  $PNAME $PFLAGS >> $LOGFILE 2>&1 &
                  RETVAL=$?
                else
                  RETVAL=1
                fi
                cmaecho
                ;;
          stop)
                cmaechon "Shutting down $NAME ($PNAME): "
                killproc $PNAME 2>/dev/null
                i=10
                while [ $i != 0 ]; do
                  #echo "i=$i"
                  pidof $PNAME >/dev/null 2>&1
                  if [ $? != 0 ]; then
                    i=0
                    continue
                  fi
                  sleep 1
                  i=`expr $i - 1`
                done
                PIDS=`pidof $PNAME`
                RETVAL=$?
                cmaecho
                ;;
          restart)
                $0 stop
                $0 start
                RETVAL=$?
                ;;
          status)
                PIDS=`pidof $PNAME`
                RETVAL=$?
                if [ -z "$PIDS" ]; then
                  echo "$PNAME is not running."
                else
                  echo "$PNAME (pid $PIDS) is running ..."
                fi
                ;;
          *)
                cmaecho "Usage: $PNAME {start|stop|restart|status}"
                exit 1
        esac

        exit $RETVAL
        ;;

     *)
        cmaecho "ERROR: Can't identify Linux OS Vendor $VENDOR"
        exit 1
esac

