#Get the key from serviceconfig.json
wfkey=$(jq ".services.workflow.key" ../deploy/lib/serviceconfig.json | sed 's/"//g')

#Get the protocol(http or https) from serviceconfig.json. This should be https.
wfhttp=$(jq ".services.workflow.certPath" ../deploy/lib/serviceconfig.json | sed 's/"//g')
if [ $wfhttp = "null" ]
then
	wfhttp="http"
else
	wfhttp="https"
fi
wfhost=$(jq ".services.workflow.hostname" ../deploy/lib/serviceconfig.json | sed 's/"//g')
wfport=$(jq ".services.workflow.port" ../deploy/lib/serviceconfig.json | sed 's/"//g')


#name of the plan is simply plan
planname=plan1

#/plans/:planname/status with payload status as stop  will stop the plan  if it's running. Needed to stop the plan before deleting
curl  -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X PUT --data "{\"status\":\"stop\"}" $wfhttp://$wfhost:$wfport/plans/$planname/status

# delete :planname in case it exists
curl  -k -H "workflow-api-key:$wfkey"  -H "Content-Type: application/json" -X DELETE  $wfhttp://$wfhost:$wfport/plans/$planname

#/plans/:planname will create an empty plan named :planname
curl  -k -H "workflow-api-key:$wfkey"  -H "Content-Type: application/json" -X POST --data "{\"priority\":100, \"interval\":100}" $wfhttp://$wfhost:$wfport/plans/$planname

#/plans/:plannamek/tasks/:taskname will add the :taskname under :planname
curl  -k -H "workflow-api-key:$wfkey"  -H "Content-Type: application/json" -X POST --data "{\"start\":true, \"url\":\"https://localhost:9600/task0\", \"func\":null, \"wait\":0, \"maxnuminvoke\":0, \"retry\":0, \"pipe\":false}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task0

#add another task - task1
curl   -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"url\":\"https://localhost:9600/task1\", \"func\":null, \"wait\":0, \"maxnuminvoke\":0, \"retry\":0, \"pipe\":true}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task1

#add another task - task2
curl   -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"url\":\"https://localhost:9600/task2\", \"func\":null, \"wait\":0, \"maxnuminvoke\":0, \"retry\":0, \"pipe\":true}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task2

#add another task - task4
curl   -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"url\":\"https://localhost:9600/task4\", \"func\":null, \"wait\":0, \"maxnuminvoke\":0, \"retry\":0, \"pipe\":true}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task4



#/plans/$planname/tasks/task1/synch synchronize task1 and 2 to execute if task0 fires and if task4 to execute if task1 & task2 fire and task4 
curl -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"pname\":\"task0\",\"op\":\"&\"}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task1/synch
curl -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"pname\":\"task0\",\"op\":\"&\"}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task2/synch
curl -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"pname\":\"task1\",\"op\":\"&\"}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task4/synch
curl -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"pname\":\"task2\",\"op\":\"&\"}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task4/synch


#/plans/:planname/status with payload status as start  will execute the plan
curl  -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X PUT --data "{\"status\":\"start\"}" $wfhttp://$wfhost:$wfport/plans/$planname/status

#get all  the plans. In this case there is only one plan since that's all that was added
curl -k -H "workflow-api-key:$wfkey" -X GET $wfhttp://$wfhost:$wfport/plans | jq
