#!/bin/sh

if [ "`whoami`" = "root" ] ; then
	all=true;
else
	all=false;
fi;

if $all ; then
	echo "Removing semaphores from all users"
	for x in `ipcs -s | sed -n 's/^[0-9x]* *\([0-9]*\) .*/\1/p'`; do
		echo "Removing $x...";
		ipcrm -s $x;
	done;
else
	echo "Removing semaphores for `whoami`"
	for x in `ipcs -s | grep \`whoami\` | sed -n 's/^[0-9x]* *\([0-9]*\) .*/\1/p'`; do
		echo "Removing $x...";
		ipcrm -s $x;
	done;
	remain="`ipcs -s | sed -n 's/^[0-9x]* *\([0-9]*\) .*/\1/p'`";
	if [ "$remain" ] ; then
		echo "Semaphores remain for other users:";
		ipcs -s | sed -n 's/^[0-9x]* * [0-9]* *\([^ ]*\) .*/\1/p' | sort | uniq -c
	fi;
fi;
