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