#!/bin/sh
#
#written by Josh Randall (joshua.randall@rsa.com)
#2MAY2019
#version 1
#
# Set Output Colors
RED=`tput setaf 1`
GRB=`tput setaf 2; tput bold`
YLW=`tput setaf 3`
BLB=`tput setaf 4; tput bold`
BLD=`tput bold`
RST=`tput sgr0`
#
#
while true; do

	#identify existing template
	read -p "${BLB}Enter The Name Of Your Notification Template ${YLW}(case sensitive...must match EXACTLY): ${RST}" TEMPLATE_NAME
	echo "${YLW}   Checking for your Notification Template. This may take a few seconds...${RST}"

	#get mongo password
	DEPLOY_PW=$(security-cli-client --get-config-prop --prop-hierarchy nw.security-client --prop-name platform.deployment.password --quiet)

	#check whether the template name entered matches an existing template
	templateAll=$(echo -e "use sa\ndb.template.find({name:\"$TEMPLATE_NAME\"})" | mongo admin -u deploy_admin -p $DEPLOY_PW --quiet)
	templateNameCheck=$(echo "$templateAll" | grep -Po "\"name\" : \".+?\"" | sed -e "s/\"name\" : //; s/\"//g")

	if [ -z "$templateNameCheck" ]; then
		echo "${RED}${BLD}   That does not appear to match an existing Notification Template Name. Please try again.${RST}"

	elif [[ $templateNameCheck == $TEMPLATE_NAME ]]; then

		#pull out the values from the existing template
		#for the new respond notification template
		echo "${YLW}   Found Existing Notification Template: ${GRB}${BLD}"$templateNameCheck${RST}
		templateId=$(echo "$templateAll" | grep -Po "ObjectId.+?\)")
		templateIdShort=$(echo "$templateAll" | grep -Po "ObjectId.+?\)" | sed -e "s/ObjectId(//; s/)//")
		templateDesc=$(echo "$templateAll" | grep -Po "\"description\" : \".+?\," | sed -e "s/\"description\" : //; s/\,//")
		templateText=$(echo "$templateAll" | grep -Po "\"text\" : \".+?\, \"readonly" | sed -e 's/\"text\" : //; s/, \"readonly//')
		templateType=$(echo "$templateAll" | grep -Po "\"templateType\" : \".+?\"" | sed -e "s/\"templateType\" : //")
		templateCreator=$(echo "$templateAll" | grep -Po "\"createdBy\" : \".+?\"" | sed -e "s/\"createdBy\" : //")
		timeStamp=$(date +%s%N | cut -b1-13)

		#mongo shell really has issues the HTML and Freemarker syntax
		#so we'll create a new mongo document with placeholder text
		#and then replace that placeholder text through nw-shell

		#first create the nw-shell script and json files
		echo -e 'connect --service integration-server\ncd template/set\ninvoke --file /tmp/newRespondTemplate.json' > /tmp/nwShellRespondTemplate.script
		echo "{\"id\" : $templateIdShort, \"name\" : \"$TEMPLATE_NAME\", \"description\" : $templateDesc, \"text\" : $templateText, \"type\" : $templateType}" > /tmp/newRespondTemplate.json

		#add a new mongo document or update fields in an existing one
		echo "${YLW}   Adding New Respond Notification Definition with Placeholder Template text...${RST}"
		echo -e "use integration-server\ndb.templateDefinition.update( {\"_id\": $templateId}, { \$set: {\"text\": \"placeholder text\", \"lastModifiedBy\": $templateCreator, \"lastModifiedOn\": NumberLong($timeStamp)}, \$setOnInsert: {\"name\": \"$TEMPLATE_NAME\", \"description\": $templateDesc, \"type\": $templateType, \"tags\": [], \"createdBy\": $templateCreator, \"createdOn\": NumberLong($timeStamp), \"_class\": \"com.rsa.asoc.integration.api.template.TemplateDefinition\" } }, {upsert: true} )" | mongo admin -u deploy_admin -p $DEPLOY_PW --quiet

		#now replace placeholder text with actual template text
		echo "${YLW}   Updating Notification Definition with Actual Template text...${RST}"
		nw-shell @/tmp/nwShellRespondTemplate.script

		#assign the new notification definition to either the incident created or incident updated trigger
		read -p "${BLB}Which Respond Notification is this New Template for? Incident ${YLW}${BLD}(Created / Updated) ${RST}" continue
		case $continue in
			Created )	echo -e "use integration-server\ndb.notificationBinding.update({\"_id\":\"incident-created\"},{\$set: {template: $templateIdShort, \"lastModifiedBy\": $templateCreator, \"lastModifiedOn\": NumberLong($timeStamp) } } )" | mongo admin -u deploy_admin -p $DEPLOY_PW --quiet;
						break;;

			Updated )	echo -e "use integration-server\ndb.notificationBinding.update({\"_id\":\"incident-state-changed\"},{\$set: {template: $templateIdShort, \"lastModifiedBy\": $templateCreator, \"lastModifiedOn\": NumberLong($timeStamp) } } )" | mongo admin -u deploy_admin -p $DEPLOY_PW --quiet;
						break;;

			* )			echo "${YLW}   Created or Updated, please${RST}";;
		esac
	else
		:
	fi
done
