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