#!/bin/sh
##################################################################
#		P A R G S
#		---------
# Description:
#	Run multiple commands with different arguments in,
#	parallel. The pargs command is used similar to xargs.
#
# Usage:
#	pargs [-n <n-processes>] [-a <appended-text>] command...
#
# By:
#	Lars Berntzon, Cecilia Data AB.
#	
##################################################################

prog=`basename $0`
nProcs=5
ARGS=""
appendText=""

main()
{
    get_args "$@"
    run_parallel $ARGS
}

get_args()
{
    while [ $# != 0 ]
    do
	case "$1" in
	-n)
	    nProcs=$2
	    shift
	    ;;
	-a)
	    appendText="$2"
	    shift
	    ;;
	-*)
	    fatal "$1: unknown option"
	    ;;
	*)
	    break
	    ;;
	esac
	shift
    done
    ARGS="$@"
    nProcsString=`echo ................................................................................................ | \
		  dd bs=1 count=$nProcs 2>/dev/null`
}

run_parallel()
{
    ARGS="$@"
    count=""
    while read inputLine
    do
	for i in $inputLine
	do
	    $ARGS $i $appendText &
	    count="$count."
	    if [ "$count" = $nProcsString ]; then
		wait
		count=""
	    fi
	done
    done
    wait
}

fatal()
{
    echo "fatal: $prog: $*" >&2
    exit 1
}

main "$@"

#
# History of changes:
# -------------------
# pargs,v
# Revision 1.1  1996/09/14 18:15:02  lasse
# Created
#
#
