Move bmx6 and luci-app-bmx6 packages into root directory
[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/$QUERY" ] && cat "$BMX6_DIR/$QUERY";
80 }
81
82 if [ "${QUERY##*/}" == "all" ]; then
83 QUERY="${QUERY%/all}"
84 QALL=1
85 fi
86
87
88 if [ "$QUERY" == '$neighbours' ]; then
89 QALL=1
90 echo '{ "neighbours": [ '
91 echo '{ "originators": '
92 print_query originators
93 echo '}, '
94 echo '{ "descriptions": '
95 print_query descriptions
96 echo "} ] }"
97 exit 0
98
99 else if [ "$QUERY" == '$tunnels' ]; then
100 bmx6 -c --jshow tunnels
101 exit 0
102
103 else
104 check_path "$BMX6_DIR/$QUERY"
105 print_query $QUERY
106 exit 0
107 fi
108 fi
109
110 ls -1F "$BMX6_DIR"
111 exit 0
112