Developing lightweight computation at the DSG edge

bmx6-info 2.73 KB
Newer Older
hitz's avatar
hitz committed
1
#!/bin/sh
2
3
#    Copyright (C) 2011 Pau Escrich
#    Contributors Jo-Philipp Wich <xm@subsignal.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
hitz's avatar
hitz committed
18
#
19
20
#    The full GNU General Public License is included in this distribution in
#    the file called "COPYING".
21
22
23
24
25
#
#    This script gives information about bmx6
#    Can be executed from a linux shell: ./bmx6-info -s links
#    Or from web interfae (with cgi enabled): http://host/cgi-bin/bmx6-info?links
#    If you ask for a directory you wil get the directory contents in JSON forman
hitz's avatar
hitz committed
26

27
BMX6_DIR="$(uci get bmx6.general.runtimeDir 2>/dev/null)" || BMX6_DIR="/var/run/bmx6/json"
hitz's avatar
hitz committed
28

hitz's avatar
hitz committed
29
#Checking if shell mode or cgi-bin mode
30
if [ "$1" == "-s" ]; then
31
	QUERY="$2"
32
else
p4u's avatar
p4u committed
33
34
	QUERY="${QUERY_STRING%%=*}"
	echo "Content-type: application/json"
35
36
	echo ""

hitz's avatar
hitz committed
37
fi
hitz's avatar
hitz committed
38
39
40
41
42
43
44

check_path() {
        [ -d "$1" ] && path=$(cd $1; pwd)
        [ -f "$1" ] && path=$(cd $1/..; pwd)
        [ $(echo "$path" | grep -c "^$BMX6_DIR") -ne 1 ] && exit 1
}

p4u's avatar
p4u committed
45
46
47
print_query() {
	# If the query is a directory
	[ -d "$BMX6_DIR/$1" ] &&
hitz's avatar
hitz committed
48
        {
p4u's avatar
p4u committed
49
50
	# If /all has not been specified
		[ -z "$QALL" ] &&
51
		{
p4u's avatar
p4u committed
52
53
54
55
56
57
58
		total=$(ls $BMX6_DIR/$1 | wc -w)
		i=1
        	echo -n "{ \"$1\": [ "
	        for f in $(ls $BMX6_DIR/$1); do
			echo -n "{ \"name\": \"$f\" }"
			[ $i -lt $total ]  && echo -n ','
			i=$(( $i + 1 ))
59
        	done
p4u's avatar
p4u committed
60
	        echo -n " ] }"
hitz's avatar
hitz committed
61

p4u's avatar
p4u committed
62
63
64
65
66
	# If /all has been specified, printing all the files together
		} || {
		comma=""
		echo -n "[ "
		for entry in "$BMX6_DIR/$1/"*; do
67
			[ -f "$entry" ] &&
p4u's avatar
p4u committed
68
69
70
71
72
73
74
			{
				${comma:+echo "$comma"}
				tr -d '\n' < "$entry"
				comma=","
			}
		done
		echo -n " ]"
75
		}
hitz's avatar
hitz committed
76
77
        }

p4u's avatar
p4u committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
	# If the query is a file, just printing the file
	[ -f "$BMX6_DIR/$QUERY" ] && cat "$BMX6_DIR/$QUERY";
}

if [ "${QUERY##*/}" == "all" ]; then
	QUERY="${QUERY%/all}"
	QALL=1
fi


if [ "$QUERY" == '$neighbours' ]; then
	QALL=1
	echo '{ "neighbours": [ '
	echo '{ "originators": '
	print_query originators
	echo '}, '
	echo '{ "descriptions": '
	print_query descriptions
	echo "} ] }"
	exit 0
else
	check_path "$BMX6_DIR/$QUERY"
	print_query $QUERY
	exit 0
fi


hitz's avatar
hitz committed
105
106
107
ls -1F "$BMX6_DIR"
exit 0