#!/bin/bash 

function Send_Message_To_Queue {
	rmq_client_executable=$1
	rmq_config_file=$2
	content=$3
	queue=$4

	#Parse Config
	hostname=$(cat "$rmq_config_file" | tr ',' '\n' | sed -nE 's|^.*"hostname":.*"(.*)"|\1|p')
	port=$(cat "$rmq_config_file" | tr ',' '\n' | sed -nE 's|^.*"port":.*"(.*)"|\1|p')
	vhost=$(cat "$rmq_config_file" | tr ',' '\n' | sed -nE 's|^.*"vhost":.*"(.*)"|\1|p')

	# Generate send file
	rnd=$(echo $RANDOM | md5sum | head -c 6)
	send_file="/tmp/send-${rnd}.json"
	echo "$content" > "$send_file"

	# Send message
	$rmq_client_executable -h "$hostname" -p "$port" -v "$vhost" -e "$queue" -r "$queue" -f "$send_file"
	echo $?

	if [ -f "$send_file" ]
	then
		rm "$send_file"
	fi
}
