~zethra/stargazer

8e84ea5018026cda0972c49e83ab79bf1a42d7c8 — Ben Aaron Goldberg 16 days ago ef06cc8
Add man cgi script to test_data
1 files changed, 40 insertions(+), 0 deletions(-)

A test_data/cgi-bin/man
A test_data/cgi-bin/man => test_data/cgi-bin/man +40 -0
@@ 0,0 1,40 @@
#!/bin/sh
search() {
	if [ "$QUERY_STRING" = "" ]
	then
		printf "10 Search for a POSIX man page\r\n"
		return
	fi

	result=$(find /usr/share/man/ -name "$QUERY_STRING.*p.gz" | head -n1)
	if [ -z "$result" ]
	then
		printf "51 No results\r\n"
		return
	fi

	result="$(basename -- "$result")"
	section="$(echo "$result" | cut -d. -f2 | cut -c1)"
	page="$(echo "$result" | cut -d. -f1)"

	printf "30 gemini://$SERVER_NAME$SCRIPT_NAME/$section/$page\r\n"
}

if [ "$PATH_INFO" = "" ] || [ "$PATH_INFO" = "/" ]
then
	search
	exit
fi

section="$(echo "$PATH_INFO" | cut -d/ -f2)"
page="$(echo "$PATH_INFO" | cut -d/ -f3)"

file="/usr/share/man/man$section/$page.${section}p.gz"
if ! [ -f "$file" ]
then
	printf "51 Not found\r\n"
	exit
fi

printf "20 text/plain\r\n"
man -T ascii -O width=72 -- "$section" "$page" | col -b