#!/bin/sh

# run-parts - concept taken from Debian
#
# modified for PLD by Pawel Wilk <siefca@pld-linux.org>
#
# NOTE:
#	1.) run-parts is now able to get arguments!
#	2.) relative pathname of the invoked directory can be
#	    obtained by reading RUNPARTS_DIR env. variable
#	3.) absolute pathname of the invoked directory can be
#	    obtained by reading RUNPARTS_ADIR env. variable
#

# keep going when something fails
set +e

# std checks
if [ $# -lt 1 ]; then
	echo "Usage: run-parts <dir> <args...>"
	exit 1
fi

if [ ! -d $1 ]; then
	echo "Is not a directory: $1"
	echo "Usage: run-parts <dir> <args...>"
	exit 1
fi

# assign passed dir name
RUNPARTS_DIR=$1

# assign absolute dir name
olddir=`pwd`
cd $RUNPARTS_DIR
RUNPARTS_ADIR=`pwd`
cd $olddir
unset olddir

# export directories for our descendants
export RUNPARTS_ADIR RUNPARTS_DIR

# shift args
shift

# exec progs if any
for i in $RUNPARTS_DIR/* ; do
	[ -d $i ] && continue
	if [ -x $i ]; then
		$i $*
	fi
done

exit 0
