#!/bin/bash
curlurl() {
	if [ $2 ]; then
		curl -s -H "Content-Type: application/json" -u asterisk:asterisk http://localhost:8088$1 -d `echo $2` > /dev/null
	else
		curl -s -u asterisk:asterisk http://localhost:8088$1 -X POST > /dev/null
	fi
}
handleStasisStart() {
    outsipchannelid=`uuidgen`
    local1channelid=`uuidgen`
    local2channelid=`uuidgen`
    body="""{\"endpoint\":\"pjsip/development\",\"channelId\":\"$outsipchannelid\",\"app\":\"blar\"}"""

    bodylocal="""{\"endpoint\":\"local/1234@local\",\"channelId\":\"$local1channelid\",\"otherChannelId\":\"$local2channelid\",\"app\":\"blar\"}"""
    bridge1id=`uuidgen`
    bridge2id=`uuidgen`
    channelId=`echo $event|jq .channel.id|tr -d \"`
    echo We got a new channel with ID=$channelId
    curlurl /ari/channels/create $bodylocal
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 1 $?


    curlurl /ari/channels/$local1channelid/dial
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 2 $?

    sleep 0.5

    curlurl /ari/channels/$local2channelid/answer
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 3 $?

    curlurl /ari/bridges {'"'bridgeId'"':'"'$bridge1id'"'}
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 4 $?

    curlurl /ari/bridges {'"'bridgeId'"':'"'$bridge2id'"'}
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 5 $?

    curlurl /ari/bridges/$bridge1id/addChannel {'"'channel'"':'"'$channelId'"'}
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 6 $?

    curlurl /ari/bridges/$bridge1id/addChannel {'"'channel'"':'"'$local1channelid'"'}
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 7 $?

    curlurl /ari/bridges/$bridge2id/addChannel {'"'channel'"':'"'$local2channelid'"'}
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 8 $?

    curlurl /ari/channels/create $body
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 9 $?

    curlurl /ari/bridges/$bridge2id/addChannel {'"'channel'"':'"'$outsipchannelid'"'}
    #curl -s localhost:8088/metrics > /dev/null
    #echo Doing Curl for Metrics 10 $?

    curlurl /ari/channels/$outsipchannelid/dial
    curl -s localhost:8088/metrics > /dev/null
    echo Doing Curl for Metrics 11 $?
}
handleStasisEnd() {
    tmpChannelId=`echo $event|jq .channel.id|tr -d \"`
    if [ $channelId == $tmpChannelId ]; then
        curl -s -u asterisk:asterisk http://localhost:8088/ari/bridges/$bridge1id -X DELETE
        curl -s -u asterisk:asterisk http://localhost:8088/ari/bridges/$bridge2id -X DELETE
        curl -s -u asterisk:asterisk http://localhost:8088/ari/channels/$outsipchannelid -X DELETE
        curl -s -u asterisk:asterisk http://localhost:8088/ari/channels/$local1channelid -X DELETE
    fi
}
handleChannelStateChange() {
    state=`echo $event |jq .channel.state|tr -d \"`
    tmpChannelId=`echo $event|jq .channel.id|tr -d \"`
    echo ChannelStateChanged for Channelid=$tmpChannelId state=$state
    if [ $channelId == $tmpChannelId ]; then
        echo Something changed for originating channel
    #elif [ $outsipchannelid == $tmpChannelId ]; then
    #    if [ $state == "Ringing" ]; then
    #        #curlurl /ari/channels/$channelId/ring
    #        #echo foo
    #    fi
    fi
}

wscat -c "ws://localhost:8088/ari/events?api_key=asterisk:asterisk&app=blar" |
    while read event; do
        #echo $event |jq .
        eventType=`echo $event|jq .type|tr -d \"`
        echo $eventType
        newCall=`echo $event|jq .channel.channelvars.NEWCALL|tr -d \"`
        if [ "$eventType" == "StasisStart" ]; then
            if [ "$newCall" == "true" ]; then
                handleStasisStart
            fi
        elif [ "$eventType" == "StasisEnd" ]; then
            if [ "$newCall" == "true" ]; then
                handleStasisEnd
            fi
        elif [ "$eventType" == "ChannelStateChange" ]; then
            handleChannelStateChange
        fi
    done
    exit 0
