#!/bin/sh
#
# $FreeBSD: head/net/asterisk/files/asterisk.in 372448 2014-11-11 14:00:43Z madpilot $
#

# PROVIDE: asterisk
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable asterisk:
#
# asterisk_enable (bool):	Set it to "YES" to enable asterisk
#				Default is "YES"
# asterisk_user (string):	User asterisk runs as
#				Default is asterisk
# asterisk_args (string):	Extra argumeents to pass to asterisk at startup
# 				Default is "-n"
# asterisk_pidfile (string):	Location of the asterisk pid file
#				Default is /var/run/asterisk/asterisk.pid
#

. /etc/rc.subr

name=asterisk
rcvar=asterisk_enable
desc="Asterisk PBX server"

load_rc_config $name

: ${asterisk_enable:=YES}
: ${asterisk_user:=asterisk}
: ${asterisk_args=-n}
: ${asterisk_pidfile:=/var/run/asterisk/asterisk.pid}

extra_commands=reload

stop_cmd=asterisk_stop
reload_cmd=asterisk_reload

command="/usr/local/sbin/asterisk"
command_args="${asterisk_args} -F -U ${asterisk_user}"
pidfile=${asterisk_pidfile}

asterisk_stop()
{
	echo 'Stopping asterisk'
	$command -nqrx 'core stop now'
  killall $name
}

asterisk_reload()
{
	echo 'Reloading asterisk'
	$command -nqrx 'reload'
}

run_rc_command "$1"
# Create paths in pfSense's volatile area if they don't exist (note: please do not remove this comment line)
for a in db spool run log 
do
if [ ! -d /var/$a/asterisk ]; then
  mkdir -p /var/$a/asterisk
  chown -R asterisk:asterisk /var/$a/asterisk
  chmod -R g+w /var/$a/asterisk
fi
done

if [ ! -d /var/log/asterisk/cdr-csv ]; then
  mkdir -p /var/log/asterisk/cdr-csv
  chown -R asterisk:asterisk /var/log/asterisk/cdr-csv
  chmod -R g+w /var/log/asterisk/cdr-csv
fi
