#!/bin/bash
# ====================================================================
#
#   P O C   *    Detect a database type
#
# --------------------------------------------------------------------
# ah: <www.axel-hahn.de>
# 2024-02-xx  v0.1  ah   Initial version
# --------------------------------------------------------------------

cd $( dirname $0 )

. vendor/ini.class.sh || exit 1
. $(dirname $0)/includes/dbdetect.class.sh

DBD_DEBUG=0

USAGE="Detect profiles for databases which are located in 
$DBD_BASEDIR.

For detected profiles it shows its used parameters.
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

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
"
# --------------------------------------------------------------------

# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

echo
echo " --==##|  DATABASE PROFILE DETECTOR  |##==-- "
echo

while [[ "$#" -gt 0 ]]; do case $1 in
    -d|--debug)     DBD_DEBUG=1;;
    -h|--help)      echo "$USAGE"; exit 0;;
    *) if grep "^-" <<< "$1" >/dev/null ; then
        echo; echo "ERROR: Unknown parameter: $1"; echo; _showHelp; exit 2
       fi
       break;
       ;;
esac; done

dbdetect._wd "------"
for config in $(dbdetect.getConfigs | grep "${1:-.}"); 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
    else
        echo "SKIP : $config" 
    fi
    dbdetect._wd
done
echo

# --------------------------------------------------------------------
