#!/bin/bash
################################################################################
# Filename: ./src/base/sbin/qrm-hooks
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
### ====================================================================== ###
##                                                                          ##
##  qrm-hooks                                                               ##
##                                                                          ##
##  QRM-script with hooks to the qrm                                        ##
##                                                                          ##
##                                                                          ##
### ====================================================================== ###

# exit status of the QRM-functions :
# 1 : unknown node
# 2 : could not update local config
# 3 : node not responding
# 4 : config file not found
# 5 : assignment locked
# 6 : error in usage
# 7 : kernel does not exists
# 8 : image does not exists
# 9 : node not responding but pingable by ping
# 10: node not responding + not pingable but replies to arping
# 11: node not responding  + offline (not responding to ping/arping
#     no netowrk IO -> maybe network error
# 12: node is not responding + offline (not responding to ping/arping
#     network IO ok, node offline, network ok

export QRM_SERVER_BASE_DIR=${QRM_SERVER_BASE_DIR:-`pushd $BASE_DIR/../.. > /dev/null; echo $PWD; popd > /dev/null`}
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-functions
QRM_LOG_FILE="$QRM_SERVER_LOG_DIR/qrm-hooks.log"
QRM_USER_HOOKS=$QRM_SERVER_BASE_DIR/qrm/hooks/qrm-user-hooks

function is_implemented() {
	declare -F $1
	return $?
}

if [ -e $QRM_USER_HOOKS ]; then
	. $QRM_USER_HOOKS
fi

##### General Functions ##

function assign_hook_internal(){
        echo "Assign hook function called"
}

function deassign_hook_internal(){
        echo "deassign hook function called"
}

function ve_start_hook_internal(){
        echo "ve start hook function called"
}

function ve_stop_hook_internal(){
        echo "ve stop hook function called"
}

function qrm_start_hook_internal(){
        echo "qrm start hook function called"
}

function qrm_stop_hook_internal(){
        echo "qrm stop hook function called"
}

function remove_node_hook_internal(){
        echo "remove node hook function called"
}

function new_node_hook_internal(){
        echo "new node hook function called"
}

function failover_hook_internal(){
        echo "failover hook function called"
}

function policy_recommend_hook_internal(){
        echo "policy recommend hook function called"
}

function node_error_hook_internal() {
	echo "node error hook called"
}

##Logic

#ENV=$1
#if [ "$ENV" != "" ]; then
#  export $ENV
#fi
#shift

HOOK=$1_hook
HOOK_INTERNAL=$1_hook_internal

qdebug "`echo calling "$@"` "

if is_implemented $HOOK_INTERNAL; then
	qdebug "`$HOOK_INTERNAL "$@" 2>&1`"
else
	qdebug "$HOOK_INTERNAL is not implemented"
fi

if is_implemented $HOOK; then
	qdebug "`$HOOK "$@" 2>&1`" &
else
	qdebug "user $HOOK not implemented"
fi

