#!/bin/sh

set -f
. alterator-sh-functions
. /usr/share/alterator/build/backend3.sh

DEBUG=1

debug()
{
    if [ "$DEBUG" = "1" ]; then
        echo -e "$@" >> /root/debug
    fi
}

config_file=/etc/ha.d/ha.cf
auth_file=/etc/ha.d/authkeys

resource_exists()
{
    new_resource="$1"
    /usr/lib/alterator-heartbeat/list-resources.sh 2> /dev/null | while read resource def; do
        if [ "$new_resource" = "$resource" ]; then
            echo "exists"
        fi
    done
}

_()
{
LANG=${in_language%%;*}.utf8 gettext "alterator-heartbeat" "$1"
}


on_message()
{
	#debug "\n\n$(set | grep '^in_')"
	case "$in_action" in
		constraints)
			echo '('
			if [ x"$in__objects" = x"/" ]; then
			    case "$in_orig_action" in
				add)
                                    printf ' resource_name (required #t match ("^[a-zA-Z0-9_-]+$" "%s") label "%s")' \
                                        "`_ "Allowed only alpha, numbers, _ and -"`" "`_ "Resource name"`"
				    ;;
				config)
				    # FIXME: add ucast_ip, mcast_settings constraints to this section and to *)
				    echo > /dev/null
				    ;;
				general)
				    printf ' service_state (default #f label "%s")' \
					"`_ "Enable heartbeat service"`"
				    # FIXME: add regexp for check spaces in key
				    printf ' key1 (equal key2 label "%s")' \
					"`_ "Enter new key"`"
				    printf ' key2 (label "%s")' \
					"`_ "Repeat new key"`"
				    printf ' local_node (label "%s")' \
					"`_ "Local node hostname:"`"
				    printf ' bcast_interfaces (label "%s")' \
					"`_ "Broadcast interfaces:"`"
				    printf ' ucast_ip (label "%s")' \
					"`_ "Ip address of remote node (optional):"`"
				    printf ' mcast_settings (label "%s")' \
					"`_ "Multicast settings (optional):"`"
				    ;;
				*)
				    printf ' service_state (default #f label "%s")' \
					"`_ "Enable heartbeat service"`"
				    # FIXME: add regexp for check spaces in key
				    printf ' key1 (equal key2 label "%s")' \
					"`_ "Enter new key"`"
				    printf ' key2 (label "%s")' \
					"`_ "Repeat new key"`"
                                    printf ' resource_name (required #t match ("^[a-zA-Z0-9_-]+$" "%s") label "%s")' \
                                        "`_ "Allowed only alpha, numbers, _ and -"`" "`_ "Resource name"`"
				    printf ' resource_type (label "%s")' \
					"`_ "Resource type"`"
				    printf ' resource_status (label "%s")' \
					"`_ "Status"`"
				    printf ' local_node (label "%s")' \
					"`_ "Local node hostname:"`"
				    printf ' bcast_interfaces (label "%s")' \
					"`_ "Broadcast interfaces:"`"
				    printf ' ucast_ip (label "%s")' \
					"`_ "Ip address of remote node (optional):"`"
				    printf ' mcast_settings (label "%s")' \
					"`_ "Multicast settings (optional):"`"
				    printf ' node_name (label "%s")' \
					"`_ "Node name"`"
				    printf ' node_status (label "%s")' \
					"`_ "Node status"`"
				    ;;
			    esac
			else
			    local resource_type="${in__objects%/*}"
			    resource_type="${resource_type##*/}"
                            printf ' resource_name (label "%s")' \
                                 "`_ "Resource name"`"
			    case "$resource_type" in
				IPaddr)
				    printf ' ip (required #t label "%s")' \
					"`_ "IP address"`"
				    ;;
				Openldap)
				    printf ' base (required #t label "%s")' \
					"`_ "Openldap base"`"
				    ;;
				*)
				    ;;
			    esac

			
			fi
			echo ')'
			;;
		list)
			echo '('	
			case "${in__objects##*/}" in
			    resource_type_list)
				printf '("%s")' "IPaddr"
				printf '("%s")' "Openldap"
				;;
			    nodes_list)
				local node_name node_status node_status_translated
				/usr/lib/alterator-heartbeat/list-nodes.sh 2> /dev/null | while read node_name node_status; do
				    case "$node_status" in
					Up)
					    node_status_translated="`_ "Up"`"
					    ;;
					Down)
					    node_status_translated="`_ "Down"`"
					    ;;
				    esac
				    printf '("%s" node_name "%s" node_status "%s")' \
					"$node_name" "$node_name" "$node_status_translated"
				done
				;;
			    *)
				local resource resource_type resource_type_primitive resource_status status host
				/usr/lib/alterator-heartbeat/list-resources.sh 2> /dev/null | while read resource resource_type; do
				    resource_status=
				    status=
				    host=
				    if [ "$resource_type" = "master_slave" ]; then
					resource_type="Master/Slave"
					resource_type_primitive=$(/usr/lib/alterator-heartbeat/get-resource-type.sh "${resource}_clone:0")
				    else
					resource_type_primitive="$resource_type"
					resource_status=$(/usr/lib/alterator-heartbeat/get-resource-status.sh "$resource")
					status=${resource_status%\ *}
					host=${resource_status#*\ }
					case "$status" in
					    Started)
						resource_status="`_ "Started on"`"
						;;
					    Master)
						resource_status="`_ "Master on"`"
						;;
					    Stopped)
						resource_status="`_ "Stopped"`"
						;;
					    *)
						resource_status="`_ "Error"`"
						;;
					esac
				    fi
				    printf '("%s/%s" resource_name "%s" resource_type "%s" resource_status "%s %s")' "$resource_type_primitive" "$resource" "$resource" "$resource_type" "$resource_status" "$host"
				done
				;;
			esac
			echo ')'
			;;
		read)
			if [ x"$in__objects" = x"/" ]; then
			    
			    local args="--bcast-interfaces auto --autojoin other"
			    # Generate config if doesn't exit
			    [ -f "$config_file" ] ||
				eval "/usr/lib/alterator-heartbeat/configure-heartbeat-config.sh $args" > "$config_file"
			    # Checking for state of service, works for non-xinetd services
			    local service="#f"
			    LANG=C chkconfig heartbeat --list | grep -qs "$(runlevel | cut -d ' ' -f 2):on" && service="#t"
			    
			    echo '('
			    printf ' service_state %s' "$service"
			    printf ' local_node %s' "$(uname -n)"
			    printf ' bcast_interfaces "%s"' "$(/usr/lib/alterator-heartbeat/get-heartbeat-config-param.sh --file /etc/ha.d/ha.cf --param bcast-interfaces 2> /dev/null)"
			    printf ' ucast_ip "%s"' "$(/usr/lib/alterator-heartbeat/get-heartbeat-config-param.sh --file /etc/ha.d/ha.cf --param ucast-ip 2> /dev/null)"
			    printf ' mcast_settings "%s"' "$(/usr/lib/alterator-heartbeat/get-heartbeat-config-param.sh --file /etc/ha.d/ha.cf --param mcast-settings 2> /dev/null)"
			    # FIXME: change warning color to red
			    local key_warning=""
			    /usr/lib/alterator-heartbeat/wait-for-cluster-ready.sh || \
				key_warning="`_ "Warning: cluster is not ready: not started or CIB is broken"`"
			    [ -f "$auth_file" ] || key_warning="`_ "Warning: cluster key not found"`"
			    printf ' key_warning "%s"' "$key_warning"
			    echo ')'
			else
			    local resource_name="${in__objects##*/}"
			    local resource_type="${in__objects%/*}"
			    resource_type="${resource_type##*/}"
			    echo '('
			    printf ' resource_name "%s"' "$resource_name"
			    case "$resource_type" in
				IPaddr)
				    local ip="$(/usr/lib/alterator-heartbeat/ipaddr-get-ip.sh $resource_name 2> /dev/null)"
				    printf ' ip "%s"' "$ip"
				    ;;
				Openldap)
				    local base="$(/usr/lib/alterator-heartbeat/openldap-get-base.sh $resource_name 2> /dev/null)"
				    printf ' base "%s"' "$base"
				    ;;
				*)
				    ;;
			    esac
			    echo ')'
			fi
			;;
		general)
                	if test_bool "$in_service_state"; then
                    	    chkconfig heartbeat on
			else
			    chkconfig heartbeat off
		        fi
			if [ -n "$in_key1" ]; then
			    [ -f "$auth_file" ] && mv -f "$auth_file" "$auth_file".bak
			    rm -f "$auth_file"
			    touch "$auth_file"
			    chmod 600 "$auth_file"
			    echo "auth 1" > "$auth_file"
			    echo "1 sha1 $in_key1" >> "$auth_file"
			    /usr/lib/alterator-heartbeat/wait-for-cluster-ready.sh && service heartbeat reload &> /dev/null
			fi
			write_nop
			;;
		config)
			local args="--bcast-interfaces auto --autojoin other"
			if [ -n "$in_ucast_ip" ]; then
			    args="$args --ucast-ip '$in_ucast_ip'"
			fi
			if [ -n "$in_mcast_settings" ]; then
			    args="$args --mcast-settings '$in_mcast_settings'"
			fi
			if [ -n "$in_bcast_interfaces" ]; then
			    args="$args --bcast-interfaces '$in_bcast_interfaces'"
			fi
			[ -f "$config_file" ] && mv -f "$config_file" "$config_file".bak
			eval "/usr/lib/alterator-heartbeat/configure-heartbeat-config.sh $args" > "$config_file"
			/usr/lib/alterator-heartbeat/wait-for-cluster-ready.sh && service heartbeat restart &> /dev/null
			write_nop
			;;
		write)
			write_nop
			;;
		edit-ipaddr)
			local resource_name="${in__objects##*/}"
			if [ x$(resource_exists "$resource_name") = x"exists" ]; then
			    /usr/lib/alterator-heartbeat/ipaddr-change-ip.sh "$resource_name" "$in_ip"
			else
			    /usr/lib/alterator-heartbeat/ipaddr-add.sh "$resource_name" "$in_ip"
			fi
			write_nop
			;;
		edit-openldap)
			local resource_name="${in__objects##*/}"
			if [ x$(resource_exists "$resource_name") = x"exists" ]; then
			     echo "$resource_name" | grep -qs ':' || \
			        /usr/lib/alterator-heartbeat/openldap-change-base.sh "$resource_name" "$in_base"
			else
			    /usr/lib/alterator-heartbeat/openldap-add.sh "$resource_name" "$in_base"
			fi
			write_nop
			;;
		add)
			echo '('
			/usr/lib/alterator-heartbeat/wait-for-cluster-ready.sh
			if [ $? -eq 0 ]; then
			    local resource
			    crm_resource -L | while read resource def; do
				if [ "$in_resource_name" = "$resource" ]; then
				    printf ' error "%s: %s"' \
					"$in_resource_name" "`_ "resource already exists"`"
				fi
			    done
			else
			    printf ' error "%s"' \
				"`_ "Error: cluster is not ready: not started or CIB is broken"`"
			fi
			echo ')'
			;;
			
		delete)
			if [ x"$in_delete" = x"#t" ]; then
			    local resource_name="${in__objects##*/}"
			    echo "$resource_name" | grep -qs ':' || \
				/usr/lib/alterator-heartbeat/remove-resource.sh "$resource_name" &> /dev/null
			fi
			write_nop
			;;
		*)
			echo '#f'
			;;
	esac
}

message_loop
