#!/bin/sh
: 'Nightfall express protocol handler.  See:
=> nex://nightfall.city/nex/info/specification.txt
Usage (replace ~/.chawan with ~/.config/chawan if you prefer XDG):
* put nex binary in ~/.chawan/cgi-bin
* put in ~/.chawan/browsecap
nex; /cgi-bin/nex get %h %p %s; cgioutput
* put in ~/.chawan/mailcap
text/x-nex; "$CHA_CGI_DIR"/nex html; x-htmloutput
* enjoy'

case $1 in
html)	printf '<!DOCTYPE html><pre>'
	sed \
'/=>/!s/</\&lt;/g;/=>/!s/>/\&gt;/g;s/"/\&quot;/g;s@^=> \([^ 	]*\)@<a href="\1">\1</a>@g'
	;;
get)	printf '%s\n' "$4" | "$CHA_LIBEXEC_DIR"/nc "$2" "${3:-1900}" | {
		case $4 in */) printf 'Content-Type: text/x-nex\n'; esac
		printf '\n'
		cat
	}
esac
