[packages] olsrd: init: reduce code duplication by reusing already_in_schema()
[feed/routing.git] / files / olsrd.init
index 6ed369fc473ece892417b1e81c81436537976896..1172623023adf60a01fb3412152d9fa02c369e83 100644 (file)
@@ -225,6 +225,7 @@ config_update_schema() {
 }
 
 config_write_options() {
+       local funcname="config_write_options"
        unset IFS
        local schema="$1"
        local cfg="$2"
@@ -244,6 +245,8 @@ config_write_options() {
        local list_value
        local i
        local position
+       local speed
+       local list_speed_vars="HelloInterval HelloValidityTime TcInterval TcValidityTime MidInterval MidValidityTime HnaInterval HnaValidityTime"
 
        get_value_for_entry()
        {
@@ -262,12 +265,88 @@ config_write_options() {
                [ "$option_type" = internal ] && return 1
 
                config_get value "$cfg" "$option"
+               [ "$option" = "speed" ] && return 1
 
                return 0
        }
 
+       already_in_schema()
+       {
+               case " $schema " in
+                       *" $1 "*)
+                               return 0
+                       ;;
+                       *)
+                               return 1
+                       ;;
+               esac
+       }
+
+       already_in_schema "speed" && {
+               get_value_for_entry "speed"
+
+               if [ 2>/dev/null $value -gt 0 -a $value -le 20 ]; then
+                       speed="$value"
+               else
+                       log "$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
+                       speed=6
+               fi
+
+               for schema_entry in $list_speed_vars; do {
+                       already_in_schema "$schema_entry" || schema="$schema $schema_entry"
+               } done
+       }
+
        for schema_entry in $schema; do
-               get_value_for_entry "$schema_entry" || continue
+               if [ -n "$speed" ]; then                # like sven-ola freifunk firmware fff-1.7.4
+                       case "$schema_entry" in
+                               HelloInterval)
+                                       value="$(( $speed / 2 + 1 )).0"
+                               ;;
+                               HelloValidityTime)
+                                       value="$(( $speed * 25 )).0"
+                               ;;
+                               TcInterval)     # todo: not fisheye? -> $(( $speed * 2 ))
+                                       value=$(( $speed / 2 ))
+                                       [ $value -eq 0 ] && value=1
+                                       value="$value.0"
+                               ;;
+                               TcValidityTime)
+                                       value="$(( $speed * 100 )).0"
+                               ;;
+                               MidInterval)
+                                       value="$(( $speed * 5 )).0"
+                               ;;
+                               MidValidityTime)
+                                       value="$(( $speed * 100 )).0"
+                               ;;
+                               HnaInterval)
+                                       value="$(( $speed * 2 )).0"
+                               ;;
+                               HnaValidityTime)
+                                       value="$(( $speed * 25 )).0"
+                               ;;
+                               *)
+                                       get_value_for_entry "$schema_entry" || continue
+                               ;;
+                       esac
+
+                       is_speed_var()
+                       {
+                               case " $list_speed_vars " in
+                                       *" $1 "*)
+                                               return 0
+                                       ;;
+                                       *)
+                                               return 1
+                                       ;;
+                               esac
+                       }
+
+                       is_speed_var "$schema_entry" && option="$schema_entry"
+               else
+                       get_value_for_entry "$schema_entry" || continue
+               fi
 
                if [ -z "$value" ]; then
                        IFS='+'