root/luci/trunk/contrib/package/asterisk-xip/files/uci/asteriskuci @ 4054

Revision 4054, 7.9 KB (checked in by frogonwheels, 4 years ago)

Fix the split_append function so that codec modules are properly enabled.

  • Property svn:executable set to *
Line 
1#!/bin/sh
2
3# Author: Michael Geddes  <michael at frog dot wheelycreek dot net>
4# Copyright 2008 Michael Geddes
5# Licensed under GPL
6Version=0.8
7
8
9# Todo
10#   Calling of Macros in dialplan
11#   Create a Menu
12#   Incoming Zones
13
14debuglevel=0
15
16. /etc/functions.sh
17
18asteriskuci_gen="; Generated by Openwrt AstriskUCI script version ${Version}$N"
19
20# Utils
21
22logerror() {
23    echo "Error: $1"
24}
25
26logdebug() {
27    if [ $(expr $1 "<=" ${debuglevel-0}) == 1 ] ; then
28        echo "Log: $2"
29    fi
30}
31
32is_in_list(){
33    val=$1
34    shift
35    for i in $* ; do
36        [ $i == $val ] && return 0
37    done
38    return 1
39}
40
41split_append() { # {list} {prefix} {item} {func call}
42    local lhs="$2"
43    local rhs="$3"
44   
45    while [ ! -z "$rhs" ] ; do
46        cur=${rhs%%,*}
47        nvar=${rhs#*,}
48        [ -z "$5" ] || eval "$5 ${cur}"
49        append $1 "${lhs}${cur}" "$4"
50        [ "$nvar" == "$rhs" ] && break
51        rhs=${nvar}
52    done
53}
54
55get_checksum() {
56    if [ -r "$2" ] ; then
57        local sum=`md5sum $2 | cut  -d " " -f 1`
58        eval "$1=\"$sum\""
59    else
60        eval "$1=NONE"
61    fi
62    #eval "logdebug 1 \"Checksum $2 : \${$1}\""
63}
64
65check_checksum() {
66    if [ -r "$2" ] ; then
67        local sum=`md5sum $2 | cut  -d " " -f 1`
68    else
69        eval sum=NONE
70    fi
71    #logdebug 1 "Compare $1 checksum $2 with new checksum $sum "
72    [ "$sum" == "$1" ]
73    return $?
74}
75
76# Add config module to initialise list
77ast_add_conf() append asterisk_conf_list $1 " "
78# Add module to initialise list
79ast_add_module() append asterisk_module_list $1 " "
80# Add to 'reload' list.
81ast_add_reload() append asterisk_load_list $1 " "
82
83# Enable a top-level type
84ast_enable_type() eval "enabled_section_${1}=1"
85
86# Is a top-level type enabled?
87ast_type_enabled() {
88    eval "local res=\${enabled_section_${1}}"
89    if [ "$res" != 1 ] ; then
90        return 1 #Fail
91    fi
92    return 0
93}
94
95# For use in sections - make sure that the last section is processed
96check_add() {
97    logdebug 1 "Check add $1"
98    if [ ! -z "${last_added_checked}" ] ; then
99        logdebug 1 "Eval check-add ${last_added_checked}"
100        eval "check_add_${last_added_checked}"
101    fi
102    last_added_checked=$1
103}
104
105# Process the section yet to be checked.
106check_all_added() check_add ""
107
108# Create static links for stuff we dont want to configure yet.
109create_staticlinks() {
110    logdebug 1 "Link in a few mostly static configurations"
111    linkconfigs="codecs.conf say.conf sip_notify.conf udptl.conf logger.conf"
112    module_enabled res_indications && append linkconfigs indications.conf " "
113    for i in ${linkconfigs} ; do
114        [ -e $DEST_DIR/$i ] || ln -s $DEST/etc/asterisk/$i $DEST_DIR
115    done
116
117    logdebug 1 "Link in #include directories"
118    for i in include inc libs lib library macro macros ; do
119        if [ -e $DEST/etc/asterisk/$i -a  ! -d "$DEST_DIR/$i" -a ! -e "$DEST_DIR/$i" ] ; then
120            ln -s $DEST/etc/asterisk/$i $DEST_DIR
121        fi
122    done
123}
124
125# default reboot
126reboot_hardware() {}
127
128
129# Top level handler
130setup_asterisk() {
131    DEST=${1%/}
132    DEST_DIR=/tmp/asterisk
133
134    testing_mode=0
135    if [ "$2" == "testonly" ] ; then
136        testing_mode=1
137    elif [ "$2" == "test" ] ; then
138        DEST_DIR=/tmp/asterisk.tmp
139        echo Using Test dir: $DEST_DIR
140        testing_mode=2
141    fi
142
143    [ -z "$3" ] || debuglevel=$3
144
145    logdebug 1 "Loading Asterisk Config"
146    . ${UCILIB}/asteriskconf
147    logdebug 2 "Loading Module Config"
148    . ${UCILIB}/moduleconf
149    logdebug 2 "Loading Dialplan Config"
150    . ${UCILIB}/dialplanconf
151
152    for f in ${DEST}/etc/asterisk/conf.d/* ; do
153        logdebug 1 "Loading Module $f"
154        [ -f $f ] && . $f
155    done
156
157    include /lib/network
158    scan_interfaces
159
160    init_asteriskconf
161    init_moduleconf
162    init_dialplanconf
163
164    for i in  ${asterisk_module_list} ; do
165        logdebug 1 "Init $i module"
166        eval "init_${i}"
167    done
168
169    for i in ${asterisk_conf_list} ; do
170        logdebug 1 "Init $i config"
171        eval "init_${i}conf"
172    done
173 
174    config_cb() {
175        cur_section=$1/$2
176        logdebug 2 "Load $1/$2"
177        eval "local val=\"\${dups_$2}\""
178        if [ "${val}" == "" ] ; then
179            eval "dups_$2=1"
180        else
181            logerror "Duplicate Section Name: $2  (type $1)"
182        fi
183
184        if ast_type_enabled $1 ; then
185            eval "handle_$1 \$2"
186        elif [ ! -z "$1" ] ; then
187
188            logerror "Unknown section: $1/$2"
189            option_cb() {
190                logerror "Invalid option '$1' for invalid section"
191            }
192        fi
193    }
194    config_load asterisk
195    check_all_added
196
197    if [ "$testing_mode" != "1" ] ; then
198        mkdir -p ${DEST_DIR}
199
200        create_asteriskconf
201        for i in ${asterisk_conf_list} ; do
202            logdebug 1 "Create $i config"
203            eval "create_${i}conf"
204        done
205        create_dialplanconf
206        create_moduleconf
207
208        # Link in a few mostly static configurations
209        create_staticlinks
210    fi
211    [ "$testing_mode" == "2" ] && reload_check_asterisk
212    return 0
213}
214
215astcmd() {
216    ASTCMD="${DEST%/}/usr/sbin/asterisk -C /tmp/asterisk/asterisk.conf "
217    logdebug 1 "Command: $1"
218    if [ -z "${2-}" ] ; then
219        ${ASTCMD} -r -x "$1" 2>&- 1>&-
220    else
221        eval "$2=`${ASTCMD} -r -x \"$1\"`"
222    fi
223    return $?
224}
225
226# waitfor() {
227#   while [ -d /proc/$1 ] ; do
228#       sleep 1
229#   done
230# }
231
232restart_gracefully() {
233    stop_uci_asterisk "$DEST"
234    startup_asterisk "$DEST"
235    #ret=0
236    #echo "Check for pid"
237    #if [ -r /var/run/asterisk.ctl ] ; then
238    #   astcmd "stop gracefully"
239    #   local ret=$?
240    #   [ ${ret} = 0 ] || return $ret
241    #   waitfor `cat /var/run/asterisk.pid`
242    #fi
243    #startup_asterisk ${DEST}
244    return 0
245}
246astcmds() {
247    while [ ! -z "$1" ] ; do
248        astcmd "$1"
249        shift
250    done
251}
252
253reload_check_asterisk() {
254    logdebug 1 "Check Reloading"
255    local reboot=0
256    if [ "${ast_restart-}" == 1 ] ; then
257        logdebug 1 "Restarting Gracefully"
258        reboot=0
259    else
260        for i in ${asterisk_load_list} ; do
261            logdebug 1 "Checking ${i} reload"
262            eval "local doload=\${ast_${i}_restart}"
263            case $doload in
264                1) logdebug 1 "Reloading ${i}" ;;
265                2) logdebug 1 "Unloading ${i}" ;;
266            esac
267        done
268    fi
269    [ ${reboot} = 1 ] && logdebug 1 "reboot hardware"
270}
271
272reload_asterisk() {
273    logdebug 1 "Reloading"
274    local reboot=0
275    if [ "${ast_restart-}" == 1 ] ; then
276        logdebug 2 "Restarting Gracefully"
277        restart_gracefully
278        reboot=0
279    else
280        for i in ${asterisk_load_list} ; do
281            logdebug 3 "Checking ${i} reload"
282            eval "local doload=\${ast_${i}_restart}"
283            case $doload in
284                1) logdebug 1 "Reloading ${i}"
285                eval "reload_${i}" || reboot=1 ;;
286                2) logdebug 1 "Unloading ${i}"
287                eval "unload_${i}" || reboot=1 ;;
288            esac
289        done
290    fi
291
292    if [ ${reboot} = 1 ] ; then
293        ( sleep 5; reboot_hardware ) &
294    fi
295}
296
297startup_asterisk() {
298    DEST="${1%/}"
299    DEFAULT=$DEST/etc/default/asterisk
300    [ -f $DEFAULT ] && . $DEFAULT
301    [ -d /var/run ] || mkdir -p /var/run
302    [ -d ${asterisk_logdir} ] || mkdir -p ${asterisk_logdir}
303    [ -d ${asterisk_spooldir} ] || mkdir -p ${asterisk_spooldir}
304    [ -d /var/spool/asterisk ] || mkdir -p /var/spool/asterisk
305    [ -h $DEST/usr/lib/asterisk/astdb ] || ln -sf /var/spool/asterisk/astdb $DEST/usr/lib/asterisk/astdb
306    [ -e /dev/zappseudo ] && [ ! -d /dev/zap -o ! -e /dev/zap/pseudo ] && mkdir -p /dev/zap && ln -s /dev/zappseudo /dev/zap/pseudo
307
308    $DEST/usr/sbin/asterisk -C /tmp/asterisk/asterisk.conf $UCIOPTIONS -f 2>&1 > ${asterisk_logdir}/asterisk_proc &
309    # Wait a bit then reboot the hardware
310    ( sleep 5; reboot_hardware ) &
311}
312
313# Init.d start() handler
314start_uci_asterisk() {
315    DEST="${1%/}"
316
317    if setup_asterisk $DEST ; then
318        startup_asterisk "$DEST"
319    fi
320}
321
322restart_uci_asterisk() {
323    DEST="${1%/}"
324    if setup_asterisk $DEST ; then
325        echo "Trying to Restart gracefully"
326        if [ -r /var/run/asterisk.ctl ] ; then
327#           if astcmd "restart gracefully" ; then
328            echo "Sending restart"
329            if restart_gracefully  ; then
330                echo "Restarting gracefully"
331                return 0
332            fi
333        fi
334        stop_uci_asterisk "$DEST"
335        startup_asterisk "$DEST"
336    else
337        stop_uci_asterisk $1
338        echo "Setup Failed"
339        return 1
340    fi
341}
342
343# init.d stop() handler
344stop_uci_asterisk() {
345    DEST=${1%/}
346    if [ -r /var/run/asterisk.ctl ] ; then
347        astcmd "stop now"
348        sleep 1
349    fi
350    [ -f /var/run/asterisk.pid ] && kill $(cat /var/run/asterisk.pid) >/dev/null 2>&1
351}
352
353reload_uci_asterisk() {
354    DEST=${1%/}
355    DEFAULT=$DEST/etc/default/asterisk
356
357    if [ -r /var/run/asterisk.ctl ] ; then
358
359        [ -e /dev/zappseudo ] && [ ! -d /dev/zap -o ! -e /dev/zap/pseudo ] && mkdir -p /dev/zap && ln -s /dev/zappseudo /dev/zap/pseudo
360        if setup_asterisk "$DEST" ; then
361            # Selective reload modules.
362            reload_asterisk
363        fi
364    else
365        start_uci_asterisk "$1"
366    fi
367}
368
369# vim: ts=2 sw=2 noet foldmethod=indent
Note: See TracBrowser for help on using the browser.