* Convert the directory parameter of etckeeper into "-d directory".
[jelmer/etckeeper.git] / etckeeper
1 #!/bin/sh
2 set -e
3 conf=/etc/etckeeper/etckeeper.conf
4
5 usage() {
6         echo "usage: etckeeper command [-d directory]" >&2
7         exit 1
8 }
9
10 if [ -e $conf ]; then
11         . $conf
12 fi
13
14 if [ -z "$VCS" ]; then
15         echo "Please configure a VCS in $conf" >&2
16         exit 1
17 fi
18 export VCS
19
20 if [ ! -z "$GIT_COMMIT_OPTIONS" ]; then
21         export GIT_COMMIT_OPTIONS
22 fi
23 if [ ! -z "$HG_COMMIT_OPTIONS" ]; then
24         export HG_COMMIT_OPTIONS
25 fi
26
27 if [ ! -z "$HIGHLEVEL_PACKAGE_MANAGER" ]; then
28         export HIGHLEVEL_PACKAGE_MANAGER
29 fi
30 if [ ! -z "$LOWLEVEL_PACKAGE_MANAGER" ]; then
31         export LOWLEVEL_PACKAGE_MANAGER
32 fi
33
34 if [ -z "$1" ]; then
35         usage
36 fi
37 command="$1"
38 shift 1
39
40 # compatability code
41 if [ "$command" = "post-apt" ]; then
42         command=post-install
43 elif [ "$command" = "pre-apt" ]; then
44         command=pre-install
45 fi
46
47 if [ ! -d "/etc/etckeeper/$command.d" ]; then
48         echo "etckeeper: /etc/etckeeper/$command.d does not exist" >&2
49         exit 1
50 fi
51
52 if [ "x$1" = "x-d" ]; then
53         if [ -n "$2" ]; then
54                 cd "$2"
55                 shift 2
56         else
57                 usage
58         fi
59 else
60         cd /etc
61 fi
62
63 lsscripts() {
64         perl -e '
65                 $dir=shift;
66                 print join "\n", grep { ! -d $_ && -x $_ }
67                         grep /^\Q$dir\/\E[-a-zA-Z0-9]+$/,
68                         glob "$dir/*";
69         ' "$1"
70 }
71
72 for script in $(lsscripts "/etc/etckeeper/$command.d"); do
73         "$script" "$@"
74 done