[packages] aiccu: Rework init script
[openwrt/svn-archive/archive.git] / ipv6 / aiccu / files / aiccu.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2011 OpenWrt.org
3 START=51
4
5 add_config_bool() {
6 local section="$1"
7 local option="$2"
8 local value="$3"
9 local default="$4"
10 local _val
11
12 config_get_bool _val "$section" "$option" "$default"
13 [ "$_val" -gt 0 ] && _val="true" || _val="false"
14 append args "$3 $_val" "\n"
15 }
16
17 add_config() {
18 local section="$1"
19 local option="$2"
20 local value="$3"
21 local _val
22
23 config_get _val "$section" "$option"
24 [ -n "$_val" ] && append args "$3 $_val" "\n"
25 }
26
27 start_instance() {
28 local cfg="$1"
29 CFGFILE="/var/etc/aiccu-${cfg}.conf"
30 PIDFILE="/var/run/aiccu-${cfg}.pid"
31 args=""
32
33 add_config "$cfg" username username
34 add_config "$cfg" password password
35 add_config "$cfg" server server
36 add_config "$cfg" protocol protocol
37 add_config "$cfg" interface ipv6_interface
38 add_config "$cfg" tunnel_id tunnel_id
39 add_config_bool "$cfg" requiretls requiretls 0
40 add_config_bool "$cfg" defaultroute defaultroute 1
41 add_config_bool "$cfg" nat behindnat 1
42 add_config_bool "$cfg" heartbeat makebeats 1
43 append args "daemonize true" "\n"
44 append args "pidfile $PIDFILE" "\n"
45
46 mkdir -p /var/run
47 echo -e "$args" > "$CFGFILE"
48
49 config_get_bool enabled "$cfg" enabled '1'
50 [ "$enabled" -gt 0 ] && aiccu start "$CFGFILE"
51 }
52
53 stop_instance() {
54 local cfg="$1"
55 aiccu stop /tmp/run/aiccu-"$cfg".conf
56 }
57
58 restart_instance() {
59 local cfg="$1"
60 stop_instance "$cfg"
61 start_instance "$cfg"
62 }
63
64 start() {
65 config_load aiccu
66 config_foreach start_instance aiccu
67 }
68
69 stop() {
70 config_load aiccu
71 config_foreach stop_instance aiccu
72 }
73
74 restart() {
75 config_load aiccu
76 config_foreach restart_instance aiccu
77 }