#!/usr/local/bin/bash

# Confirm if no user was specified
while [ -z "$1" ]
do
        echo -n "Delete all IPC resources owned by $USER? [y/N] "
        read response
        case "$response" in
                y* | Y*)
                        break
                ;;
                n* | N* | "")
                        exit
                ;;
        esac
done

OWNER=`echo ${1:-$USER} | cut -c 1-8`

# Parse the output of ipcs for the IDs of the resources we want
SEM_IDS=`ipcs -s -c | grep "$OWNER" | awk '{print $2}'`

for ID in $SEM_IDS
do
        ipcrm -s $ID > /dev/null
done

# Parse the output of ipcs for the IDs of the resources we want
SHM_IDS=`ipcs -m -c | grep "$OWNER" | awk '{print $2}'`

for ID in $SHM_IDS
do
        ipcrm -m $ID > /dev/null
done
