From d2ebb15c724e1a317f85cc94b742b2f68fa742cb Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 11 Mar 2024 11:36:07 +0100
Subject: [PATCH] move included files into subfolder

---
 backup.sh                             |  4 +--
 check_clientbackup.sh                 |  2 +-
 detector.sh                           | 51 ++++++++++++++++-----------
 includes/dbdetect.class.sh            | 16 ++++++---
 inc_bash.sh => includes/inc_bash.sh   |  0
 jobhelper.sh => includes/jobhelper.sh |  0
 localdump.sh                          |  8 +++--
 restore.sh                            |  4 +--
 transfer.sh                           |  4 +--
 9 files changed, 55 insertions(+), 34 deletions(-)
 rename inc_bash.sh => includes/inc_bash.sh (100%)
 rename jobhelper.sh => includes/jobhelper.sh (100%)

diff --git a/backup.sh b/backup.sh
index c9ddb85..61ad863 100755
--- a/backup.sh
+++ b/backup.sh
@@ -17,8 +17,8 @@
 # 2022-11-04  ah  v1.2  rename hooks
 # ================================================================================
 
-. $( dirname "$0" )/jobhelper.sh
-. `dirname $0`/inc_bash.sh
+. $( dirname "$0" )/includes/jobhelper.sh
+. `dirname $0`/includes/inc_bash.sh
 
   typeset -i rcBackup=0
   typeset -i rcTransfer=0
diff --git a/check_clientbackup.sh b/check_clientbackup.sh
index 1daec2a..73d5688 100755
--- a/check_clientbackup.sh
+++ b/check_clientbackup.sh
@@ -20,7 +20,7 @@
 # 2023-09-04  ah     v1.6  fix perfdata dirs-del=
 # ==============================================================================
 
-. $(dirname $0)/jobhelper.sh
+. $(dirname $0)/includes/jobhelper.sh
 
 # ------------------------------------------------------------------------------
 # CONFIG
diff --git a/detector.sh b/detector.sh
index 7c57574..2eca8cf 100755
--- a/detector.sh
+++ b/detector.sh
@@ -1,7 +1,7 @@
 #!/bin/bash
 # ====================================================================
 #
-#   P O C   *    Detect a database type
+#   Detect a database type
 #
 # --------------------------------------------------------------------
 # ah: <www.axel-hahn.de>
@@ -11,10 +11,12 @@
 cd $( dirname $0 )
 
 . vendor/ini.class.sh || exit 1
+. vendor/color.class.sh || exit 1
+
 . $(dirname $0)/includes/dbdetect.class.sh
 
 DBD_DEBUG=0
-
+showInfos=0
 USAGE="Detect profiles for databases which are located in 
 $DBD_BASEDIR.
 
@@ -24,15 +26,17 @@ This script helps you to define / verify custom profiles.
 SYNTAX: $( basename $0) [OPTIONS] [FILTER]
 
 OPTIONS:
-    -d|--debug  show debug information
-    -h|--help   Show this help
+    -h|--help     Show this help
+    -i|--infos    Show infos about detected profiles
+    -v|--verbose  Show debug information during detection. This helps to find
+                  out why a profile was skipped.
 
 PARAMETERS:
     FILTER   a string / regex to filter profilenames.
 
 SXYNTAX:
-    $( basename $0) -d      Test all profiles with debug output
     $( basename $0) mysql   Test profiles matching 'mysql' only
+    $( basename $0) -v      Test all profiles with debug output
 "
 # --------------------------------------------------------------------
 
@@ -40,36 +44,43 @@ SXYNTAX:
 # MAIN
 # ----------------------------------------------------------------------
 
-echo
-echo " --==##|  DATABASE PROFILE DETECTOR  |##==-- "
-echo
+echo ';' >&2
+echo '; --==##|  DATABASE PROFILE DETECTOR  |##==-- '
+echo ';' >&2
 
-while [[ "$#" -gt 0 ]]; do case $1 in
-    -d|--debug)     DBD_DEBUG=1;;
+while [ "$#" -gt 0 ]; do case $1 in
     -h|--help)      echo "$USAGE"; exit 0;;
+    -i|--infos)     showInfos=1; shift 1;;
+    -v|--verbose)   DBD_DEBUG=1; shift 1;;
+
     *) if grep "^-" <<< "$1" >/dev/null ; then
-        echo; echo "ERROR: Unknown parameter: $1"; echo; _showHelp; exit 2
+        echo; echo "ERROR: Unknown parameter: $1"; echo; echo "$USAGE"; exit 2
        fi
        break;
        ;;
 esac; done
 
+sFilter="${1:-.}"
+test ! "${sFilter}" = "." && ( echo -n "Filter enabled: "; color.echo "blue" "${sFilter}"; echo ';' >&2)
+
 dbdetect._wd "------"
-for config in $(dbdetect.getConfigs | grep "${1:-.}"); do
+for config in $(dbdetect.getConfigs | grep "${sFilter}"); do
     dbdetect._wd "----- $config"
     if dbdetect.exists $config; then
