#!/bin/csh
#
# IPCCLEAN - Cleans up the IPC message queues, semaphores and shared memory.
#
# <what> - Combination of the letters identifying the IPC items to clean:
#              M - Shared memory segment
#              Q - Message queue
#              S - Semaphore set
#
#
# Examples:
#    1. Clean shared memory only (most common):
#         $ ipcclean m
#
#    2. Clean message queue and semaphore set:
#         $ ipcclean qs
#
#    3. Clean shared memory, message queue and semaphore set:
#         $ ipcclean mqs
#

setenv I_AM `whoami`


if (( $1 =~ *M* ) || ( $1 =~ *m* )) then
  setenv TMP_ID `/usr/bin/ipcs -m | awk ' $3 ~ name && $6 ~ /0/ { print "-m",$2 }' name=$I_AM`
  ipcrm $TMP_ID
endif

if (( $1 =~ *Q* ) || ( $1 =~ *q* )) then
  setenv TMP_ID `/usr/bin/ipcs -s | awk ' $3 ~ name { print "-s",$2 }' name=$I_AM`
  ipcrm $TMP_ID
endif

if (( $1 =~ *S* ) || ( $1 =~ *s* )) then
  setenv TMP_ID `/usr/bin/ipcs -q | awk ' $3 ~ name { print "-q",$2 }' name=$I_AM`
  ipcrm $TMP_ID
endif
