#!/bin/sh

USAGE="Usage: mwcheck_comsrvrinit <init_file> <com_server> [com_server_arguments]"

############## init

echo "----------------------------------------------------"

if [ $# -gt 1  ] ;then
  INIT_FILE="$1"
  COM_SRVR="$2"
else
  echo ""
  echo "$USAGE"
  echo ""
  echo "----------------------------------------------------"
  exit 1
fi

############# check init file permissions

INIT_FILE_MODE=`ls -l $INIT_FILE | awk '{ print $1;}'`

INIT_FILE_MODE_SHORT=`echo $INIT_FILE_MODE |  sed 's/w//g'| sed 's/-//g' | sed 's/s/x/g' | sed 's/+//g'`

if [ "$INIT_FILE_MODE_SHORT" != "rxrxrx" ]; then
   echo ""
   echo "WARNING"
   echo "    Current init file mode is $INIT_FILE_MODE."
   echo "    Please make sure, that the COM server activator"
   echo "    has permissions to read/execute this file."
   echo ""
fi

############# run

exec mwcomchk $* &

############# get pid

SRVR_EX_NAME=`echo $COM_SRVR | sed 's+^.*/++g'`

sleep 2

SRVR_ALIVE=`ps -f | grep "$SRVR_EX_NAME" | grep -v grep | awk '{ print $2; }'`

############# check

if [ "$SRVR_ALIVE" ];then
   echo ""
   echo "CHECK SUCCEEDED"
   echo ""
   echo "----------------------------------------------------"
   kill "$SRVR_ALIVE"
   exit `echo $status`
else
   echo ""
   echo "CHECK FAILED"
   echo ""
   echo "$USAGE"
   echo ""
   echo "----------------------------------------------------"
   exit 1
fi

############## end