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