[packages] aiccu: Rework init script
authoracinonyx <acinonyx@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Thu, 3 Nov 2011 16:05:38 +0000 (16:05 +0000)
committeracinonyx <acinonyx@3c298f89-4303-0410-b956-a3cf2f4a3e73>
Thu, 3 Nov 2011 16:05:38 +0000 (16:05 +0000)
git-svn-id: svn://svn.openwrt.org/openwrt/packages@28726 3c298f89-4303-0410-b956-a3cf2f4a3e73

ipv6/aiccu/files/aiccu.init

index 68bac20..bc5ef75 100644 (file)
@@ -1,52 +1,77 @@
 #!/bin/sh /etc/rc.common
-# Copyright (C) 2006 OpenWrt.org
+# Copyright (C) 2006-2011 OpenWrt.org
 START=51
 
-config_cb() {
-       local cfg_type="$1"
-       local cfg_name="$2"
+add_config_bool() {
+       local section="$1"
+       local option="$2"
+       local value="$3"
+       local default="$4"
+       local _val
 
-       case "$cfg_type" in
-               aiccu)
-                       append cfgs_sections "$cfg_name" "$N"
-               ;;
-       esac
+       config_get_bool _val "$section" "$option" "$default"
+       [ "$_val" -gt 0 ] && _val="true" || _val="false"
+       append args "$3 $_val" "\n"
+}
+
+add_config() {
+       local section="$1"
+       local option="$2"
+       local value="$3"
+       local _val
+
+       config_get _val "$section" "$option"
+       [ -n "$_val" ] && append args "$3 $_val" "\n"
+}
+
+start_instance() {
+       local cfg="$1"
+       CFGFILE="/var/etc/aiccu-${cfg}.conf"
+       PIDFILE="/var/run/aiccu-${cfg}.pid"
+       args=""
+       
+       add_config "$cfg" username username
+       add_config "$cfg" password password
+       add_config "$cfg" server server
+       add_config "$cfg" protocol protocol
+       add_config "$cfg" interface ipv6_interface
+       add_config "$cfg" tunnel_id tunnel_id
+       add_config_bool "$cfg" requiretls requiretls 0
+       add_config_bool "$cfg" defaultroute defaultroute 1
+       add_config_bool "$cfg" nat behindnat 1
+       add_config_bool "$cfg" heartbeat makebeats 1
+       append args "daemonize true" "\n"
+       append args "pidfile $PIDFILE" "\n"
+
+       mkdir -p /var/run
+       echo -e "$args" > "$CFGFILE"
+
+       config_get_bool enabled "$cfg" enabled '1'
+       [ "$enabled" -gt 0 ] && aiccu start "$CFGFILE"
+}
+
+stop_instance() {
+       local cfg="$1"
+       aiccu stop /tmp/run/aiccu-"$cfg".conf
+}
+
+restart_instance() {
+       local cfg="$1"
+       stop_instance "$cfg"
+       start_instance "$cfg"
 }
 
 start() {
        config_load aiccu
-       for cfgs_section in $cfgs_sections; do
-               config_get username $cfgs_section username
-               config_get password $cfgs_section password
-               config_get server $cfgs_section server
-               config_get protocol $cfgs_section protocol
-               config_get interface $cfgs_section interface
-               config_get tunnel_id $cfgs_section tunnel_id
-               config_get_bool requiretls $cfgs_section requiretls 0
-               config_get_bool defaultroute $cfgs_section defaultroute 1
-               config_get_bool nat $cfgs_section nat 1
-               config_get_bool heartbeat $cfgs_section heartbeat 1
-               mkdir -p /tmp/run
-               echo "username $username" > /tmp/run/aiccu-${cfgs_section}.conf
-               echo "password $password" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ -n "$server" ] && echo "server $server" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ -n "$protocol" ] && echo "protocol $protocol" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ -n "$interface" ] && echo "ipv6_interface $interface" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ -n "$tunnel_id" ] && echo "tunnel_id $tunnel_id" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ "$requiretls" = "1" ] && echo "requiretls true" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ "$defaultroute" != "1" ] && echo "defaultroute false" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ "$nat" = "1" ] && echo "behindnat true" >> /tmp/run/aiccu-${cfgs_section}.conf
-               [ "$heartbeat" != "1" ] && echo "makebeats false" >> /tmp/run/aiccu-${cfgs_section}.conf
-               echo 'daemonize true' >> /tmp/run/aiccu-${cfgs_section}.conf
-               echo "pidfile /var/run/aiccu-${cfgs_section}.pid" >> /tmp/run/aiccu-${cfgs_section}.conf
-       
-               aiccu start /tmp/run/aiccu-$cfgs_section.conf
-       done
+       config_foreach start_instance aiccu
 }
 
 stop() {
        config_load aiccu
-       for cfgs_section in $cfgs_sections; do
-               aiccu stop /tmp/run/aiccu-$cfgs_section.conf
-       done
+       config_foreach stop_instance aiccu
 }
+
+restart() {
+       config_load aiccu
+       config_foreach restart_instance aiccu
+}
\ No newline at end of file