summaryrefslogtreecommitdiff
path: root/search_cgi
blob: 68031bb1a40f2c1ca556430a9f0eaac93da9207b (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 (){
	echo "<ul>\n"
	l=$(echo "$@" | tr ' ' '\n')
	for r in $l; do 
		echo "<li><div><a href=\"$r\" target='new'>$r</a></div></li>\n"
	done
	echo "</ul>\n"
}


printf "Content-type: text/html\n\n"

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

##func 
print_head(){

	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'></input></div>"
	echo "<div>in: <input type='radio' name='type' value='url' checked>url</input>"
	echo "<input type='radio' name='type' value='descr'>description</input>"
	echo "<input type='radio' name='type' value='read'>readme</input>"
	echo "<input type='radio' name='type' value='all'>all</input>"
	echo "</div>"
	echo "<input type='submit' value='Search!'></input>"
	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" $curstr
		fi
	fi
fi

print_foot