(no commit message)
[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 if [ ! -x "`which dialog`" ]; then
149         echo "ninjahelper is a menu based wizard for backupninja."
150         echo "It requires 'dialog' in order to run. Do you want to install dialog now?"
151         while true; do
152           echo -n "(yes/no): "
153           read install
154           if [ "$install" == "yes" ]; then
155              apt-get install dialog
156              break
157           elif [ "$install" == "no" ]; then
158              exit
159           else
160              echo "You must answer 'yes' or 'no'"
161           fi
162     done
163 fi
164
165 conffile="/etc/backupninja.conf"
166 if [ ! -r "$conffile" ]; then
167         echo "Configuration file $conffile not found." 
168         exit 1
169 fi
170 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
171 if [ ! -n "$scriptdir" ]; then
172         echo "Cound not find entry 'scriptdirectory' in $conffile" 
173         exit 1
174 fi
175 if [ ! -d "$scriptdir" ]; then
176         echo "Script directory $scriptdir not found." 
177         exit 1
178 fi
179 configdirectory=`grep configdirectory $conffile | awk '{print $3}'`
180 if [ ! -n "$configdirectory" ]; then
181         echo "Cound not find entry 'configdirectory' in $conffile" 
182         exit 1
183 fi
184 if [ ! -d "$configdirectory" ]; then
185         echo "Configuration directory $configdirectory not found." 
186         exit 1
187 fi
188
189 . $scriptdir/easydialog.sh
190
191 if [ "$UID" != "0" ]; then
192         msgBox "warning" "ninjahelper must be run by root!"
193         exit 1
194 fi
195
196 # load all the helpers
197 HELPERS=""
198 for file in `find $scriptdir -follow -name '*.helper'`; do
199    check_perms $file
200    . $file
201 done
202
203 setApplicationTitle "ninjahelper"
204 setDimension 75 19
205
206 #####################################################
207 ## main event loop
208
209 while true; do
210
211 menulist=
212 action=
213 let "i = 1"
214 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
215   menulist="$menulist $i $file"
216   actions[$i]=$file
217   let "i += 1"
218 done
219
220 menuBox "main menu" "select an action to edit" $menulist \
221   new "create a new backup action" \
222   quit "leave ninjahelper" 
223
224 [ $? = 1 -o $? = 255 ] && exit 0;
225
226 choice="$REPLY"
227 if [ "$choice" == "new" ]; then
228   donew;
229 elif [ "$choice" == "quit" ]; then
230   exit 0;
231 else
232   action=${actions[$choice]};
233   if [ -f "$action" ]; then
234      doaction $action
235   else
236      msgBox "error" "error: cannot find the file '$action'"
237   fi
238 fi
239
240
241 done