#!/bin/sh

#   check usage
if [ $# -ne 3 ]; then
    echo "mattermost-post: ERROR: invalid number of arguments" 1>&2
    echo "mattermost-post: USAGE: $0 <access-token> <channel-id> <message>" 1>&2
    exit 1
fi

#   take over arguments
token="$1"
channel="$2"
message="$3"

#   determine REST endpoint URL
endpoint=`grep SiteURL @l_prefix@/etc/mattermost/mattermost.json | \
    sed -e 's;,*"SiteURL"[^"]*"\([^"]*\)".*;;'`
endpoint="$endpoint/api/v4/posts"

#   post message
@l_prefix@/bin/curl \
    -i \
    -X POST \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $token" \
    -d "{ \"channel_id\": \"$channel\", \"message\": \"$message\" }" \
    $endpoint

