#!/bin/bash
#------------------------------------------------
# Author(s): Geaaru, geaaru@gmail.com
# Created: July 22, 2008
# $Id: 90e79fdfa0e52db60e0e9a10cf740dc13842f0d2 $
# License: GPL 2.0
#------------------------------------------------
VERSION="0.1.1"

prefix=/usr
CONFIG_FILE=${prefix}/etc/dbrm.conf
DBRM_CORE_FILES_DIR=${prefix}/share/dbrm/core
CONFIG_FILE_STUB=${prefix}/share/dbrm/dbrm.conf.example

# Insert config file data
if [ -f $CONFIG_FILE ] ; then
  . $CONFIG_FILE
fi

# If already present OVERRIDE_LOCAL_CONFIG_FILE
# variable i use environment variable.
if [[ ! -z $OVERRIDE_LOCAL_CONFIG_FILE && -e $OVERRIDE_LOCAL_CONFIG_FILE ]] ; then
  . $OVERRIDE_LOCAL_CONFIG_FILE
  [[ $DEBUG ]] && echo -en "dbrm: Override LOCAL_CONFIG_FILE with: $OVERRIDE_LOCAL_CONFIG_FILE\n"

else

  # Check if present a dbrm.conf file in current directory
  # I use this file for import configurations fields.
  # I ignore $LOCAL_DIR and $LOCAL_CONFIG_FILE
  if [ -e ./dbrm.conf ] ; then

    . ./dbrm.conf

    [[ $DEBUG ]] && echo -en "dbrm: I use local file ./dbrm.conf\n"

  else

    # Check if exists local directory
    if [[ ! -d $LOCAL_DIR ]] ; then
      echo -en "Create local DRM directory.\n"
      mkdir -p $LOCAL_DIR

    fi

    # If LOCAL_CONFIG_FILE is not defined
    # use only main configuration file ${prefix}/etc/dbrm.conf.
    if [[ -n $LOCAL_CONFIG_FILE ]] ; then

      if [[ ! -e $LOCAL_CONFIG_FILE ]] ; then
        cp $CONFIG_FILE_STUB $LOCAL_CONFIG_FILE
      fi

      . $LOCAL_CONFIG_FILE

    fi

  fi

# end else
fi

# Use . directory if MODULES_DIR isn't defined
if [ -z $MODULES_DIR ] ; then
   MODULES_DIR="."
fi

# Load core scripts
for f in $DBRM_CORE_FILES_DIR/*.sh ; do
  . $f
done

#######################################################
# Functions                                           #
#######################################################

show_help () {
  local i=""

  for i in $modules ; do
    if type -t ${i}_show_help > /dev/null ; then
      ${i}_show_help
    fi
  done
}

load_profile () {

  _dbm_get_default_profile_filepath

  if [ -n "$DRM_PROFILE_FILE" ] ; then
    . $DRM_PROFILE_FILE
  fi

}

# return 0 ok
# return 1 error or help message
check_cmdline_args () {
   local normal_help=1
   local i=""
   local x=""

   if [[ $# -eq 0 || $# -lt 2 ]] ; then
      if [ x$1 == x"-v" ] ; then
         echo -en "Database Release Manager - version $VERSION\n"
         return 0
      fi

      if [[ $# -eq 1 ]] ; then

        if [[ $1 =~ .*^-.* ]] ; then
          if [[ $1 = "-l" ]] ; then
            normal_help=0
            echo -en "Availables modules:\n"
            for m in $modules ; do
              echo -en "- $m\n"
            done
          fi
        else
          if type -t $1_show_help > /dev/null ; then
            normal_help=0
            $1_show_help
          fi
        fi
      fi

      if [ $normal_help -eq 1 ] ; then
        show_help
      fi

      return 1
   fi

   for i in $modules ; do
      if [ x"$i" == x"$1" ] ; then
         if type -t ${i}_${2} > /dev/null ; then

           if [[ -n $DRM_PROFILE && x$DRM_PROFILE == x1 ]] ; then

             load_profile

           fi

            ${i}_${2} "$@"
            return 0
         else
            echo -en "Command $2 of module $1 not founded\n"
            return 1;
         fi
      fi
   done

   echo -en "module $1 not founded\n"
   return 1
}

main () {
   local result=1
   local version=""
   local mod=""

   # Load modules
   for mod in $MODULES_DIR/*.mod ; do
      . $mod
      if type -t _${name}_init > /dev/null ; then

         [[ $DEBUG && $DEBUG == true ]] && echo "Call init method of the module $name"
         _${name}_init "$@"
      fi
      if type -t ${name}_version > /dev/null ; then
        version=`${name}_version`
      else
        version="N.A."
      fi
      [[ $DEBUG && $DEBUG == true ]] && echo "Loaded module $name $version"
      modules="$modules $name"
   done

   # Export modules variable so every modules can known
   # what modules are installed.
   export modules

   # Call post_init function of all modules
   for name in $modules ; do
     if type -t _${name}_post_init > /dev/null ; then

         [[ $DEBUG ]] && echo "Call post_init method of the module $name"
         _${name}_post_init "$@"

     fi
   done

   check_cmdline_args "$@"
   result=$?

   # Check if there is a deinit method
   for i in $modules ; do
      if type -t _${i}_deinit > /dev/null ; then
         [[ $DEBUG ]] && echo "Call deinit method of the module $i"
         _${i}_deinit
      fi
   done


   exit $result
}

main "$@"

# vim: syn=sh filetype=sh ts=2 sw=2
