Dash:
[t2.git] / package / network / rocknet / rocknet.sh
blob9f443d567c5c47c2210ed4f5dfd65979a930c3ad
1 #!/bin/bash
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # This copyright note is auto-generated by scripts/Create-CopyPatch.
4 #
5 # T2 SDE: package/.../rocknet/rocknet.sh
6 # Copyright (C) 2004 - 2020 The T2 SDE Project
7 #
8 # More information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- T2-COPYRIGHT-NOTE-END ---
16 rocknet_debug=0
17 rocknet_base="/lib/network"
18 rocknet_config="/etc/conf"
19 rocknet_tmp_base="/var/run/rocknet"
21 [ -d $rocknet_tmp_base ] || mkdir -p $rocknet_tmp_base
23 unset code_snipplets_idx code_snipplets_dat code_snipplets_counter
24 declare -a code_snipplets_idx='()'
25 declare -a code_snipplets_dat='()'
26 code_snipplets_counter=0
28 lineno=0
29 ignore=0
30 global=1
32 if [ "$3" != "up" -a "$3" != "down" ]; then
33 echo "Usage: $0 { profile | default } { interface | auto } { up | down }"
34 exit 1
37 if [ ! -f $rocknet_config/network ]; then
38 echo "Configuration file \"$rocknet_config/network\" missing."
39 exit 2
42 profile=$1
43 interface=$2
44 mode=$3
46 pmatched=0 # some profile matched ?
47 imatched=0 # some interface matched ?
50 # addcode mode major-priority minor-priority code1
52 addcode() {
53 [ "$mode" != "$1" ] && return
54 [ "$ignore" -gt 0 ] && return
55 if [ "$1" = "up" ]; then udo="+"; else udo="-"; fi
56 code_snipplets_idx[code_snipplets_counter]="`
57 printf '%04d.%04d.%04d' $((5000$udo$2)) $((5000$udo$lineno)) \
58 $((5000$udo$3))` $code_snipplets_counter"
59 code_snipplets_dat[code_snipplets_counter]="$4"
60 (( code_snipplets_counter++ ))
64 # isfirst unique-id
66 isfirst() {
67 [ "$ignore" -gt 0 ] && return 0
68 eval "\$isfirst_$1"
69 eval "isfirst_$1='return 1'"
70 return 0
74 # error error-message
76 error() {
77 echo "$*"
80 status() {
81 echo "$*"
85 # register / unregister active interfaces for user input validation
88 register() {
89 echo -n "${1}," >> $rocknet_tmp_base/active-interfaces
92 unregister () {
93 active_interfaces="`cat $rocknet_tmp_base/active-interfaces 2>/dev/null`"
94 active_interfaces="${active_interfaces//${1},/}"
95 echo -n "$active_interfaces" > $rocknet_tmp_base/active-interfaces
98 for x in $rocknet_base/*.sh; do . "$x"; done
100 errno=0
101 while read cmd para
103 (( lineno++ ))
104 if [ -n "$cmd" ]; then
105 cmd="${cmd//-/_}"
106 para="$( echo "$para" | sed 's,[\*\?],\\&,g' )"
107 if declare -f public_$cmd > /dev/null
108 then
109 # optimization: Only execute commands when they are
110 # inside an unignored interface section ...
111 if [ $cmd = "interface" ] ; then
112 public_$cmd $para
113 global=0 ip=
114 elif [ $ignore -eq 0 -o $global -gt 0 ] ; then
115 public_$cmd $para
117 else
118 error "Unknown statement in config file: $cmd"
121 done < <( sed 's,\(^\|[ \t]\)#.*$,,' < $rocknet_config/network )
123 while read id1 id2; do
124 if [ "$rocknet_debug" = 1 ]; then
125 echo ">> $id1 -> $id2: ${code_snipplets_dat[id2]}"
127 eval "${code_snipplets_dat[id2]}"
128 done < <(
129 for x in "${code_snipplets_idx[@]}"; do echo "$x"
130 done | sort
133 if [ "$pmatched" = 0 -a "$profile" != "default" ]; then
134 error "Unknown profile: '$profile'"
135 errno=1
137 if [ "$imatched" = 0 ]; then
138 error "Unknown interface for profile: '$interface'"
139 errno=1
141 exit $errno