#!/usr/bin/perl

# waitpid -- wait for process(es) to complete
#
# This software is Public Domain. Please maintain version history.
# Please report bugs to erco@3dsite.com.
#
#       VERS    DATE            AUTHOR          COMMENTS
#       1.00    11/17/99        erco@3dsite.com Initial implementation
# 

if ( $#ARGV < 0 || $ARGV[0] =~ /^-/ )
{
    print STDERR <<"EOF";
NAME
    waitpid -- wait for process(es) to complete

USAGE
    waitpid pid [..]

EXAMPLES
    waitpid 3131 ; echo "The process is done."
    waitpid 3131 22132 ; echo "Both processes done."

DESCRIPTION
    Waits for specified process id(s) to complete
    (ie. disappear from the process table). 

    When all specified pids have completed, waitpid(1) 
    will exit.

AUTHOR
    erco\@3dsite.com
EOF
    exit(1);
}

# LOOP UNTIL ALL PROCESSES COMPLETED
while ( 1 )
{
    $count = 0;

    # PROCESS LOOP
    foreach ( @ARGV )
    { 
	# PROCESS STILL RUNNING?
	$err = (kill(0,($_+0))) ? 0 : $!;	# obfuscated perl contest ;)

	# NO SUCH PROCESS? DONE
	if ( $err == 3 ) { $count++; }
	# printf("%8s: %d\n", $_, $err);	# debugging
    }

    # ALL PIDS HAVE COMPLETED? DONE
    if ( $count == ($#ARGV + 1) )
	{ exit(0); }

    # KEEP WAITING
    sleep(1);
}
#NOTREACHED#
