#!/bin/bash
set -e -u
opt_1=
deep=
n=
. opts
pid=$1 name=$2
found=

find_child_processes() {
	local pid=$1 name=$2
	for pid in `pgrep --parent "$pid"`; do
		c_name=`ps -w -w --pid "$pid" -o comm=`
		if [ "$c_name" = "$name" ]; then
			echo "$pid"
			found=1
			if [ -n "$opt_1" ]; then
				exit
			fi
			if [ -z "$deep" ]; then
				continue
			fi
		fi
		find_child_processes "$pid" "$name"
	done
}

find_child_processes "$pid" "$name"

if [ -z "$n" -a -z "$found" ]; then
	echo >&2 "child: did not find descendant process"
	exit 1
fi
