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

while true; do
	pid=`ps -o ppid:1= "$pid"`
	if [ "$pid" = 0 ]; then
		break
	fi
	c_name=`ps -w -w --pid "$pid" -o comm=`
	if [ "$c_name" = "$name" ]; then
		echo "$pid"
		found=1
		if [ -z "$deep" ]; then
			exit 0
		fi
	fi
done

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