#!/bin/sh

# Voicemail.conf

ast_add_conf voicemail

init_voicemailconf() {
	ast_add_reload voicemail

	ast_enable_type voicegeneral
	ast_enable_type voicemail
	ast_enable_type voicezone
	ast_enable_type dialplanvoice

	voice_format=
	voice_serveremail=
	voice_attach=no
	voice_skipms=3000
	voice_maxsilence=10
	voice_silencethreshold=128
	voice_maxlogins=3
	voice_emaildateformat="%A, %B %d, %Y at %r"
	voice_sendvoicemail=no
	voice_maxmsg=100
	voice_maxmessage=180
	voice_minmessage=3
	voice_maxgreet=60
	return 0
}

voicegeneral_list="format emailformat serveremail attach skipms maxsilence silencethreshold maxlogins emaildateformat sendvoicemail maxmsg maxmessage minmessage maxgreet"
voicegeneral_ext_list=""

valid_voicemail(){
	is_in_list $1 ${voicegeneral_list} ${voicegeneral_ext_list}
	return $?
}

voicebox_list="context number password name email pager"

voicebox_listopt="tz attach serveremail saycid dialout callback review operator envelope sayduration saydurationm"

valid_voicebox() {
	is_in_list $1 ${voicebox_list} ${voicebox_listopt}
	return $?
}

check_add_voicebox() {
	if [ ! -z "${voicebox_number}" ] ; then
		[ -z "${voicebox_context}" ] && voicebox_context=default
		logdebug 1 "Adding Voicebox ${voicebox_number} in ${voicebox_context}"
		# Construct the voicebox line
		local line="$voicebox_number => "
		[ -z "${voicebox_tz}" ] && voicebox_tz=homeloc
		[ -z "${voicebox_name}" ] && voicebox_name=OpenWRT

		logdebug 1 "Adding in order options"
		for i in password name email pager ; do
			eval "local value=\"\${voicebox_$i}\""
			line="${line}${value},"
		done

		# Then add named options.
		logdebug 2 "Adding in named options"
		for i in ${voicebox_listopt} ; do
			eval val=\${voicebox_$i}
			[ -z "${val}" ] || append  line "$i=$val" \|
		done

		logdebug 2 "Check for empty"
		# Check if the current voicebox context has anything
		eval local cur="\${voicebox_section_$voicebox_context}"
		# if not add it to the list of contexts used
		[ -z "$cur" ] && append voice_contextlist "${voicebox_context}" " "

		# Then add the voicebox line to the context
		logdebug 4 "Add Voicebox $line to ${voicebox_context}"
		append voicebox_section_${voicebox_context} "$line" "$N"
	fi

	# Then clear the settings for the next one.
	for i in ${voicebox_list} ${voicebox_listopt} ; do
		eval unset voicebox_$i
	done
}

create_voicemailconf() {
	# Construct the file
	file=${DEST_DIR}/voicemail.conf
	get_checksum voicemail_conf $file

	local isempty=1
	if [ -z "${voice_contextlist}" ] ; then
		local isempty=2
		rm -f $file
	else

		[ -z "${voice_format}" ] && voice_format="wav49|gsm|wav"
		# Make emailformat first in the list
		if [ ! -z "${voice_emailformat}" ] ; then
			local newfmt=${voice_emailformat}
			for i in ${voice_format//|/ } ; do
				[ "$i" == "${voice_emailformat}" ] || newfmt="${newfmt}|${i}"
			done
			voice_format="${newfmt}"
		fi
		echo "${asteriskuci_gen}[general]" > $file
		for i in ${voicegeneral_list} ; do
			eval value=\${voice_$i}
			if [ ! -z "$value" ] ; then
				echo "$i=$value" >> $file
			fi
		done
		echo "${N}[zonemessages]" >> $file
		echo "homeloc=${asterisk_zone}| Q IMp" >> $file
		echo "${voicezone_list}" >> $file
		for i in ${voice_contextlist} ; do
			echo "${N}[$i]" >> $file
			eval "local cursection=\"\${voicebox_section_${i}}\""
			echo "$cursection" >> $file
			eval unset voicebox_section_${i}
		done
		unset voice_contexts
	fi
	check_checksum "$voicemail_conf" "$file" || ast_voicemail_restart=$isempty
}

handle_voicegeneral() {
	option_cb() {
		case "$1" in
			format|format_ITEM*) append voice_format "$2" "|" ;;
			format_LENGTH) ;;
		  *)
			if valid_voicemail $1 $2 ; then
				eval voice_$1="$2"
			else
				logerror "Invalid general voice option: $1"
			fi
		esac
	}
}

