#!/usr/bin/ksh
#
# $Id: ripc,v 1.1 2006/06/16 14:03:48 laskait Exp $
#
#set -vx
MODE=0

for i in "$@"
do
  if [ "$i" = "1" ]
  then 
    MODE=1;
  fi
  if [ "$i" = "2" ]
  then 
    MODE=2;
  fi
done

if [ $MODE = 1 ]
then
  export PGWCONFIG=$PGW1CONFIG
else
  if [ $MODE = 2 ]
  then
    export PGWCONFIG=$PGW2CONFIG
  else
    echo "Usage: ripc <1|2>"
    exit -1
  fi
fi

actuser=`whoami`
if [ -z "$PGWCONFIG" ]
then
  echo "The PGWCONFIG is not set!"
  exit
fi

uname="`uname`"
if [ $uname != "Linux" ]
then
  sem="-s"
  shm="-m"
else
  sem="sem"
  shm="shm"
fi

key=`awk -F= '/^ipckey/ {print $2}' $PGWCONFIG`
semid=`ipcs -s | grep $actuser | grep $key | awk '{print $2}'` 
shmid=`ipcs -m | grep $actuser | grep $key | awk '{print $2}'`

if [ -n "$semid" ]
then
  echo "Removing semaphore $semid"
  ipcrm $sem $semid
fi
if [ -n "$shmid" ]
then
  echo "Removing shared memory $shmid"
  ipcrm $shm $shmid
fi

exit 0