-        echo "FOUND: $config"
-
-        echo "  Type       : $( dbdetect.getType    $config )"
-        echo "  Target dir : $( dbdetect.getProfile $config )"
-        echo "  runas      : $( dbdetect.runas )"
-        echo "  Params     : $( dbdetect.getParams )"
-        echo
+        color.print "green" "FOUND"; echo ": $config"
+        if [ "$showInfos" -gt "0" ]; then
+            echo "  Type       : $( dbdetect.getType    $config )"
+            echo "  Target dir : $( dbdetect.getProfile $config )"
+            echo "  runas      : $( dbdetect.runas )"
+            echo "  Params     : $( dbdetect.getParams )"
+            echo "  Env        : $( dbdetect.setenv )"
+            echo "  Files      : $( dbdetect.getFiles | grep -c '.' )"
+            echo ';'
+        fi
     else
         echo "SKIP : $config" 
     fi
     dbdetect._wd
 done
-echo
 
 # --------------------------------------------------------------------
diff --git a/includes/dbdetect.class.sh b/includes/dbdetect.class.sh
index 2cdd781..1c33a5b 100644
--- a/includes/dbdetect.class.sh
+++ b/includes/dbdetect.class.sh
@@ -6,6 +6,10 @@
 # it analyzes all ini files with database profiles and sets
 # variables / environment to perform database backup/ restore
 #
+# Remark:
+# The script that sources this file must source vendor/ini.class.sh
+# before using these functions.
+#
 # ----------------------------------------------------------------------
 # ======================================================================
 
@@ -24,13 +28,13 @@ declare -A DBD_PARAMS
 # write debug output if DBD_DEBUG is enabled
 # param  string  text to show
 function dbdetect._wd(){
-    test "$DBD_DEBUG" -eq "1" && echo "DEBUG: $*" >&2
+    test "$DBD_DEBUG" -ne "0" && color.echo "darkgray" "DEBUG: $*" >&2
 }
 
 # get a list of all config files
 # param  string  full path of ini file
 function dbdetect.getConfigs(){
-    find ${DBD_BASEDIR} -type f -name "*.ini"
+    find ${DBD_BASEDIR} -type f -name "*.ini" | sort
 }
 
 # get type from given ini file
@@ -115,12 +119,16 @@ function dbdetect.exists(){
       for myfile in $( ini.value "file[]" )
       do
         if ! file -b "${myfile}" | grep -i "$filetype" >/dev/null; then
-          dbdetect._wd "... File ${myfile} is no type $filetype"
+          dbdetect._wd "... File ${myfile} is not of type $filetype"
           return 1
         fi
         dbdetect._wd "... File ${myfile} is type $filetype"
         myfiles+="${myfile}|"
       done
+      if [ -z "${myfiles}" ]; then
+        dbdetect._wd "... No files for type [${filetype}] were added"
+        return 1
+      fi
     fi
 
     # --- OK, everything was found ... we initialize it
@@ -135,7 +143,7 @@ function dbdetect.exists(){
     for mykey in su env params
     do
         value="$( ini.value "$mykey" )"
-        value="${value//\{\{tcp\}\}/$tcpport}"
+        value="${value//\{\{tcp-port\}\}/$tcpport}"
         value="${value//\{\{tcp-target\}\}/$tcptarget}"
         value="${value//\{\{dbuser\}\}/$dbuser}"
         value="${value//\{\{dbpassword\}\}/$dbpassword}"
diff --git a/inc_bash.sh b/includes/inc_bash.sh
similarity index 100%
rename from inc_bash.sh
rename to includes/inc_bash.sh
diff --git a/jobhelper.sh b/includes/jobhelper.sh
similarity index 100%
rename from jobhelper.sh
rename to includes/jobhelper.sh
diff --git a/localdump.sh b/localdump.sh
index d5e9334..9d830a1 100755
--- a/localdump.sh
+++ b/localdump.sh
@@ -31,10 +31,12 @@
 # CONFIG VARS
 # ----------------------------------------------------------------------
 
+  . $(dirname $0)/vendor/ini.class.sh    
+  . $(dirname $0)/vendor/color.class.sh
+
+  . $(dirname $0)/includes/jobhelper.sh
+  . $(dirname $0)/includes/inc_bash.sh   | exit 1
 
-  . $(dirname $0)/jobhelper.sh
-  . $(dirname $0)/inc_bash.sh
-  . $(dirname $0)/vendor/ini.class.sh
   . $(dirname $0)/includes/dbdetect.class.sh
 
   # if [ -r ~/.backup.conf ]; then
diff --git a/restore.sh b/restore.sh
index 3b1ce4d..713adad 100755
--- a/restore.sh
+++ b/restore.sh
@@ -33,8 +33,8 @@
 
   # . `dirname $0`/inc_config.sh
 
-  . $(dirname $0)/jobhelper.sh
-  . $(dirname $0)/inc_bash.sh
+  . $(dirname $0)/includes/jobhelper.sh
+  . $(dirname $0)/includes/inc_bash.sh
 
   # --- load a transfer plugin
   STORAGE_BIN=$(_j_getvar ${STORAGEFILE} "bin")
diff --git a/transfer.sh b/transfer.sh
index dd0c900..9aa8335 100755
--- a/transfer.sh
+++ b/transfer.sh
@@ -48,8 +48,8 @@
 
   # . `dirname $0`/inc_config.sh
 
-  . $(dirname $0)/jobhelper.sh
-  . $(dirname $0)/inc_bash.sh
+  . $(dirname $0)/includes/jobhelper.sh
+  . $(dirname $0)/includes/inc_bash.sh
 
   typeset -i rc=0
   typeset -i doBackup=1
-- 
GitLab