function run () {

# - 'funcfile'  is the name of the file contains the function X, X_check, X_setenv,
# - 'functorun' is the function to run.
# We do not complain if we don't find a _check functions...


# If the first parameter is 'check' then we now that we don't have to complain, 
# because still there is some missing _check function, in this case the 
# second parameter is the function.

# And in this case 'requires' function should not exit, that's why we pass this parameter
# to the requires function.

        myquit=""
        if [ "x$1" = "xcheck" ]; then 
          funcfile=`echo $2 | sed -e 's/_setenv$//g' | sed -e 's/_check$//g'`
          functorun="$2"
          myquit="donotquitonerror"
        else
          funcfile=`echo $1 | sed -e 's/_setenv$//g' | sed -e 's/_check$//g'`
          functorun="$1"
        fi 

	### Check if the function is referrenced in any of the possible locations
        is_defined="0"

	for location in "$FUNCTIONS_DIR/" "$FUNCTIONS_DIR/local/" 
	do
	  is_defined=`/usr/bin/expr $is_defined + \`cat ${location}/${funcfile} 2>/dev/null |grep ${functorun}| wc -l\` `
	done

        if [ $is_defined -eq 0 ]; then
            yaimlog DEBUG "Skipping function: ${functorun} because it is not defined"
            if [ "xdonotquitonerror" = "x${myquit}" ] ; then
		return 127
            else
                return 1;
            fi
        fi
 
        yaimlog INFO "Executing function: ${functorun} "
        
        if [ -f $FUNCTIONS_DIR/local/$funcfile ]; then
            yaimlog INFO "Using locally defined function $FUNCTIONS_DIR/local/$funcfile"
            . $FUNCTIONS_DIR/local/$funcfile
        elif [ -f $FUNCTIONS_DIR/$funcfile ]; then
            . $FUNCTIONS_DIR/$funcfile
        elif ! [ "x$1" = "xcheck" ]; then
            yaimlog ERROR "Couldn't find a function file called $funcfile"
            return 1
        fi
        if [ -f $FUNCTIONS_DIR/pre/$funcfile ]; then
            yaimlog INFO "Executing pre script for $functorun"
            . $FUNCTIONS_DIR/pre/$funcfile
            ${functorun}_pre
        fi

        ${functorun} ${myquit}
        ret=$?

        if [  $ret -eq 1 ]; then
                yaimlog ERROR "Error during the execution of function: $functorun"
                return $ret
        fi
		#  After adding Nuno's check at the beginning this 'if' is not needed
        if [  $ret -eq 127 ]; then
                yaimlog DEBUG "The function: $functorun hasn't been executed, because not defined. It is OK, don't worry."
                return $ret
        fi
        if [ -f $FUNCTIONS_DIR/post/$funcfile ]; then
            yaimlog INFO "Executing post script for $functorun"
            . $FUNCTIONS_DIR/post/$funcfile
            ${functorun}_post
        fi     
        return $ret
}
