#!/bin/sh
set -e
+conf=/etc/etckeeper/etckeeper.conf
-if [ "$(basename $0)" != etckeeper ]; then
- command="$(basename $0 | sed -e s/etckeeper-//)"
-else
- if [ -z "$1" ]; then
- echo "usage: etckeeper command" >&2
- exit 1
- fi
- command="$1"
- shift 1
+usage() {
+ echo "usage: etckeeper command [-d directory]" >&2
+ exit 1
+}
+
+if [ -e $conf ]; then
+ . $conf
+fi
+
+if [ -z "$VCS" ]; then
+ echo "Please configure a VCS in $conf" >&2
+ exit 1
+fi
+export VCS
+
+if [ ! -z "$GIT_COMMIT_OPTIONS" ]; then
+ export GIT_COMMIT_OPTIONS
+fi
+if [ ! -z "$HG_COMMIT_OPTIONS" ]; then
+ export HG_COMMIT_OPTIONS
+fi
+if [ ! -z "$BZR_COMMIT_OPTIONS" ]; then
+ export BZR_COMMIT_OPTIONS
+fi
+
+if [ ! -z "$HIGHLEVEL_PACKAGE_MANAGER" ]; then
+ export HIGHLEVEL_PACKAGE_MANAGER
+fi
+if [ ! -z "$LOWLEVEL_PACKAGE_MANAGER" ]; then
+ export LOWLEVEL_PACKAGE_MANAGER
+fi
+
+if [ -z "$1" ]; then
+ usage
+fi
+command="$1"
+shift 1
+
+# compatability code
+if [ "$command" = "post-apt" ]; then
+ command=post-install
+elif [ "$command" = "pre-apt" ]; then
+ command=pre-install
fi
if [ ! -d "/etc/etckeeper/$command.d" ]; then
exit 1
fi
-if [ "$command" = post-apt ] || [ "$command" = pre-apt ]; then
+if [ "x$1" = "x-d" ]; then
+ if [ -n "$2" ]; then
+ cd "$2"
+ shift 2
+ else
+ usage
+ fi
+else
cd /etc
-elif [ "$command" = pre-commit ] && [ -n "$1" ]; then
- chdir "$1"
fi
-run-parts "/etc/etckeeper/$command.d"
+lsscripts() {
+ perl -e '
+ $dir=shift;
+ print join "\n", grep { ! -d $_ && -x $_ }
+ grep /^\Q$dir\/\E[-a-zA-Z0-9]+$/,
+ glob "$dir/*";
+ ' "$1"
+}
+
+for script in $(lsscripts "/etc/etckeeper/$command.d"); do
+ "$script" "$@"
+done