#!/bin/sh
PROVISION_FILE=$0
PARAM1=$1
#for a in "$@" ;do
#   PROVISION_FILE=$a
#done

echo " $PROVISION_FILE is the provision file"
# 1. Login
curl -c /tmp/semaphore-cookie -XPOST \
-H 'Content-Type: application/json' -H 'Accept: application/json' \
-d '{"auth": "admin", "password": "may12002"}' \
"http://172.16.4.90:3000/api/auth/login"

# 2. Get token
curl -b /tmp/semaphore-cookie \
-H 'Content-Type: application/json' -H 'Accept: application/json' \
"http://172.16.4.90:3000/api/user/tokens" > tokens.txt

# 2.1. Generate a new token & get it
curl -v -b /tmp/semaphore-cookie -XPOST \
-H 'Content-Type: application/json' -H 'Accept: application/json' \
"http://172.16.4.90:3000/api/user/tokens"

# 2.2. Get a new token
curl -v -b /tmp/semaphore-cookie \
-H 'Content-Type: application/json' -H 'Accept: application/json' \
"http://172.16.4.90:3000/api/user/tokens" > tokens.txt

# get the last one from array:
export TOKEN=`cat tokens.txt | jq .[-1].id | xargs echo`
echo "Obtained token: $TOKEN"

# DEBUG
curl \
-H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer $TOKEN" \
"http://172.16.4.90:3000/api/projects" -v
#curl -b /tmp/semaphore-cookie "http://172.16.4.90:3000/api/projects"

# 3. Pre-created project (playbooks are stored in git repository and configured through templates 7 variables)
# Sample is here https://gitlab.chiefsoft.net/MERMS/AnsibleTest
export PROJECT_ID=8

# 4. Lauch task
#curl -XPOST \
#-H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer '$TOKEN \
#-d '{"template_id": 1, "debug": false, "dry_run": false, "playbook": "/var/www/html/ANSIBLE/23607_devprov_mermsemr_com.yml", "environment": "{}"}' \
#"http://172.16.4.90:3000/api/project/$PROJECT_ID/tasks" > task.txt

curl -XPOST \
-H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer '$TOKEN \
-d '{"template_id": 1, "debug": false, "dry_run": false, "playbook": $PROVISION_FILE, "environment": "{}"}' \
"http://172.16.4.90:3000/api/project/$PROJECT_ID/tasks" > task.txt

export TASK_ID=`cat task.txt | jq .id | xargs echo`
echo "Task queued ID: $TASK_ID"

# 5. Check task status
# need to loop here until it is finished while it is "waiting" and it turns either to "success" or "error"
curl \
-H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer $TOKEN" \
"http://172.16.4.90:3000/api/project/$PROJECT_ID/tasks/$TASK_ID" > status.txt

export STATUS=`cat status.txt | jq .status | xargs echo`
echo "Task status is: $STATUS"

# 6. Get output
curl \
-H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer $TOKEN" \
"http://172.16.4.90:3000/api/project/$PROJECT_ID/tasks/$TASK_ID/output"

# 7. Expire token
curl -XDELETE \
-H 'Content-Type: application/json' -H 'Accept: application/json' -H "Authorization: Bearer $TOKEN" \
"http://172.16.4.90:3000/api/user/tokens/$TOKEN"


# 8. Logout
curl -c /tmp/semaphore-cookie -XPOST \
-H 'Content-Type: application/json' -H 'Accept: application/json' \
"http://172.16.4.90:3000/api/auth/logout"
