#!/bin/sh

# exit on error
set -eo

# parse args
# https://unix.stackexchange.com/a/388038
while [ $# -gt 0 ]; do
  case "$1" in
    --songwhipEnv=*)
      songwhipEnv="${1#*=}"
      ;;
    --message=*)
      message="${1#*=}"
      ;;
    --url=*)
      url="${1#*=}"
      ;;
    *)
      printf "***************************\n"
      printf "* Error: Invalid argument.*\n"
      printf "***************************\n"
      exit 1
  esac
  shift
done

if [ "$songwhipEnv" != "staging" ] && [ "$songwhipEnv" != "production" ]; then
  echo 'invalid `--songwhipEnv`: must be "staging" or "production"'
  echo
  exit 1
fi

curl --request POST --silent \
  --url https://events.songwhip.com/ \
  --header 'Content-Type: application/json' \
  --data '{
	"context": {
		"env": "'$songwhipEnv'"
	},
	"event": {
    "type": "new-deployment",
    "serviceName": "songwhip-api",
    "message": "'"$message"'",
    "url": "'"$url"'"
	}
}'
