#!/bin/bash
# ---------------------------------------------------------------------------
# NAME
#
#     killsmc - kills smc user applications and those running in the
#               same session
#
# SYNOPSIS
#
#     killsmc {-signal}
#
# DESCRIPTION
#
#     This script looks for processes that are running in sessions that
#     were spawned in the "smc" user; this ensures that sudo applications
#     that were started by the smc user will also be killed.  The optional
#     argument will be provided to "kill" to set the signal number to
#     send.
#
# EXAMPLES
#
#     To kill with signal 15:
#
#         killsmc
#
#     To kill with signal 9:
#
#         killsmc -9
#
# ---------------------------------------------------------------------------

# Outputs a comma-separated list of items that were read from standard
# input
commasep() {
	read line
	if [ -n "$line" ]
	then
		echo -n "$line"
		while [ -n "$line" ]
		do
			read line
			if [ -n "$line" ]
			then
				echo -n ",$line"
			fi
		done
	fi
	return 0
}

# gets a list of all session ID's that are running "smc" processes,
# separated by commas
allsess=`ps --noheadings -U smc -o sess | sort -u | commasep`

# gets a list of pids to kill based on the session ID's above
killpids=`ps --noheadings --sid "$allsess" -o pid`

# kills the pids
sudo kill $1 $killpids

