made it so that helpers are dynamically defined.
[matthijs/upstream/backupninja.git] / ninjahelper
1 #!/bin/bash
2
3 ####################################################
4 ## Functions
5
6 function check_perms() {
7         local file=$1
8         local perms=`ls -ld $file`
9         perms=${perms:4:6}
10         if [[ "$perms" != "------" && "$perms" != "r--r--" ]]; then
11             echo $perms
12                 echo "helper scripts must not be group or world writable! Dying on file $file"
13                 exit
14         fi
15         if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
16                 echo "helper scripts must be owned by root! Dying on file $file"
17                 exit
18         fi
19 }
20
21
22 ##
23 ## returns the next available file name given a file
24 ## in the form /etc/backup.d/10.sys
25 ## sets variable $returned_filename
26 ##
27 get_next_filename() {
28   next_filename=$1
29   dir=`dirname $next_filename`
30   file=`basename $next_filename`
31   number=${file:0:2}
32   suffix=${file:3}
33   while [ -f $next_filename ]; do
34     let "number += 1"
35     next_filename="$dir/$number.$suffix"
36   done
37 }
38
39 ##
40 ## installs packages (passed in as $@) if not present
41 ##
42 require_packages() {
43         for pkg in "$@"; do
44            installed=`dpkg -s $pkg | grep 'ok installed'`
45        if [ -z "$installed" ]; then 
46            booleanBox "install $pkg?" "This backup action requires package $pkg. Do you want to install it now?"
47            if [ $? = 0 ]; then
48               apt-get install $pkg
49               echo "hit return to continue...."
50               read
51            fi
52        fi
53     done
54 }
55
56 ##
57 ## menu for the wizards
58 ##
59 donew() {
60   listBegin "new action menu" "select an action to create"
61   listItem return "return to main menu"
62   for data in $HELPERS; do
63     data=${data//_/ }
64     helper_function=${data%%:*}
65     helper_info=${data##*:}
66     listItem $helper_function "$helper_info"
67   done
68   listDisplay menu
69     
70   [ $? = 1 ] && return
71   result="$REPLY"
72   [ "$result" = "return" ] && return
73   result=${result}_wizard
74   $result
75 }
76
77 do_rm_action() {
78    booleanBox "remove action" "Are you sure you want to remove action file $1?"
79    if [ $? = 0 ]; then
80      rm $1;
81    fi
82 }
83
84 do_run() {
85    backupninja --run $1
86    echo "Hit return to continue..."
87    read
88 }
89
90 do_run_test() {
91   backupninja --test --run $1
92   echo "Hit return to continue..."
93   read
94 }
95
96 do_disable() {
97   mv $1 $1.disabled
98 }
99
100 do_enable() {
101   mv $1 ${1%.*}
102 }
103
104 do_rename() {
105   dir=`dirname $1`
106   filename=`basename $1`
107   inputBox "rename action" "enter a new filename" $filename
108   mv $dir/$filename $dir/$REPLY
109 }
110
111 doaction() {
112   action=$1
113   base=`basename $action`
114   if [ "${base##*.}" == "disabled" ]; then
115      enable="enable";
116   else
117      enable="disable";
118   fi
119   while true; do
120     menuBox "action menu" "$action $first" \
121       main "return to main menu" \
122       view "view configuration" \
123       xedit "launch external editor" \
124       $enable "$enable action" \
125       name "change the filename" \
126       run "run this action now" \
127       test "do a test run" \
128       kill "remove this action" 
129     [ $? = 1 ] && return;
130     result="$REPLY"
131         case "$result" in
132            "view") dialog --textbox $action 0 0;; 
133            "xedit") $EDITOR $action;;
134            "disable") do_disable $action; return;;
135            "enable") do_enable $action; return;;
136            "name") do_rename $action; return;;
137            "run") do_run $action;;
138            "test") do_run_test $action;; 
139            "kill") do_rm_action $action; return;;
140            "main") return;;
141         esac
142   done
143 }
144
145 #####################################################
146 ## begin program
147
148 conffile="/etc/backupninja.conf"
149 if [ ! -r "$conffile" ]; then
150         echo "Configuration file $conffile not found." 
151         exit 1
152 fi
153 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
154 if [ ! -n "$scriptdir" ]; then
155         echo "Cound not find entry 'scriptdirectory' in $conffile" 
156         exit 1
157 fi
158 if [ ! -d "$scriptdir" ]; then
159         echo "Script directory $scriptdir not found." 
160         exit 1
161 fi
162 configdirectory=`grep configdirectory $conffile | awk '{print $3}'`
163 if [ ! -n "$configdirectory" ]; then
164         echo "Cound not find entry 'configdirectory' in $conffile" 
165         exit 1
166 fi
167 if [ ! -d "$configdirectory" ]; then
168         echo "Configuration directory $configdirectory not found." 
169         exit 1
170 fi
171
172 . $scriptdir/easydialog.sh
173
174 if [ "$UID" != "0" ]; then
175         msgBox "warning" "ninjahelper must be run by root!"
176         exit 1
177 fi
178
179 # load all the helpers
180 HELPERS=""
181 for file in `find $scriptdir -follow -name '*.helper'`; do
182    check_perms $file
183    . $file
184 done
185
186 setApplicationTitle "ninjahelper"
187 setDimension 75 19
188
189 #####################################################
190 ## main event loop
191
192 while true; do
193
194 menulist=
195 action=
196 let "i = 1"
197 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
198   menulist="$menulist $i $file"
199   actions[$i]=$file
200   let "i += 1"
201 done
202
203 menuBox "main menu" "select an action to edit" $menulist \
204   new "create a new backup action" \
205   quit "leave ninjahelper" 
206
207 [ $? = 1 -o $? = 255 ] && exit 0;
208
209 choice="$REPLY"
210 if [ "$choice" == "new" ]; then
211   donew;
212 elif [ "$choice" == "quit" ]; then
213   exit 0;
214 else
215   action=${actions[$choice]};
216   if [ -f "$action" ]; then
217      doaction $action
218   else
219      msgBox "error" "error: cannot find the file '$action'"
220   fi
221 fi
222
223
224 done