#!/bin/bash

function header() {
  echo
  echo
  echo $1
  echo --------------------------
}

port=${PORT:-43273}
base=http://localhost:$port
keydata=${KEYDATA:-"ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ=="}
localpath=$(which systemctl)
islocal=$?

seq=${SEQ:-$(date +%s)00}
gear=$seq
gear1=$gear

header "Create a container and expose a single port"
curl "$base/container/$gear" -X PUT -d '{"Image":"openshift/busybox-http-app", "Ports":[{"Internal":8080}],"Started":true}'
echo

seq=$[seq+1]

header "List the ports from the container"
curl "$base/container/$gear/ports"
echo

seq=$[seq+1]

header "Follow the logs of the container for 30 seconds"
curl "$base/container/$gear/log"
echo

header "Print status of the container over the API"
curl "$base/container/$gear/status"
echo

seq=$[seq+1]

if [ $islocal -eq 0 ]; then
  header "See the status of the container"
  systemctl status ctr-$gear
fi

seq=$[seq+1]
gear=$[gear+1]
gear2=$gear

header "Create a second container"
curl "$base/container/$gear" -H "Content-Type: application/json" -d '{"Image":"openshift/busybox-http-app"}' -X PUT
echo

seq=$[seq+1]

header "Stop the second container"
curl "$base/container/$gear/stopped" -X PUT
echo

seq=$[seq+1]

header "Start the second container"
curl "$base/container/$gear/started" -X PUT
echo

if [ $islocal -eq 0 ]; then
  header "See the gear slice"
  systemctl status gear.slice
fi

seq=$[seq+1]

header "Set an environment resource"
curl "$base/environment/1000" -X PUT  -H "Content-Type: application/json" -d '{"env":[{"name":"foo","value":"bar"}]}'
echo

seq=$[seq+1]

header "Patch that environment resource"
curl "$base/environment/1000" -X PATCH  -H "Content-Type: application/json" -d '{"env":[{"name":"baz","value":"boo"}]}'
echo

seq=$[seq+1]

header "Fetch environment resource"
curl "$base/environment/1000"
echo

seq=$[seq+1]

header "Add keys to both gears"
curl "$base/keys" -X PUT  -H "Content-Type: application/json" -d "{\"keys\":[{\"type\":\"authorized_keys\",\"value\":\"$keydata\"}],\"containers\":[{\"id\":\"$gear1\"},{\"id\":\"$gear2\"}]}"
echo

seq=$[seq+1]

header "Build an image"
curl "$base/build-image" -X POST -H "Content-Type: application/json" -d '{"BaseImage":"openshift/centos-ruby","Source":"http://github.com/openshift/sinatra-example.git","Tag":"mybuild-1"}'

seq=$[seq+1]
repo=${REPO:-$(date +%s)00}

header "Create an empty repository"
curl "$base/repository/$repo" -X PUT
echo

if [ $islocal -eq 0 ]; then
  header "See the job logs for the repo"
  journalctl --unit job-$repo --no-pager
  echo
fi

seq=$[seq+1]
repo=$[repo+1]

header "Create a cloned repository"
curl "$base/repository/$repo"  -H "Content-Type: application/json" -d '{"CloneUrl":"http://github.com/smarterclayton/docker-cartridge-examples"}' -X PUT
echo

if [ $islocal -eq 0 ]; then
  header "See the job logs for the repo"
  journalctl --unit job-$repo --no-pager
fi

echo
echo 'You can delete the artifacts generated by this script by stopping the daemon, running "gear purge",'
echo 'and then deleting /var/lib/containers/*'
