#!/usr/bin/env bash
 
initiator=""
parent=""
children=""
event=""
node_list=""
list=""
master_list=""
slave_list=""
synced_list=""
 
process_arguments()
{
   while [ "$1" != "" ]; do
      if [[ "$1" =~ ^--initiator=.* ]]; then
         initiator=${1#'--initiator='}
      elif [[ "$1" =~ ^--parent.* ]]; then
         parent=${1#'--parent='}
      elif [[ "$1" =~ ^--children.* ]]; then
         children=${1#'--children='}
      elif [[ "$1" =~ ^--event.* ]]; then
         event=${1#'--event='}
      elif [[ "$1" =~ ^--node_list.* ]]; then
         node_list=${1#'--node_list='}
      elif [[ "$1" =~ ^--list.* ]]; then
         list=${1#'--list='}
      elif [[ "$1" =~ ^--master_list.* ]]; then
         master_list=${1#'--master_list='}
      elif [[ "$1" =~ ^--slave_list.* ]]; then
         slave_list=${1#'--slave_list='}
      elif [[ "$1" =~ ^--synced_list.* ]]; then
         synced_list=${1#'--synced_list='}
      fi
      shift
   done
}
 
process_arguments $@
read -r -d '' MESSAGE << EOM
A server has changed state. The following information was provided:
 
Initiator: $initiator
Parent: $parent
Children: $children
Event: $event
Node list: $node_list
List: $list
Primary list: $master_list
Replica list: $slave_list
Synced list: $synced_list
EOM
 
# print message to file
echo "$MESSAGE" > /tmp/monitor_output.txt
 
initiator_ip=$(echo $initiator | sed 's/\[\(.*\)]:.*/\1/')
server_name=$(maxctrl list servers | grep $initiator_ip | sed 's/^│ \([A-Za-z\-]*\) │.*$/\1/')
 
echo "--------------"
echo $initiator_ip
echo $server_name
 
if [ $event = 'server_up' ] || [ $event = 'lost_slave' ]; then
   maxctrl call command mariadbmon async-rebuild-server MainMonitor $server_name
   echo "Server Up Event -- Rebuilding..."
fi