#!/bin/sh
## function definitions ##
##func
printlines (){
printf "
\\n"
l=$(echo "$@" | tr ' ' '\n')
for r in $l; do
printf "\\n" "$r" "$r"
done
printf "
\\n"
}
##func
print_head(){
printf "Content-type: text/html\\n\\n"
echo ""
echo "Search for:
"
}
##func
print_foot(){
echo ""
}
##func
print_results(){
for line in $@; do
case "$line" in
"--URL")
#echo "$line
"
if [ "$qtype" = "url" -o "$qtype" = "all" ]; then
curstr="url"
else
curstr=""
fi
;;
"--DESCR")
if [ -n "$curstr" ]; then
if [ -n "$results" ]; then
printf "%s results in repo %s
\\n" $numres $curstr
printlines "$results"
else
printf "No results in repo %s
\\n" $curstr
fi
fi
results=""
numres=0
if [ "$qtype" = "descr" -o "$qtype" = "all" ]; then
curstr="description"
else
curstr=""
fi
;;
"--README")
#echo "$line
"
if [ -n "$curstr" ]; then
if [ -n "$results" ]; then
printf "%s results in repo %s
\\n" $numres $curstr
printlines "$results"
else
printf "No results in repo %s
\\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 "%s results in readme files
\\n" $numres
printlines "$results"
else
printf "No results in readme files
\\n"
fi
fi
}
## main loop ##
query=$(echo "${QUERY_STRING}" | sed -E 's/["\\$^?<>@;*`|/()]//g')
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 )
print_results $lines
fi
print_foot