r3561@krups: intrigeri | 2005-11-15 15:16:50 +0100
[matthijs/upstream/backupninja.git] / src / ninjahelper.in
1 #!@BASH@
2
3 ####################################################
4 ## Functions
5
6 function check_perms() {
7         local file=$1
8         local perms=`ls -ld $file`
9         group_w_perm=${perms:5:1}
10         world_w_perm=${perms:8:1}
11         if [ "$group_w_perm" == "w" -o "$world_w_perm" == "w" ]; then
12             echo $perms
13             echo "helper scripts must not be group or world writable! Dying on file $file"
14             exit
15         fi
16         if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
17                 echo "helper scripts must be owned by root! Dying on file $file"
18                 exit
19         fi
20 }
21
22
23 ##
24 ## returns the next available file name given a file
25 ## in the form @CFGDIR@/backup.d/10.sys
26 ## sets variable $next_filename
27 ##
28 get_next_filename() {
29   next_filename=$1
30   dir=`dirname $next_filename`
31   file=`basename $next_filename`
32   number=${file:0:2}
33   suffix=${file:3}
34   while [ -f $next_filename ]; do
35     let "number += 1"
36     next_filename="$dir/$number.$suffix"
37   done
38 }
39
40 ##
41 ## installs packages (passed in as $@) if not present
42 ##
43 require_packages() {
44        for pkg in "$@"; do
45            installed=`dpkg -s $pkg | grep 'ok installed'`
46        if [ -z "$installed" ]; then 
47            booleanBox "install $pkg?" "This backup action requires package $pkg. Do you want to install it now?"
48            if [ $? = 0 ]; then
49               apt-get install $pkg
50               echo "hit return to continue...."
51               read
52            fi
53        fi
54     done
55 }
56
57 ##
58 ## menu for the wizards
59 ##
60 donew() {
61   listBegin "new action menu" "select an action to create"
62   listItem return "return to main menu"
63   for data in $HELPERS; do
64     data=${data//_/ }
65     helper_function=${data%%:*}
66     helper_info=${data##*:}
67     listItem $helper_function "$helper_info"
68   done
69   listDisplay menu
70     
71   [ $? = 1 ] && return
72   result="$REPLY"
73   [ "$result" = "return" ] && return
74   result=${result}_wizard
75   $result
76 }
77
78 do_rm_action() {
79    booleanBox "remove action" "Are you sure you want to remove action file $1?"
80    if [ $? = 0 ]; then
81      rm $1;
82    fi
83 }
84
85 do_run() {
86    backupninja --run $1
87    echo "Hit return to continue..."
88    read
89 }
90
91 do_xedit() {
92    if [ -z "$EDITOR" -o ! -x "`which $EDITOR`" ]; then
93       if [ -h /etc/alternatives/editor -a -x "`readlink /etc/alternatives/editor`" ]; then
94          EDITOR="`readlink /etc/alternatives/editor`"
95       elif [ -x "`which nano`" ]; then
96          EDITOR="`which nano`"
97       elif [ -x "`which vim`" ]; then
98          EDITOR="`which vim`"
99       elif [ -x "`which vi`" ]; then
100          EDITOR="`which vi`"
101       else
102          echo "No suitable editor found."
103          echo "Please define $EDITOR or configure /etc/alternatives/editor."
104          exit
105       fi
106    fi
107    $EDITOR $1
108 }
109
110 do_run_test() {
111   backupninja --test --run $1
112   echo "Hit return to continue..."
113   read
114 }
115
116 do_disable() {
117   mv $1 $1.disabled
118 }
119
120 do_enable() {
121   mv $1 ${1%.*}
122 }
123
124 do_rename() {
125   dir=`dirname $1`
126   filename=`basename $1`
127   inputBox "rename action" "enter a new filename" $filename
128   mv $dir/$filename $dir/$REPLY
129 }
130
131 doaction() {
132   action=$1
133   base=`basename $action`
134   if [ "${base##*.}" == "disabled" ]; then
135      enable="enable";
136   else
137      enable="disable";
138   fi
139   while true; do
140     menuBox "action menu" "$action $first" \
141       main "return to main menu" \
142       view "view configuration" \
143       xedit "launch external editor" \
144       $enable "$enable action" \
145       name "change the filename" \
146       run "run this action now" \
147       test "do a test run" \
148       kill "remove this action" 
149     [ $? = 1 ] && return;
150     result="$REPLY"
151         case "$result" in
152            "view") dialog --textbox $action 0 0;; 
153            "xedit") do_xedit $action;;
154            "disable") do_disable $action; return;;
155            "enable") do_enable $action; return;;
156            "name") do_rename $action; return;;
157            "run") do_run $action;;
158            "test") do_run_test $action;; 
159            "kill") do_rm_action $action; return;;
160            "main") return;;
161         esac
162   done
163 }
164
165 #####################################################
166 ## begin program
167
168 if [ ! -x "`which dialog`" ]; then
169         echo "ninjahelper is a menu based wizard for backupninja."
170         echo "It requires 'dialog' in order to run. Do you want to install dialog now?"
171         while true; do
172           echo -n "(yes/no): "
173           read install
174           if [ "$install" == "yes" ]; then
175              apt-get install dialog
176              break
177           elif [ "$install" == "no" ]; then
178              exit
179           else
180              echo "You must answer 'yes' or 'no'"
181           fi
182     done
183 fi
184
185 conffile="@CFGDIR@/backupninja.conf"
186 if [ ! -r "$conffile" ]; then
187         echo "Configuration file $conffile not found." 
188         exit 1
189 fi
190
191 # find $scriptdir
192 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
193 if [ -z "$scriptdir" ]; then
194         if [ -d "@datadir@" ]; then
195            scriptdir="@datadir@"
196         else
197            echo "Could not find entry 'scriptdirectory' in $conffile" 
198            exit 1
199         fi
200 else
201         if [ ! -d "$scriptdir" ]; then
202            echo "Script directory $scriptdir not found." 
203            exit 1
204         fi         
205 fi
206
207 # find $libdir
208 libdir=`grep libdirectory $conffile | awk '{print $3}'`
209 if [ -z "$libdir" ]; then
210         if [ -d "@libdir@" ]; then
211            libdir="@libdir@"
212         else
213            echo "Could not find entry 'libdirectory' in $conffile." 
214            exit 1
215         fi
216 else
217         if [ ! -d "$libdir" ]; then
218            echo "Lib directory $libdir not found." 
219            exit 1
220         fi
221 fi
222
223 configdirectory=`grep configdirectory $conffile | awk '{print $3}'`
224 if [ ! -n "$configdirectory" ]; then
225         echo "Cound not find entry 'configdirectory' in $conffile" 
226         exit 1
227 fi
228 if [ ! -d "$configdirectory" ]; then
229         echo "Configuration directory $configdirectory not found." 
230         exit 1
231 fi
232
233 . $libdir/easydialog
234
235 if [ "$UID" != "0" ]; then
236         msgBox "warning" "ninjahelper must be run by root!"
237         exit 1
238 fi
239
240 # load all the helpers
241 HELPERS=""
242 for file in `find $scriptdir -follow -name '*.helper'`; do
243    check_perms $file
244    . $file
245 done
246
247 setApplicationTitle "ninjahelper"
248 setDimension 75 19
249
250 #####################################################
251 ## main event loop
252
253 while true; do
254
255 menulist=
256 action=
257 let "i = 1"
258 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
259   menulist="$menulist $i $file"
260   actions[$i]=$file
261   let "i += 1"
262 done
263
264 menuBox "main menu" "Select a backup action for more options, or create a new action:" $menulist \
265   new "create a new backup action" \
266   quit "leave ninjahelper" 
267
268 [ $? = 1 -o $? = 255 ] && exit 0;
269
270 choice="$REPLY"
271 if [ "$choice" == "new" ]; then
272   donew;
273 elif [ "$choice" == "quit" ]; then
274   exit 0;
275 else
276   action=${actions[$choice]};
277   if [ -f "$action" ]; then
278      doaction $action
279   else
280      msgBox "error" "error: cannot find the file '$action'"
281   fi
282 fi
283
284
285 done