handle_voicemail() {
	check_add voicebox
	voicebox_context="${1%[-_]*}"
	if [ "${voicebox_context}" == "$1" ] ; then
		voicebox_context=default
	fi
	voicebox_number=${1#*[-_]}
	option_cb() {
		case $1 in
			zone) voicebox_tz="$2" ;;
			*)
				if valid_voicebox $1 $2 ; then
					eval "voicebox_$1=\"$2\""
				else
					logerror "Invalid voicebox option: $1"
				fi
		esac
	}
}

# Locality options for voicemail

check_add_voicezone() {
	if [ ! -z "${voicezone_name}" ] ; then
		[ -z "${voicezone_zone}" ] && voicezone_zone=${asterisk_zone}
		if [ -z "${voicezone_message}" ] ; then
			voicezone_message="Q IMp"
		else
			voicezone_message=`echo "$voicezone_message"|tr \" \'`
		fi
		append voicezone_list "${voicezone_name}=${voicezone_zone}|${voicezone_message}" "${N}"
	fi
	unset voicezone_name
	unset voicezone_zone
	unset voicezone_message
}

handle_voicezone() {
	voicezone_name=$1
	option_cb() {
		case $1 in
			name) voicezone_name="$2" ;;
			zone) voicezone_zone="$2" ;;
			message) voicezone_message="$2" ;;
			*) logerror "Invalid voicezone option: $1"
		esac
	}
}

handle_dialplanvoice() {
	check_add dialplanvoice
	option_cb() {
		case $1 in
			dialplan|extension|voicecontext|voicebox)
				eval "dial_voice_$1=\"$2\"" ;;
			*) logerror "Invalid option: $1 for dialplanvoice"
		esac
	}
}

check_add_dialplanvoice() {
	if [ ! -z "${dial_voice_dialplan}" -a ! -z "${dial_voice_extension}" ] ; then
		local ext="exten => ${dial_voice_extension},"
		[ -z "${dial_voice_voicebox}" ] && dial_voice_voicebox=default
		if [ -z "${dial_voice_voicebox}" ] ; then
			logerror "Expecting voicebox for ${dial_voice_dialplan}/${dial_voice_extension}"
		else
			check_add_context ${dial_voice_dialplan}
			local voiceext="${dial_voice_voicebox}@${dial_voice_voicecontext}"
			enable_voicemail
			append dialplan_context_${dial_voice_dialplan} "${ext}1,VoiceMailMain(${voiceext})" "${N}"
		fi
	fi
	for i in dialplan extension voicecontext voicebox ; do
		eval "unset dial_voice_$i"
	done
}

enable_voicemail() {
	enable_module res_adsi
	enable_module app_voicemail
	enable_format gsm
}


add_dialplan_voice() {
	local context=$1
	logdebug 1 "Adding Dialplan voice $1 $2"
	check_add_context "$context"
	local ext="exten => $2,"
	enable_voicemail
	append dialplan_context_${context} "${ext}1,VoiceMailMain($3)" "${N}"
}



reload_voicemail() astcmd "module reload app_voicemail.so"
unload_voicemail() astcmd "module unload app_voicemail.so"


# vim: ts=2 sw=2 noet foldmethod=indent
