[packages] ahcpd: update to v0.2, add config and initscript
[packages.git] / ipv6 / ahcpd / files / ahcpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007 OpenWrt.org
3
4 START=99
5
6 NAME=ahcpd
7 BIN_F=/usr/sbin/$NAME
8 SSD=start-stop-daemon
9
10 is_enabled() {
11         local cfg="$1"
12
13         config_get_bool enabled "$cfg" enabled '1'
14         [ $enabled -ne 0 ] || {
15                 echo "$initscript: not enabled"
16                 return 1
17         }
18 }
19
20 get_interface() {
21         local cfg="$1"
22
23         config_get interface "$cfg" interface
24         [ -n "$interface" ] || {
25                 echo "$initscript: not 'interface' option specified"
26                 return 1
27         }
28 }
29
30 get_pid_file() {
31         local cfg="$1"
32
33         config_get pid_file "$cfg" pid_file
34         [ -n "$pid_file" ] || pid_file="/var/run/$NAME-$interface.pid"
35 }
36
37 get_options() {
38         local cfg="$1"
39         local address
40         local port
41         local authoritative
42
43         config_get options "$cfg" options
44
45         config_get config_script "$cfg" config_script
46         [ -n "$config_script" ] && append options "-c $config_script"
47
48         config_get address "$cfg" address
49         [ -n "$adress" ] && append options "-m $address"
50
51         config_get port "$cfg" port
52         [ -n "$port" ] && append options "-p $port"
53
54         config_get_bool authoritative "$cfg" authoritative '0'
55         [ $authoritative -ne 0 ] && {
56                 local dat_file
57                 local gen_options
58
59                 config_get dat_file "$cfg" authority_file
60                 [ -n "$dat_file" ] || dat_file="/var/run/$NAME-$interface.dat"
61
62                 [ -f "$dat_file" ] || {
63                         local expire
64                         local prefix
65                         local prototocol
66                         local dns_server
67                         local ntp_server
68
69                         config_get prefix "$cfg" prefix
70                         [ -n "$prefix" ] || prefix=`ahcp-generate-address -p -s -r`
71                         append gen_options "-p $prefix"
72
73                         config_get expire "$cfg" expire
74                         [ -n "$expire" ] && append gen_options "-e $expire"
75
76                         config_get protocol "$cfg" protocol
77                         [ -n "$protocol" ] && append gen_options "-P $protocol"
78
79                         [ "$protocol" = "static" ] && {
80                                 local gateway
81
82                                 config_get gateway "$cfg" gateway
83                                 [ -n "$gateway" ] && append gen_options "-g $gateway"
84                         }
85
86                         config_get dns_server "$cfg" dns_server
87                         [ -n "$dns_server" ] && append gen_options "-n $dns_server"
88
89                         config_get ntp_server "$cfg" ntp_server
90                         [ -n "$ntp_server" ] && append gen_options "-N $ntp_server"
91
92                         ahcp-generate $gen_options > $dat_file
93                 }
94                 append options "-a $dat_file"
95         }
96         append options "$interface"
97 }
98
99 start_service() {
100         local cfg="$1"
101
102         is_enabled "$cfg" || return
103         get_interface "$cfg" || return
104         get_pid_file $cfg
105         get_options $cfg
106
107         $SSD -S -p $pid_file -b -m -x $BIN_F -- $options &>/dev/null
108 }
109
110 stop_service() {
111         local cfg="$1"
112
113         is_enabled "$cfg" || return
114         get_interface "$cfg" || return
115         get_pid_file $cfg
116
117         $SSD -K -p $pid_file &>/dev/null
118 }
119
120 start() {
121         config_load $NAME
122         config_foreach start_service $NAME
123 }
124
125 stop() {
126         config_load $NAME
127         config_foreach stop_service $NAME
128 }
129