/services/man.cgi

#!/usr/bin/env bash
# man.bash - Man Page Script
# Author: Sean B. Palmer, inamidst.com
# Augmentor: John Cowan, ccil.org/~cowan
# License: GPL 2; share and enjoy!

CACHE=/home/sbp/man
PAGE=${PATH_INFO#*/}

function serve () { 
   echo -e "Status: $1\nContent-Type: $2\n"
   cat ${3:-/dev/stdin}
}

if [ -z "$PAGE" ]
then (echo '<title>Man Pages</title>'
      echo '<p>Example: <a href="/man/cat">man cat</a>.</p>'
      echo '<address><a href="/">inamidst.com</a></address>') | \
       serve 200 text/html
elif echo "$PAGE" | egrep '^[A-Za-z_.][A-Za-z_.-]*$' &> /dev/null
   (( $? ))
then echo Not Found | serve 404 text/plain
elif ! man -w "$PAGE" &> /dev/null
then echo No man page found for $PAGE | serve 404 text/plain
else ((RECENT=$(date +%s) - 2592000))
   if [ -f "$CACHE/$PAGE" ]
   then MTIME=$(find "$CACHE/$PAGE" -printf %T@)
   else ((MTIME=0))
   fi
   if [[ (! -f "$CACHE/$PAGE") || (($RECENT > $MTIME)) ]]
   then man "$PAGE" 2> /dev/null | col -b > "$CACHE/$PAGE"
   fi
   serve 200 text/plain "$CACHE/$PAGE"
fi