/services/dict.cgi

#!/usr/bin/env bash
# dict.bash - Dictionary Script
# Author: Sean B. Palmer, inamidst.com
# Derived in part from: 
#    http://inamidst.com/inside/man
#    Credit: John Cowan, ccil.org/~cowan
# License: GPL 2; share and enjoy!

CACHE=/home/sbp/dict
WORD=${PATH_INFO#*/}

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

if [ -z "$WORD" ]
then (echo '<title>Dictionary Lookup</title>'
      echo '<p>Example: <a href="/dict/love">dict love</a>.</p>'
      echo '<address><a href="/">inamidst.com</a></address>') | \
       serve 200 text/html
elif echo "$WORD" | egrep '^[A-Za-z]+$' &> /dev/null; (( $? ))
then echo Not Found | serve 404 text/plain
else ((RECENT=$(date +%s) - 2592000))
   if [ -f "$CACHE/$WORD" ]
   then MTIME=$(find "$CACHE/$WORD" -printf %T@)
   else ((MTIME=0))
   fi
   if [[ (! -f "$CACHE/$WORD") || (($RECENT > $MTIME)) ]]
   then echo -en "DEFINE * $WORD\r\nQUIT\r\n" | \
      nc dict.org 2628 | \
      egrep -v '^2[0-9][0-9]' | \
      sed 's/^15[0-9] //g; s/^\.//g' > "$CACHE/$WORD" \
        && echo '-- dict.org:2628' >> "$CACHE/$WORD"
   fi
   serve 200 text/plain "$CACHE/$WORD"
fi