* move libffi to base/ Category
[t2sde.git] / package / network / rocknet / rocknet.sh
blobc4c99b192a9b48107ff043a7477589787686a96d
1 #!/bin/bash
2 # --- T2-COPYRIGHT-NOTE-BEGIN ---
3 # T2 SDE: package/*/rocknet/rocknet.sh
4 # Copyright (C) 2004 - 2024 The T2 SDE Project
5 #
6 # This Copyright note is generated by scripts/Create-CopyPatch,
7 # more information can be found in the files COPYING and README.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2.
11 # --- T2-COPYRIGHT-NOTE-END ---
13 rocknet_debug=0
14 rocknet_base="/lib/network"
15 rocknet_config="/etc/conf"
16 rocknet_tmp_base="/var/run/rocknet"
18 [ -d $rocknet_tmp_base ] || mkdir -p $rocknet_tmp_base
20 unset code_snipplets_idx code_snipplets_dat code_snipplets_counter
21 declare -a code_snipplets_idx='()'
22 declare -a code_snipplets_dat='()'
23 code_snipplets_counter=0
25 lineno=0
26 ignore=0
27 global=1
29 if [ "$3" != "up" -a "$3" != "down" ]; then
30 echo "Usage: $0 { profile | default } { interface | auto } { up | down }"
31 exit 1
34 if [ ! -f $rocknet_config/network ]; then
35 echo "Configuration file \"$rocknet_config/network\" missing."
36 exit 2
39 profile=$1
40 interface=$2
41 mode=$3
43 pmatched=0 # some profile matched ?
44 imatched=0 # some interface matched ?
47 # addcode mode major-priority minor-priority code1
49 addcode() {
50 [ "$mode" != "$1" ] && return
51 [ "$ignore" -gt 0 ] && return
52 if [ "$1" = "up" ]; then udo="+"; else udo="-"; fi
53 code_snipplets_idx[code_snipplets_counter]="`
54 printf '%04d.%04d.%04d' $((5000$udo$2)) $((5000$udo$lineno)) \
55 $((5000$udo$3))` $code_snipplets_counter"
56 code_snipplets_dat[code_snipplets_counter]="$4"
57 ((code_snipplets_counter++))
61 # isfirst unique-id
63 isfirst() {
64 [ "$ignore" -gt 0 ] && return 0
65 eval "\$isfirst_$1"
66 eval "isfirst_$1='return 1'"
67 return 0
71 # error error-message
73 error() {
74 echo "$*"
77 status() {
78 echo "$*"
82 # register / unregister active interfaces for user input validation
85 register() {
86 echo -n "${1}," >> $rocknet_tmp_base/active-interfaces
89 unregister () {
90 active_interfaces="`cat $rocknet_tmp_base/active-interfaces 2>/dev/null`"
91 active_interfaces="${active_interfaces//${1},/}"
92 echo -n "$active_interfaces" > $rocknet_tmp_base/active-interfaces
95 for x in $rocknet_base/*.sh; do . "$x"; done
97 errno=0
98 while read cmd para; do
99 ((lineno++))
100 if [ -n "$cmd" ]; then
101 cmd="${cmd//-/_}"
102 para="$(echo "$para" | sed 's,[\*\?],\\&,g')"
103 if declare -f public_$cmd > /dev/null; then
104 # optimization: Only execute commands when they are
105 # inside an unignored interface section ...
106 if [ $cmd = "interface" ]; then
107 public_$cmd $para
108 global=0 ip=
109 elif [ $ignore -eq 0 -o $global -gt 0 ]; then
110 public_$cmd $para
112 else
113 error "Unknown statement in config file: $cmd"
116 done < <(sed 's,\(^\|[ \t]\)#.*$,,' < $rocknet_config/network)
118 while read id1 id2; do
119 if [ "$rocknet_debug" = 1 ]; then
120 echo ">> $id1 -> $id2: ${code_snipplets_dat[id2]}"
122 eval "${code_snipplets_dat[id2]}"
123 done < <(
124 for x in "${code_snipplets_idx[@]}"; do echo "$x"
125 done | sort
128 if [ "$pmatched" = 0 -a "$profile" != "default" ]; then
129 error "Unknown profile: '$profile'"
130 errno=1
132 if [ "$imatched" = 0 ]; then
133 error "Unknown interface for profile: '$interface'"
134 errno=1
136 exit $errno