52630816f76bef63c8d1363aee42490ecd8795a1
[feed/routing.git] / luci-app-bmx6 / files / www / cgi-bin / bmx6-info
1 #!/bin/sh
2 # Copyright (C) 2011 Pau Escrich
3 # Contributors Jo-Philipp Wich <xm@subsignal.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #
19 # The full GNU General Public License is included in this distribution in
20 # the file called "COPYING".
21 #
22 # This script gives information about bmx6
23 # Can be executed from a linux shell: ./bmx6-info -s links
24 # Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
25 # If you ask for a directory you wil get the directory contents in JSON forman
26
27 BMX6_DIR="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR="/var/run/bmx6/json"
28
29 #Checking if shell mode or cgi-bin mode
30 if [ "$1" == "-s" ]; then
31 QUERY="$2"
32 else
33 QUERY="${QUERY_STRING%%=*}"
34 echo "Content-type: application/json"
35 echo ""
36
37 fi
38
39 check_path() {
40 [ -d "$1" ] && path=$(cd $1; pwd)
41 [ -f "$1" ] && path=$(cd $1/..; pwd)
42 [ $(echo "$path" | grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
43 }
44
45 print_query() {
46 # If the query is a directory
47 [ -d "$BMX6_DIR/$1" ] &&
48 {
49 # If /all has not been specified
50 [ -z "$QALL" ] &&
51 {
52 total=$(ls $BMX6_DIR/$1 | wc -w)
53 i=1
54 echo -n "{ \"$1\": [ "
55 for f in $(ls $BMX6_DIR/$1); do
56 echo -n "{ \"name\": \"$f\" }"
57 [ $i -lt $total ] && echo -n ','
58 i=$(( $i + 1 ))
59 done
60 echo -n " ] }"
61
62 # If /all has been specified, printing all the files together
63 } || {
64 comma=""
65 echo -n "[ "
66 for entry in "$BMX6_DIR/$1/"*; do
67 [ -f "$entry" ] &&
68 {
69 ${comma:+echo "$comma"}
70 tr -d '\n' < "$entry"
71 comma=","
72 }
73 done
74 echo -n " ]"
75 }
76 }
77
78 # If the query is a file, just printing the file
79 [ -f "$BMX6_DIR/$1" ] && cat "$BMX6_DIR/$1";
80 }
81
82 if [ "${QUERY##*/}" == "all" ]; then
83 QUERY="${QUERY%/all}"
84 QALL=1
85 fi
86
87 if [ "$QUERY" == '$info' ]; then
88 echo '{ "info": [ '
89 print_query status
90 echo -n ","
91 print_query interfaces
92 echo "] }"
93 fi
94
95 if [ "$QUERY" == '$neighbours' ]; then
96 QALL=1
97 echo '{ "neighbours": [ '
98 echo '{ "originators": '
99 print_query originators
100 echo '}, '
101 echo '{ "descriptions": '
102 print_query descriptions
103 echo "} ] }"
104 exit 0
105
106 else if [ "$QUERY" == '$tunnels' ]; then
107 bmx6 -c --jshow tunnels /r=0
108 exit 0
109
110 else
111 check_path "$BMX6_DIR/$QUERY"
112 print_query $QUERY
113 exit 0
114 fi
115 fi
116
117 ls -1F "$BMX6_DIR"
118 exit 0
119