#!/bin/bash
# $Id$ 
#----------------------------------------------------------------------------------------
# Title       : change_endpoints 							  |
# Author      : E.R.S Subramanian & N.Krishna Kumar  
# Modified    : Dhinesh Kumar M   				    |	
# Description : Replace the respective endpoints in trisulHubconfig.xml	                |
# Version     : 0.0.3		
# Date				: 07-May-2014			                |
# Usage       : ./change_endpoints							|

#----------------------------------------------------------------------------------------

red='\033[0;31m'
green='\033[0;32m'
blue='\033[0;34m'
yellow='\033[0;33m'
cyan='\033[0;36m'
# Clear the color after that
clear='\033[0m'
purple='\033[0;35m'  

usage(){
  echo -e $purple
  echo "Usage: $0 [ -d DOMAIN(default domain0) ] [ -n HUBNODE(default hub0) ]  [ -c CONTEXT(default context0) ] [-i IPADDERSS ] [-p PORT ]" 1>&2
  echo "Example   ./change_endpoints -d domain0 -n hub0 -c context0 -i 192.168.1.1 -p 12001"
  echo -e "for Help  ./change_endpoints -h\n"
  printf $clear
  exit 1
}

while getopts "d:n:c:i:p:h" options; do

  case "${options}" in
     d)
      DOMAIN="${OPTARG}"
      ;;
    n)
      HUB="${OPTARG}"
      ;;
    c)
      CONTEXT="${OPTARG}"
      ;;
    i)
      IP="${OPTARG}"
      ;;
    p)
      PORT="${OPTARG}"
      ;;
    h)
      usage
      exit 1
      ;;
    ?)
      echo "Invalid option: -${OPTARG}."
      exit 1
      ;;
    :)
      echo "Error: -"${OPTARG}" requires an argument."
      exit 1
      ;;
    *)
      exit 1
      ;;
  esac
done

if [[ -z "$DOMAIN" ]]; then
  DOMAIN="domain0"
fi

if [[ -z "$HUB" ]]; then
  HUB="hub0"
fi

#check for context name from command line
if [[  -z "$CONTEXT" ]]; then
  CONTEXT="context0"
elif [[ "$CONTEXT" == "default" || "$CONTEXT" == "context0" ]]; then
  CONTEXT="context0"
else
  CONTEXT="context_$CONTEXT"
fi





FILE="/usr/local/etc/trisul-hub/$DOMAIN/$HUB/$CONTEXT/trisulHubConfig.xml"
if [ ! -f $FILE ]; then
 echo -e $red"Hub Config file not found : $FILE "
 printf $clear
 exit 1	
fi

if [ -z  $IP ]; then
 echo -e $red"Requires IP Address "
 printf $clear
 usage
 exit 1   
fi


if [ -z  $PORT ]; then
 echo -e $red "Requires port number"

 printf $clear
 usage
 exit 1   
fi

cp $FILE /tmp/$DOMAIN-$HUB-$CONTEXT-trisulHubConfig.xml 




##############3 
# Display current endpoints for context 
echo -e "Current endpoints for context $CONTEXT" 
a=$(grep -o '</Connection>' $FILE | wc -l)
b=$(awk -v n=$a 'BEGIN{RS="</Connection>\n";ORS=RS} NR<=n' $FILE | sed -n '/<Connection>/,/<\/Connection>/p')
b=$(echo $b | grep -o -P '(?<=<Connection>).*(?=</Connection>)')
echo -e $cyan

echo $b | sed -e 's/<\/Connection>.*<Connection>\s*/\n/g'


# Displaying <ExtPublisherConnection> tag info
b=$(sed -e '/<ExtPublisherConnection>/,/<\/ExtPublisherConnection/!d' $FILE)
b=$(echo $b | grep -o -P '(?<=<ExtPublisherConnection>).*(?=</ExtPublisherConnection>)')
b=${b#" "}
echo $b


# Displaying <IntPublisherConnection> tag info
b=$(sed -e '/<IntPublisherConnection>/,/<\/IntPublisherConnection/!d' $FILE)
b=$(echo $b | grep -o -P '(?<=<IntPublisherConnection>).*(?=</IntPublisherConnection>)')
b=${b#" "}
echo $b


# Displaying <ZmqConnection> tag info
b=$(sed -e '/<ZmqConnection>/,/<\/ZmqConnection/!d' $FILE)
b=$(echo $b | grep -o -P '(?<=<ZmqConnection>).*(?=</ZmqConnection>)')
b=${b#" "}
echo $b
printf $clear

###################################################################################################################################################


i=0
sed -i -n '1h;1!H;${;g;s#<Connection>\n\+#<Connection>#g;p;}' $FILE
sed -i '/<Connection>/s/>.*</>somethingelse</' $FILE
sed -i '/<Connection>/s#>.*#>somethingelse</Connection>#' $FILE  
sed -i 's#^\s\+</Connection>$#\n#g#' $FILE

while grep "somethingelse" $FILE > /dev/null
do
	sed -i '0,/somethingelse/ s#somethingelse#tcp://'"$IP"':'"$((PORT+i))"'#' $FILE
	i=$(($i+1))
done

sed -i -n '1h;1!H;${;g;s#<ExtPublisherConnection>\n\+#<ExtPublisherConnection>#g;p;}' $FILE
sed -i '/<ExtPublisherConnection>/s#>.*<#>tcp://'"$IP"':'"$((PORT+i))"'<#' $FILE
sed -i '/<ExtPublisherConnection>/s#>.*#>tcp://'"$IP"':'"$((PORT+i))"'</ExtPublisherConnection>#' $FILE  
sed -i 's#^\s*</ExtPublisherConnection>$#\n#g#' $FILE

sed -i -n '1h;1!H;${;g;s#<IntPublisherConnection>\n\+#<IntPublisherConnection>#g;p;}' $FILE
sed -i '/<IntPublisherConnection>/s#>.*<#>tcp://'"$IP"':'"$((PORT+i+1))"'<#' $FILE
sed -i '/<IntPublisherConnection>/s#>.*#>tcp://'"$IP"':'"$((PORT+i+1))"'</IntPublisherConnection>#' $FILE  
sed -i 's#^\s*</IntPublisherConnection>$#\n#g#' $FILE

sed -i -n '1h;1!H;${;g;s#<ZmqConnection>\n\+#<ZmqConnection>#g;p;}' $FILE
sed -i '/<ZmqConnection>/s#>.*<#>tcp://'"$IP"':'"$((PORT+i+2))"'<#' $FILE
sed -i '/<ZmqConnection>/s#>.*#>tcp://'"$IP"':'"$((PORT+i+2))"'</ZmqConnection>#' $FILE  
sed -i 's#^\s\+</ZmqConnection>$#\n#g#' $FILE

echo -e $green '\nSuccessfully changed endpoints to\n'
printf $clear
echo -e $cyan
grep -i "connection" $FILE| sed -e 's/^\s*//' -e '/^$/d'
printf $clear

echo -e '\n'
