summaryrefslogtreecommitdiff
path: root/run-parts.sh
blob: 8b473a599fafb6ea0dac8725efa484797bcb759a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh

# run-parts - concept taken from Debian
#
# modified for PLD Linux 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

if [ "$1" = "--test" ]; then
	test=yes
	shift
fi

if [ "$1" = "--" ]; then
	shift
fi

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

if [ ! -d $1 ]; then
	echo "Is not a directory: $1"
	echo "Usage: run-parts [-u] [--test] <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

# Ignore *~ and *, scripts
for i in $RUNPARTS_DIR/*[!~,] ; do
	[ -d "$i" ] && continue
	# Don't run *.{rpmsave,rpmorig,rpmnew,swp} scripts
	[ "${i%.rpmsave}" != "${i}" ] && continue
	[ "${i%.rpmorig}" != "${i}" ] && continue
	[ "${i%.rpmnew}" != "${i}" ] && continue
	[ "${i%.swp}" != "${i}" ] && continue
	[ "${i%,v}" != "${i}" ] && continue

	if [ -x "$i" ]; then
		runprog="$i $@"
		if [ "$test" = yes ]; then
			echo "$runprog"
			continue
		fi
		$runprog 2>&1 | awk -v "progname=$i" \
			'progname {
			 	print progname ":\n"
				progname="";
				}
			{ print; }'
	fi
done

exit 0