#!/bin/bash
# Version("2.2.0.1")
# InformationalVersion("2.2.0.1")


REPSRC=`expr "$PATH_INFO" : '.*/\(.*.\)/.*/'`
REPDST=`expr "$PATH_INFO" : '.*/.*/\(.*.\)/'`

# Functions

function send_404 {
	echo -e "HTTP/1.1 404 Not Found\r"
	echo -e "Content-Type: text/html\r"
	echo -e "\r"
	echo -e "<html><head><title>404 Not Found</title></head><body>\r"
	echo -e "<h1>404 Not Found</h1>\r"
	echo -e "The requested failover options was not found.\r"
	echo -e "</body></html>\r"
	echo -e "\r"
	
}

function send_403 {
	echo -e "HTTP/1.1 403 Forbidden\r"
	echo -e "Content-Type: text/html\r"
	echo -e "\r"
	echo -e "<html><head><title>403 Forbidden</title></head><body>\r"
	echo -e "<h1>403 Forbidden</h1>\r"
	echo -e "<p>The requested failover option is not available.\r</p>"
	#echo -e "<p>$PATH_INFO - $REPSRC - $REPDST</p>"
	#echo -e "<p>$QUERY_STRING</p>"
	echo -e "</body></html>\r"
	echo -e "\r"
	exit 0
}




function handle_replace_sip {

	echo -e "HTTP/1.1 200 OK\r"
	echo -e "Content-Type: text/plain\r"
	echo -e "\r"
	sudo /usr/bin/perl -pi -e 's/'$REPSRC'/'$REPDST'/g' /etc/asterisk/sip.conf 
	echo -e "\r"
	exit 0
}


# Main Routine

if [ $QUERY_STRING = "sip" ]
then
	handle_replace_sip
fi

# No replace action found

send_403
exit 0

