# config section - change as needed
HOST=localhost
PORT=8080

KIE_USER=rhpamAdmin
KIE_PWD=password1!

PROJECT_GROUP=com.redhat.cee
PROJECT_ARTIFACT=wih-project
PROJECT_ID=1.0
PROJECT_ID2=1.1

PROCESS_NAME=wih.process
RUNTIME_STRATEGY=PER_PROCESS_INSTANCE

# no need to change here
KIE_CRED=$KIE_USER:$KIE_PWD
PROJECT_GAV=$PROJECT_GROUP:$PROJECT_ARTIFACT:$PROJECT_ID
PROJECT_GAV2=$PROJECT_GROUP:$PROJECT_ARTIFACT:$PROJECT_ID2

if [ -z "$1" ]
  then
    echo Running script without redeploying the container
else
	# build the kjars (versions 1.0 and 1.1)
	(cd ${PROJECT_ARTIFACT} && git checkout branch1_0 && mvn clean install -Dmaven.compiler.target=1.8 -Dmaven.compiler.source=1.8)
	(cd ${PROJECT_ARTIFACT} && git checkout branch1_1 && mvn clean install -Dmaven.compiler.target=1.8 -Dmaven.compiler.source=1.8)

	# Delete containers
	curl -v -X DELETE -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/containers/${PROJECT_GAV}
	curl -v -X DELETE -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/containers/${PROJECT_GAV2}


	# Create container template:
	echo '<script>' > create-container.xml
	echo '  <create-container>' >> create-container.xml
	echo '    <container container-id="'${PROJECT_GAV}'">' >> create-container.xml
	echo '      <release-id>' >> create-container.xml
	echo '        <group-id>'${PROJECT_GROUP}'</group-id>' >> create-container.xml
	echo '        <artifact-id>'${PROJECT_ARTIFACT}'</artifact-id>' >> create-container.xml
	echo '        <version>'${PROJECT_ID}'</version>' >> create-container.xml
	echo '      </release-id>' >> create-container.xml
	echo '      <config-items>' >> create-container.xml
	echo '        <itemName>RuntimeStrategy</itemName>' >> create-container.xml
	echo '        <itemValue>'${RUNTIME_STRATEGY}'</itemValue>' >> create-container.xml
	echo '        <itemType></itemType>' >> create-container.xml
	echo '      </config-items>' >> create-container.xml
	echo '    </container>' >> create-container.xml
	echo '  </create-container>' >> create-container.xml
	echo '</script>' >> create-container.xml

	# Create container #1:
	curl -v -X POST -H 'Content-type: application/xml' -H 'X-KIE-Content-Type: xstream' -d @create-container.xml -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/config/

	# Create container template:
	echo '<script>' > create-container.xml
	echo '  <create-container>' >> create-container.xml
	echo '    <container container-id="'${PROJECT_GAV2}'">' >> create-container.xml
	echo '      <release-id>' >> create-container.xml
	echo '        <group-id>'${PROJECT_GROUP}'</group-id>' >> create-container.xml
	echo '        <artifact-id>'${PROJECT_ARTIFACT}'</artifact-id>' >> create-container.xml
	echo '        <version>'${PROJECT_ID2}'</version>' >> create-container.xml
	echo '      </release-id>' >> create-container.xml
	echo '      <config-items>' >> create-container.xml
	echo '        <itemName>RuntimeStrategy</itemName>' >> create-container.xml
	echo '        <itemValue>'${RUNTIME_STRATEGY}'</itemValue>' >> create-container.xml
	echo '        <itemType></itemType>' >> create-container.xml
	echo '      </config-items>' >> create-container.xml
	echo '    </container>' >> create-container.xml
	echo '  </create-container>' >> create-container.xml
	echo '</script>' >> create-container.xml

	# Create container #2:
	curl -v -X POST -H 'Content-type: application/xml' -H 'X-KIE-Content-Type: xstream' -d @create-container.xml -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/config/
fi

# create process instance 
PROCID=`curl -s -X POST -H 'Content-type: application/json' -H 'X-KIE-Content-Type: json' -d '{}' -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/containers/${PROJECT_GAV}/processes/${PROCESS_NAME}/instances` && echo Process ${PROCID} created for process ${PROCESS_NAME} 

# short wait before querying the async job id
sleep 2

# get the failing job id
echo "curl -s -X GET -H 'Content-type: application/json' -H 'accept: application/json' -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/jobs/containers/${PROJECT_GAV}?status=ERROR"
JOBID=`curl -s -X GET -H 'Content-type: application/json' -H 'accept: application/json' -u ${KIE_CRED} http://${HOST}:${PORT}/kie-server/services/rest/server/jobs/containers/${PROJECT_GAV}?status=ERROR|grep -m 1 'request-instance-id'|cut -d ':' -f 2|cut -d ',' -f 1|xargs`  && echo Job ${JOBID} in state ERROR

echo ""
read -n1 -r -p "Press space to migrate process instance now..." key

# migrate process instance
curl -X PUT -u ${KIE_CRED} -H "accept: application/json" -H "content-type: application/json" -d "{}" "http://${HOST}:${PORT}/kie-server/services/rest/server/admin/containers/${PROJECT_GAV}/processes/instances/${PROCID}?targetContainerId=${PROJECT_GAV2}&targetProcessId=${PROCESS_NAME}"

echo ""
read -n1 -r -p "Press space to create new job..." key

# get info about the job
SIGNAL_NAME=`curl -s -X GET -u ${KIE_CRED} "http://${HOST}:${PORT}/kie-server/services/rest/server/jobs/${JOBID}?withErrors=false&withData=true" -H "accept: application/json"|grep '"Signal"'|cut -d ':' -f 2|cut -d ',' -f 1|xargs` && echo Creating new job with signal name ${SIGNAL_NAME}

curl -X POST -u ${KIE_CRED} "http://${HOST}:${PORT}/kie-server/services/rest/server/jobs?containerId=${PROJECT_GAV2}" -H "accept: application/json" -H "content-type: application/json" -d "{ \"job-command\" : \"org.jbpm.process.core.async.AsyncSignalEventCommand\", \"scheduled-date\" : { \"java.util.Date\" : 1540023411655 }, \"request-data\": { \"processInstanceId\": ${PROCID}, \"Signal\": \"${SIGNAL_NAME}\", \"deploymentId\": \"${PROJECT_GAV2}\", \"Event\": null }}"
