A little rearranging to get to autotools
[matthijs/upstream/backupninja.git] / ninjahelper
index 9631b80e9c41742b253ca07ebeaa7f401c83a48e..13a4ca9a727f62659327549ba9f8cd9f089d84ce 100755 (executable)
@@ -3,6 +3,23 @@
 ####################################################
 ## Functions
 
+function check_perms() {
+       local file=$1
+       local perms=`ls -ld $file`
+       group_w_perm=${perms:5:1}
+       world_w_perm=${perms:8:1}
+       if [ "$group_w_perm" == "w" -o "$world_w_perm" == "w" ]; then
+           echo $perms
+           echo "helper scripts must not be group or world writable! Dying on file $file"
+           exit
+       fi
+       if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
+               echo "helper scripts must be owned by root! Dying on file $file"
+               exit
+       fi
+}
+
+
 ##
 ## returns the next available file name given a file
 ## in the form /etc/backup.d/10.sys
@@ -37,37 +54,25 @@ require_packages() {
     done
 }
 
-doradiobox() {
-       defaultchoice="red is.pretty on"
-       choices="green is_nice_too off blue i_love_blue off yellow is.bright off orange make.me.hungry off"
-       radioBox "radio title" "choose one color" $defaultchoice $choices
-       case $? in
-          0) ;;
-          1) echo "color choice cancelled..."; sleep 1;;
-          255) echo "something went wrong, exiting..."
-             exit 1 ;;
-       esac
-       result="$REPLY"
-       msgBox "message title" "you said $result."
-}
-
+##
+## menu for the wizards
+##
 donew() {
-    menuBox "new action menu" "select an action to create"  \
-      return "return to main menu" \
-      sys "general hardware and system info" \
-      mysql "mysql database backup" \
-      ldap "ldap database backup" \
-      rdiff "incremental filesystem backup"
-  
-    [ $? = 1 ] && return;
-    result="$REPLY"
-       case "$result" in
-          "sys") sys_wizard;; 
-          "mysql") mysql_wizard;;
-          "ldap") ldap_wizard;;
-          "rdiff") rdiff_wizard;;
-          "return") return;;
-       esac
+  listBegin "new action menu" "select an action to create"
+  listItem return "return to main menu"
+  for data in $HELPERS; do
+    data=${data//_/ }
+    helper_function=${data%%:*}
+    helper_info=${data##*:}
+    listItem $helper_function "$helper_info"
+  done
+  listDisplay menu
+    
+  [ $? = 1 ] && return
+  result="$REPLY"
+  [ "$result" = "return" ] && return
+  result=${result}_wizard
+  $result
 }
 
 do_rm_action() {
@@ -83,6 +88,25 @@ do_run() {
    read
 }
 
+do_xedit() {
+   if [ -z "$EDITOR" -o ! -x "`which $EDITOR`" ]; then
+      if [ -h /etc/alternatives/editor -a -x "`readlink /etc/alternatives/editor`" ]; then
+        EDITOR="`readlink /etc/alternatives/editor`"
+      elif [ -x "`which nano`" ]; then
+        EDITOR="`which nano`"
+      elif [ -x "`which vim`" ]; then
+        EDITOR="`which vim`"
+      elif [ -x "`which vi`" ]; then
+        EDITOR="`which vi`"
+      else
+        echo "No suitable editor found."
+        echo "Please define $EDITOR or configure /etc/alternatives/editor."
+        exit
+      fi
+   fi
+   $EDITOR $1
+}
+
 do_run_test() {
   backupninja --test --run $1
   echo "Hit return to continue..."
@@ -90,15 +114,11 @@ do_run_test() {
 }
 
 do_disable() {
-  dir=`dirname $1`
-  base=`basename $1`
-  mv $dir/$base $dir/0-$base
+  mv $1 $1.disabled
 }
 
 do_enable() {
-  dir=`dirname $1`
-  base=`basename $1`
-  mv $dir/$base $dir/${base:2}
+  mv $1 ${1%.*}
 }
 
 do_rename() {
@@ -111,7 +131,7 @@ do_rename() {
 doaction() {
   action=$1
   base=`basename $action`
-  if [ "${base:0:2}" == "0-" ]; then
+  if [ "${base##*.}" == "disabled" ]; then
      enable="enable";
   else
      enable="disable";
@@ -124,13 +144,13 @@ doaction() {
       $enable "$enable action" \
       name "change the filename" \
       run "run this action now" \
-      test "do a test run" \
+      test "test connections and passwords only" \
       kill "remove this action" 
     [ $? = 1 ] && return;
     result="$REPLY"
        case "$result" in
           "view") dialog --textbox $action 0 0;; 
-          "xedit") $EDITOR $action;;
+          "xedit") do_xedit $action;;
           "disable") do_disable $action; return;;
           "enable") do_enable $action; return;;
           "name") do_rename $action; return;;
@@ -145,6 +165,23 @@ doaction() {
 #####################################################
 ## begin program
 
+if [ ! -x "`which dialog`" ]; then
+       echo "ninjahelper is a menu based wizard for backupninja."
+       echo "It requires 'dialog' in order to run. Do you want to install dialog now?"
+       while true; do
+         echo -n "(yes/no): "
+         read install
+         if [ "$install" == "yes" ]; then
+            apt-get install dialog
+            break
+         elif [ "$install" == "no" ]; then
+            exit
+         else
+            echo "You must answer 'yes' or 'no'"
+         fi
+    done
+fi
+
 conffile="/etc/backupninja.conf"
 if [ ! -r "$conffile" ]; then
        echo "Configuration file $conffile not found." 
@@ -176,7 +213,10 @@ if [ "$UID" != "0" ]; then
        exit 1
 fi
 
+# load all the helpers
+HELPERS=""
 for file in `find $scriptdir -follow -name '*.helper'`; do
+   check_perms $file
    . $file
 done
 
@@ -197,7 +237,7 @@ for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
   let "i += 1"
 done
 
-menuBox "main menu" "select an action to edit" $menulist \
+menuBox "main menu" "Select a backup action for more options, or create a new action:" $menulist \
   new "create a new backup action" \
   quit "leave ninjahelper"