#!/usr/sbin/nginx
#	cnst: mdoc.su.nginx.conf, 2013-02-14/23
# Deterministic URL shortener for BSD manual pages, written in nginx.conf
# Author: Constantine A. Murenin (cnst)
# http://mdoc.su/ -- short manual page URLs
# http://nginx.conf.mdoc.su/mdoc.su.nginx.conf
# https://github.com/cnst/mdoc.su
# https://bitbucket.org/cnst/mdoc.su
  ############################################################################
 #
# Copyright (c) 2013 Constantine A. Murenin <C++@Cns.SU>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 #
  ############################################################################
server {
	listen *:80;
	listen [::]:80;
	server_name
		mdoc.su
		www.mdoc.su
		*.mdoc.su;
	if ($host != "mdoc.su") {
		rewrite	^	http://mdoc.su$request_uri?	redirect;
	}
	location = / {
		if ($args ~ ^/) {	rewrite	^	/-$args	last;	}
		default_type	text/html;
		return	200
"<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-CA' lang='en-CA'>
<head>
<title>mdoc.su &mdash; Manual Pages for FreeBSD, OpenBSD, NetBSD and DragonFly BSD!</title>
<meta name='description' content='Deterministic URL shortener for BSD manual pages, written in nginx.conf'/>
<meta name='keywords' content='short manual page URLs, man, URL, BSD UNIX'/>
</head>
<body>
<div style='text-align: center;'>
<h1 style='margin: 4pt auto; color: navy;'>mdoc.su</h1>
<h2 style='margin: 4pt auto;'>man pages for FreeBSD, NetBSD, OpenBSD and DragonFly</h2>
<hr style='width: 48em; font-family: monospace; border: none; border-top: 1px solid navy;'/>
<pre style='margin: 0 auto; width: 48em; font-family: monospace; text-align: left;'>Usage:
	<strong>mdoc.su/b/p</strong>
	<strong>mdoc.su/b/p.0</strong>
	<strong>mdoc.su/b/0/p</strong>
where
	b is	-, or 
		f|n|o|d, or 
		FreeBSD|NetBSD|OpenBSD|DragonFly, or 
		same lower case		, and
	p is the name of the manual page, and
	0 is the section number.

Now, what's mdoc?
See:
	<a href='http://mdoc.su/f/mdoc'>http://mdoc.su/f/mdoc</a> &mdash; according to FreeBSD
	<a href='http://mdoc.su/n/mdoc'>http://mdoc.su/n/mdoc</a> &mdash; according to NetBSD
	<a href='http://mdoc.su/o/mdoc'>http://mdoc.su/o/mdoc</a> &mdash; according to OpenBSD
	<a href='http://mdoc.su/d/mdoc'>http://mdoc.su/d/mdoc</a> &mdash; according to DragonFly

Or, if you will,
	<a href='http://mdoc.su/-/mdoc.7'>http://mdoc.su/-/mdoc.7</a>
	<a href='http://mdoc.su/-/7/mdoc'>http://mdoc.su/-/7/mdoc</a>
</pre>
<hr style='width: 48em; font-family: monospace; border: none; border-top: 1px solid darkred;'/>
<p><small>
&copy; 2013 Constantine A. Murenin (cnst)
<br/>
<a href='http://nginx.conf.mdoc.su/mdoc.su.nginx.conf'>nginx.conf</a> |
<a href='https://github.com/cnst/mdoc.su' rel='nofollow'>github</a> |
<a href='https://bitbucket.org/cnst/mdoc.su' rel='nofollow'>bitbucket</a>
</small></p>
<hr/>
<p>nginx/$nginx_version at $host</p>
</div>
</body>
</html>";
	}
	location = /google2a7d1d40a6b37a23.html {
		rewrite ^/(.*)	$1;
		return 200 "google-site-verification: $uri";
	}
	location ^~ /FreeBSD {
		rewrite	"^/FreeBSD([ ,-/].*)?$"	/freebsd$1	last;
		return	404;
	}
	location ^~ /f {
		set	$fb	"http://www.freebsd.org/cgi/man.cgi?query=";
		set	$fs	"&sektion=";
		rewrite	^/f([4-9]|1[0-9])([0-9])([,/].*)?$	/freebsd-$1.$2$3;
		rewrite	"^/freebsd[ -/](?<fr>[0-9]+(\.[0-9]+)+)([,/].*)?$"	/.$3;
		rewrite	^/freebsd([,/].*)?$	/.$1;
		rewrite	^/(?<bf>.)(?<b>,)(.*)$	/$3	last;
		if ($b) {
			rewrite	^/(?<bf>.)(/.*)$	/-$2	last;
		}
		if ($fr) {
			set	$fr	"&manpath=FreeBSD+$fr-RELEASE";
		}
		rewrite	^/./([^/.]+)/([^/]+)$		$fb$2$fs$1$fr	redirect;
		rewrite	^/./([^/]+)\.([1-9])$		$fb$1$fs$2$fr	redirect;
		rewrite	^/./([^/]+)$			$fb$1$fs$fr	redirect;
		rewrite	^/./?$	/	last;
		return	410;
	}
	location ^~ /NetBSD {
		rewrite	"^/NetBSD([ ,-/].*)?$"	/netbsd$1	last;
		return	404;
	}
	location ^~ /n {
		set	$nb	"http://netbsd.gw.com/cgi-bin/man-cgi?";
		rewrite	^/n([3-9])([0-9])([,/].*)?$	/netbsd-$1.$2$3;
		rewrite	"^/netbsd[ -/](?<nr>[0-9]+(\.[0-9]+)+)([,/].*)?$"	/.$3;
		rewrite	^/netbsd([,/].*)?$	/.$1;
		rewrite	^/(?<bn>.)(?<b>,)(.*)$	/$3	last;
		if ($b) {
			rewrite	^/(?<bn>.)(/.*)$	/-$2	last;
		}
		if ($nr) {
			set	$nr	"+NetBSD-$nr";
		}
		rewrite	^/./([a-z]+[0-9]*[kx]?)/([^/]+)/([^/]+)$	$nb$3+$2.$1$nr	redirect;
		rewrite	^/./([^/]+)/([^/]+)$		$nb$2+$1$nr	redirect;
		rewrite	^/./([^/]+)\.([1-9]\.[a-z]+[0-9]*[kx]?)$	$nb$1+$2$nr	redirect;
		rewrite	^/./([^/]+)\.([1-9])$		$nb$1+$2$nr	redirect;
		rewrite	^/./([^/]+)$			$nb$1+$nr	redirect;
		rewrite	^/./?$	/	last;
		return	410;
	}
	location ^~ /OpenBSD {
		rewrite	"^/OpenBSD([ ,-/].*)?$"	/openbsd$1	last;
		return	404;
	}
	location ^~ /o {
		set	$ob	"http://www.openbsd.org/cgi-bin/man.cgi?query=";
		set	$os	"&sektion=";
		rewrite	^/o([2-9])([0-9])([,/].*)?$	/openbsd-$1.$2$3;
		rewrite	"^/openbsd[ -/](?<or>[0-9]+(\.[0-9]+))([,/].*)?$"	/.$3;
		rewrite	^/openbsd([,/].*)?$	/.$1;
		rewrite	^/(?<bo>.)(?<b>,)(.*)$	/$3	last;
		if ($b) {
			rewrite	^/(?<bo>.)(/.*)$	/-$2	last;
		}
		if ($or) {
			set	$or	"&manpath=OpenBSD+$or";
		}
		rewrite	^/./([a-z]+[0-9]*[k]?)/([1-9]|3p)/([^/]+)$	$ob$3$os$2$or&arch=$1	redirect;
		rewrite	^/./([^/.]+)/([^/]+)$		$ob$2$os$1$or	redirect;
		rewrite	^/./([^/]+)\.([1-9]|3p)\.([a-z]+[0-9]*[k]?)$	$ob$1$os$2$or&arch=$3	redirect;
		rewrite	^/./([^/]+)\.([1-9]|3p)$	$ob$1$os$2$or	redirect;
		rewrite	^/./([^/]+)$			$ob$1$os$or	redirect;
		rewrite	^/./?$	/	last;
		return	410;
	}
	location ^~ /DragonFly {
		rewrite	^/DragonFly(BSD)?([,/].*)?$	/d$2	last;
		return	404;
	}
	location ^~ /d {
		set	$db	"http://leaf.dragonflybsd.org/cgi/web-man?command=";
		set	$ds	"&section=";
		rewrite	^/dragonfly(bsd)?([,/].*)?$	/d$2;
		rewrite	^/d(ragon)?fly([,/].*)?$	/d$2;
		rewrite	^/(?<bd>.)(?<b>,)(.*)$	/$3	last;
		if ($b) {
			rewrite	^/(?<bd>.)(/.*)$	/-$2	last;
		}
		rewrite	^/./([^/.]+)/([^/]+)$		$db$2$ds$1	redirect;
		rewrite	^/./([^/]+)\.([1-9])$		$db$1$ds$2	redirect;
		rewrite	^/./([^/]+)$			$db$1$ds	redirect;
		rewrite	^/./?$	/	last;
		return	410;
	}
	error_page	410	=	@410;
	location @410 {
		rewrite	"^/[a-z]{2,}\.?[1-9]?$"	/-$uri	last;
		rewrite "^/[fnod]{2,4}/"	@$uri	last;
		return	404;
	}
	location ~ "^/[a-z]{2,}\.?[1-9]?$" {
		rewrite	^	/-$uri;
	}
	location ~ "^@/[fnod]{2,4}/" {
		internal;
		rewrite	^@(.+)	$1;
		set	$b	,;
		rewrite	^/([^/]*)(?<bf>f)(.*)	/$1$3;
		rewrite	^/([^/]*)(?<bn>n)(.*)	/$1$3;
		rewrite	^/([^/]*)(?<bo>o)(.*)	/$1$3;
		rewrite	^/([^/]*)(?<bd>d)(.*)	/$1$3;
		rewrite	^/(/.*)$	/-$1	last;
		return 403;
	}
	# location /./ doesn't actually work, paths must be are assumed;
	# [#%?] are out, too; but /?/ is supported in "location = /"
	location ~ ^/[*+,.:_|~]/ {	rewrite	^/.(.*)$	/-$1;	}
	location ^~ /-/ {
		rewrite	^/./$	/	last;
		rewrite	"^/.((/[-.0-9:A-Z_a-z]+){1,3})$"	$1;
		if ($uri ~ ^/-/) {	return	404;	}
		rewrite	^/(.*)	$1;
		default_type	text/html;
		if ($b != ",") {
			set	$bf	f;
			set	$bn	n;
			set	$bo	o;
			set	$bd	d;
		}
		set	$ul	"";
		if ($fr) {	set	$fr	" $fr";	}
		if ($nr) {	set	$nr	" $nr";	}
		if ($or) {	set	$or	" $or";	}
		if ($bf) {
			set	$ul
"$ul<li style='margin: 8pt;'><a href='/FreeBSD$fr/$uri'>FreeBSD$fr</a></li>\n";
		}
		if ($bn) {
			set	$ul
"$ul<li style='margin: 8pt;'><a href='/NetBSD$nr/$uri'>NetBSD$nr</a></li>\n";
		}
		if ($bo) {
			set	$ul
"$ul<li style='margin: 8pt;'><a href='/OpenBSD$or/$uri'>OpenBSD$or</a></li>\n";
		}
		if ($bd) {
			set	$ul
"$ul<li style='margin: 8pt;'><a href='/DragonFly/$uri'>DragonFly</a></li>\n";
		}
		return	300
"<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en-CA' lang='en-CA'>
<head>
<title>300 &ndash; Multiple Choices for $uri &there4; mdoc.su &mdash; short man-page URLs</title>
</head>
<body>
<div style='text-align: center;'>
<h1 style='margin: 4pt auto; color: navy;'><a href='http://mdoc.su/' 
style='text-decoration: none; color: navy;'>mdoc.su</a></h1>
<hr style='width: 48em; font-family: monospace; border: none; border-top: 1px solid navy;'/>
<h2 style='margin: 4pt auto;'><small>$server_protocol</small> 300 &mdash; Multiple Choices</h2>
<h3 style='margin: 4pt auto;'>Which <big><strong><tt>$uri</tt></strong></big> is required?</h3>
<ul style='list-style: none; padding: 0;'>
$ul</ul>
<hr style='width: 48em; font-family: monospace; border: none; border-top: 1px solid darkred;'/>
<p><small>&copy; 2013 Constantine A. Murenin (cnst)</small></p>
<hr/>
<p>nginx/$nginx_version at $host</p>
</div>
</body>
</html>";
	}
	location / {	return 403;	}
	access_log	logs/mdoc.su/mdoc.su.access.log	combined;
	error_log	logs/mdoc.su/mdoc.su.error.log	warn;
}