#!/bin/sh

# Check if the configuration is valid. Based on the makefile
# target 'checkconfig'

ASTETCDIR=${ASTETCDIR:-/etc/asterisk}

echo " --- Checking Asterisk configuration to see if it will support the GUI ---"
echo -n "* Checking for http.conf: "
if [ -f ${ASTETCDIR}/http.conf ]; then 
	echo "OK" ; 
else 
	echo "FAILED"; 
	echo " -- Please run 'make samples' in *Asterisk* or " ; 
	echo " -- create your own ${ASTETCDIR}/http.conf" ; 
	exit 1; 
fi
echo -n "* Checking for manager.conf: "
if [ -f ${ASTETCDIR}/manager.conf ]; then 
	echo "OK" ; 
else 
	echo "FAILED"; 
	echo " -- Please run 'make samples' in *Asterisk* or " ; 
	echo " -- create your own ${ASTETCDIR}/manager.conf" ; 
	exit 1; 
fi
echo -n "* Checking if HTTP is enabled: "
if grep -v '^;' ${ASTETCDIR}/http.conf | grep enabled | grep -q yes 2>/dev/null; then 
	echo "OK" ; 
else 
	echo "FAILED"; 
	echo " -- Please be sure you have 'enabled = yes'" ; 
	echo " -- in ${ASTETCDIR}/http.conf" ; 
	exit 1; 
fi
echo -n "* Checking if HTTP static support is enabled: "
if grep -v '^;' ${ASTETCDIR}/http.conf | grep enablestatic | grep -q yes 2>/dev/null; then 
	echo "OK" ; 
else 
	echo "FAILED"; 
	echo " -- Please be sure you have 'enablestatic = yes'" ; 
	echo " -- in ${ASTETCDIR}/http.conf" ; 
	exit 1; 
fi
echo -n "* Checking if manager is enabled: "
if grep -v '^;' ${ASTETCDIR}/manager.conf | grep ^enabled | grep -q yes 2>/dev/null; then 
	echo "OK" ; 
else 
	echo "FAILED"; 
	echo " -- Please be sure you have 'enabled = yes'" ; 
	echo " -- in ${ASTETCDIR}/manager.conf" ; 
	exit 1; 
fi
echo -n "* Checking if manager over HTTP is enabled: "
if grep -v '^;' ${ASTETCDIR}/manager.conf | grep webenabled | grep -q yes 2>/dev/null; then 
	echo "OK" ; 
else 
	echo "FAILED"; 
	echo " -- Please be sure you have 'webenabled = yes'" ; 
	echo " -- in ${ASTETCDIR}/manager.conf" ; 
	exit 1; 
fi

HTTPHOST=`hostname`
HTTPBINDPORT=`grep -v '^;' ${ASTETCDIR}/http.conf | grep bindport | cut -f 2 -d '=' | sed 's/ //g'`
if [ "${HTTPBINDPORT}" = '' ]; then
  HTTPBINDPORT=8088
fi
HTTPPREFIXBASE=` grep -v '^;' ${ASTETCDIR}/http.conf | grep prefix | sed 's/ //g'`
HTTPPREFIX=` echo ${HTTPPREFIXBASE} | cut -f 2 -d '='`
if [ "${HTTPPREFIXBASE}" = '' ]; then
  HTTPPREFIX='asterisk'
fi
HTTPURL="http://${HTTPHOST}:${HTTPBINDPORT}/${HTTPPREFIX}/static/config/index.html"
HTTPLOCALURL="http://localhost:${HTTPBINDPORT}/${HTTPPREFIX}/static/config/index.html"

echo " --- Everything looks good ---	"
echo " * GUI should be available at ${HTTPURL}"
echo "" 
echo " * Note: If you have bindaddr=127.0.0.1 in ${ASTETCDIR}/http.conf " 
echo "   you will only be able to visit it from the local machine. " 
echo "" 
echo "   Example: ${HTTPLOCALURL}" 
echo "" 
echo " * The login and password should be an entry from ${ASTETCDIR}/manager.conf"
echo "   which has 'config' permission in read and write.  For example:"
echo ""
echo "    [admin]"
echo "    secret = mysecret$$$$"
echo "    read = config"
echo "    write = originate,command,config"
echo ""
echo " --- Good luck! ---	"
