#!/bin/bash

if [ "$ALLGREADED" = "" ]; then
        
    ALLGREADED=1
    set -f
    
    declare -a params
    while [ $# -gt 0 ] ; do
      case $1 in
        -projectroot) projectroot=$2;   shift 2;;
            -project) project=$2;       shift 2;;
               -path) path=(${2//:/ }); shift 2;;
               -user) daemonuser=$2;    shift 2;;
              -group) daemongroup=$2;   shift 2;;
             -locale) deamonlocale=$2;  shift 2;;
                -va*) p=${1//\./_};
                      v="${2//\'/\'\\\'\'}"
                      eval "${p:1}='$v'";
                      i="${p:1}ignore"
                      if [ "${!i}" = "" ] && [[ ! "${p:1}" == *"passwd"* ]] && [[ ! "${p:1}" == *"password"* ]]; then
                        params[${#params[@]}]="${p:1}=$v"
                      else
                        params[${#params[@]}]="${p:1}=********"
                      fi
                      shift 2 ;;
                   *) shift 1 ;;
      esac
    done

   function readfile()
   {
     for (( i=${#path[@]}-1 ; i>=0 ; i-- ))
     do
       if [ -f "${path[$i]}/$1/$2" ]; then
      	 . "${path[$i]}/$1/$2";
      	 if [ "$3" != "all" ]; then
      	   return;
      	 fi
       fi
     done
   }

    readfile config config all
    readfile config functions all

    trap 'mne_error_handler "${BASH_SOURCE[*]}" $LINENO $?' ERR
    trap cleanup EXIT

    logstart

    if [ "$debug" != "" ]; then
      for i in "${params[@]}"; do log "$i"; done;
    fi
fi

if [ -f "$(dirname $0)/check" ]; then
    . $(dirname ${BASH_SOURCE[1]})/check
    mne_checksys
fi


