#!/bin/bash
type="$1"

broadcast_notification() {
    local message="$1"
    local room="$2"
    local data="{\"username\":\"DeployBot\",\"channel\":\"$room\",\"icon_emoji\":\":shipit:\",\"text\":\"$message\"}"
    curl -X POST -H 'Content-type: application/json' --data "$data" $SLACK_HOOK_URL
}

if [[ -z $SLACK_HOOK_URL ]]
then
    echo "SLACK_HOOK_URL not set, post to #changelog yourself"
else
    if [[ "$type" == "pre" ]]
    then
        broadcast_notification "about to deploy RCE service $CG_GIT_COMMIT_ID to $AWS_REGION $CG_ENVIRONMENT" "#canvas_deploys"
    elif [[ "$type" == "post" ]]
    then
        broadcast_notification "RCE service $CG_GIT_COMMIT_ID deployed successfully to $AWS_REGION $CG_ENVIRONMENT" "#canvas_deploys"
        if [[ "$AWS_REGION" == "us-east-1" ]]
        then
          broadcast_notification "Deployed Rich Content Service $CG_ENVIRONMENT revision $CG_GIT_COMMIT_ID to $AWS_REGION" "#changelog"
        fi
    else
        echo "I don't know how to send a notification of type $type"
    fi
fi
