#!/usr/bin/env bash

#
# Wrapper around SCN ( https://scn.blackforestbytes.com/ )
# ========================================================
#
# ./scn_send [@channel] title [content] [priority]
#
#
# Call with   scn_send              "${title}"
#        or   scn_send              "${title}" ${content}"
#        or   scn_send              "${title}" ${content}" "${priority:0|1|2}"
#        or   scn_send "@${channel} "${title}"
#        or   scn_send "@${channel} "${title}" ${content}"
#        or   scn_send "@${channel} "${title}" ${content}" "${priority:0|1|2}"
#
#

################################################################################
# INSERT YOUR DATA HERE                                                        #
################################################################################
user_id="999"
key="??"
################################################################################

usage() {
    echo "Usage: "
    echo "  scn_send [@channel] title [content] [priority]"
    echo ""
}

args=( "$@" )

title=$1
content=""
channel=""
priority=1
usr_msg_id="$(uuidgen)"
sendtime="$(date +%s)"
sender="$(hostname)"

if [ ${#args[@]} -lt 1 ]; then
    echo "[ERROR]: no title supplied via parameter" 1>&2
    usage
    exit 1
fi

if [[ "${args[0]}" =~ ^@.* ]]; then
	channel="${args[0]}"
    unset "args[0]"
	channel="${channel:1}"
fi

if [ ${#args[@]} -lt 1 ]; then
    echo "[ERROR]: no title supplied via parameter" 1>&2
    usage
    exit 1
fi

title="${args[0]}"
content=""

if [ ${#args[@]} -gt 1 ]; then
    content="${args[0]}"
    unset "args[0]"
fi

if [ ${#args[@]} -gt 1 ]; then
    priority="${args[0]}"
    unset "args[0]"
fi

if [ ${#args[@]} -gt 1 ]; then
        echo "Too many arguments to scn_send" 1>&2
        usage
        exit 1
fi


while true ; do

    curlresp=$(curl --silent                             \
                    --output /dev/null                   \
                    --write-out "%{http_code}"           \
                    --data "user_id=$user_id"            \
                    --data "key=$key"                    \
                    --data "title=$title"                \
                    --data "timestamp=$sendtime"         \
                    --data "content=$content"            \
                    --data "priority=$priority"          \
                    --data "msg_id=$usr_msg_id"          \
                    --data "channel=$channel"            \
                    --data "sender_name=$sender"         \
                    "https://scn.blackforestbytes.com/"  )

    if [ "$curlresp" == 200 ] ; then
        echo "Successfully send"
        exit 0
    fi

    if [ "$curlresp" == 400 ] ; then
        echo "Bad request - something went wrong" 1>&2
        exit 1
    fi

    if [ "$curlresp" == 401 ] ; then
        echo "Unauthorized - wrong userid/userkey" 1>&2
        exit 1
    fi

    if [ "$curlresp" == 403 ] ; then
        echo "Quota exceeded - wait one hour before re-try" 1>&2
        sleep 3600
    fi

    if [ "$curlresp" == 412 ] ; then
        echo "Precondition Failed - No device linked" 1>&2
        exit 1
    fi

    if [ "$curlresp" == 500 ] ; then
        echo "Internal server error - waiting for better times" 1>&2
        sleep 60
    fi

    # if none of the above matched we probably hav no network ...
    echo "Send failed (response code $curlresp) ... try again in 5s" 1>&2
    sleep 5
done