summaryrefslogtreecommitdiff
path: root/search_cgi
blob: b6dccfe9aea13f93728738c63930993e4091ce27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
105
106
107
108
109
110
111
112
113
114
#!/bin/sh

##func 
printlines (){
	printf "<ul>\\n"
	l=$(echo "$@" | tr ' ' '\n')
	for r in $l; do 
		printf "<li><div><a href=\"%s\" >%s</a></div></li>\\n" "$r" "$r"
	done
	printf "</ul>\\n"
}



query=$(echo "${QUERY_STRING}" | sed -E 's/["\\$^?<>@;*`|/()]//g')

##func 
print_head(){

	printf "Content-type: text/html\\n\\n"
	echo "<html><head><style type='text/css'>"
	echo "body{padding: 20px; margin: 40px auto;line-height: 1.6;font-size: 18px; color:#444;}"
	echo "</style></head>"
	echo "<body><div>Search for: </div><form method='GET' action='/cgi-bin/search_cgi'>"
	echo "<div><input type='text' name='query' autofocus /></div>"
	echo "<div>in: <input type='radio' name='type' id='type_url' value='url' checked /><label for='type_url'>url</label>"
	echo "<input type='radio' name='type' id='type_descr' value='descr' /><label for='type_descr'>description</label>"
	echo "<input type='radio' name='type' id='type_readme' value='read' /><label for='type_readme'>readme</label>"
	echo "<input type='radio' name='type' id='type_all' value='all' /><label for='type_all'>all</label>"
	echo "</div>"
	echo "<input type='submit' value='Search!' />"
	echo "</form>"

}

##func
print_foot(){
	echo "</body></html>"
}

terms=$(echo "${query}" | tr '&' '\n' | grep -E "^query=" | sed -E 's/^query=//')
qtype=$(echo "${query}" | tr '&' '\n' | grep -E "^type=" | sed -E 's/^type=//')

print_head

if [ -n "$terms" ]; then 
	search=$(echo "$terms" | sed -E 's/\+/ /g')
	numres=0
	lines=$(./search_repo ./ "$search" 2>&1 )
	for line in $lines; do 
		case "$line" in
			"--URL")
			#echo "$line<br>"
			if [ "$qtype" = "url" -o "$qtype" = "all" ]; then
				curstr="url"
			else	
				curstr=""
			fi
			;; 

			"--DESCR")
			if [ -n "$curstr" ]; then 
				if [ -n "$results" ]; then 
					printf "<div>%s results in repo %s</div>\\n" $numres $curstr
					printlines "$results"
				else 
					printf "<div>No results in repo %s</div>\\n" $curstr
				fi
			fi
			results=""
			numres=0
			if [ "$qtype" = "descr" -o "$qtype" = "all" ]; then
				curstr="description"
			else	
				curstr=""
			fi
			;;

			"--README") 
			#echo "$line<br>"
			if [ -n "$curstr" ]; then 
				if [ -n "$results" ]; then 
					printf "<div>%s results in repo %s</div>\\n" $numres $curstr
					printlines "$results"
				else 
					printf "<div>No results in repo %s</div>\\n" $curstr
				fi
			fi
			results=""
			numres=0
			if [ "$qtype" = "read" -o "$qtype" = "all" ]; then
				curstr="readme"
			else	
				curstr=""
			fi
			;; 

			*)
			numres=$(($((numres)) + 1))
			results="$results $line"
			;;
		esac
	done
	if [ -n "$curstr" ]; then 
		if [ -n "$results" ]; then 
			printf "<div>%s results in readme files</div>\\n" $numres
			printlines "$results"
		else 
			printf "<div>No results in readme files </div>\\n" 
		fi
	fi
fi

print_foot