#!/bin/sh

packpid() {
	local pid

	if ! pid=$(pidof "$1");then
		for w in $(ps | grep "$1" |grep -v grep | awk '{print $1}');do
			if [ -e "/proc/$w/stat" ];then
				read wid name rest < /proc/$w/stat 1>/dev/null 2>&1
			else
				continue
			fi
			name="${name#(}"; name="${name%)}"
			if [ "$name" = "$1" ];then
				pid="${w}${pid:+ }${pid}"
			elif [ "$name" = "sh" ];then
				for n in `cat /proc/$w/cmdline | awk '{print $1}'`;do
					name=`basename $n`
					[ "$name" = "$1" ] && {
						pid="${w}${pid:+ }${pid}"
						break;
					}
				done
			fi
		done
	fi

	[ -n "$pid" ] && echo "$pid"
}

killpack() {
	local ret=1

	for w in $(packpid "$2");do
		[ "$$" = "$w" ] && continue
		kill $1 $w 1>/dev/null 2>&1;
		ret=0
	done

	return $ret
}