#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
# 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\":0}" $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

#/plans/:planname/tasks/:ptask/nexttasks will insert task1 as child task of task0. This means task1 will execute after task0 is completed
curl  -k -H "workflow-api-key:$wfkey" -H "Content-Type: application/json" -X POST --data "{\"name\":\"task1\"}" $wfhttp://$wfhost:$wfport/plans/$planname/tasks/task0/nexttasks

#/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


