#!/bin/sh
######################################################################
# tirblog - Generate tir blog for directory tree.
# Created 2006-8-15 by David Meyer.
######################################################################
# Identification #####################################################
PROGRAM='tirblog'
VERSION=0.0
COPYRIGHT='Copyright (C) 2006 David Meyer'
DESCRIPTION='Generate tir blog for directory tree.'
USAGE="Usage: $0 [-Vh] [DIRECTORY]"
CONTACT='David Meyer <papa@jtan.com>'
# Initialize environment #############################################
unset IFS
PATH=
BASENAME=/usr/bin/basename
CAT=/bin/cat
ECHO=/bin/echo
FIND=/usr/bin/find
GREP=/bin/grep
HEAD=/bin/head
LS=/bin/ls
SED=/bin/sed
TAIL=/usr/bin/tail
: ${TIRROOT:=/mnt/card/Meyer21C}
# Arguments ##########################################################
while getopts Vh option
do case $option in
V ) echo"$PROGRAM $VERSION"
$ECHO $COPYRIGHT
exit 0
;;
h ) $CAT << ENDHELP
$USAGE
$DESCRIPTION
DIRECTORY defaults to current directory.
Options:
-V Display version number
-h Display this help message
Report bugs to $CONTACT.
ENDHELP
exit 0
;;
* ) $ECHO $USAGE >&2
exit 1
;;
esac
done
shift $(( $OPTIND-1 ))
HERE=${1-.}
# Functions ##########################################################
cat_filter_root_path() {
if [ $HERE -ef $TIRROOT ]
then $SED -e 's:\.\./:./:' $1
else $CAT $1
fi
}
inherit_fn() {
fn=${2:?"Insufficient arguments: inherit_fn $@"}
if [ -f $fn ]
then eval $1=$fn
elif [ -f $TIRROOT/$fn ]
then eval $1=$TIRROOT/$fn
else eval $1=${3:-/dev/null}
fi
}
set_long_day() {
if [ $# -lt 2 ]
then $ECHO "$0: set_long_day: Insufficient arguments" >&2
exit 1
fi
case $2 in
Sun | Mon | Fri )
eval $1=${2}day ;;
Tue ) eval $1=Tuesday ;;
Wed ) eval $1=Wednesday ;;
Thu ) eval $1=Thursday ;;
Sat ) eval $1=Saturday ;;
* ) eval $1='???' ;;
esac
}
set_long_month() {
if [ $# -lt 2 ]
then $ECHO "$0: set_long_month: Insufficient arguments" >&2
exit 1
fi
case $2 in
Jan ) eval $1=January ;;
Feb ) eval $1=February ;;
Mar ) eval $1=March ;;
Apr ) eval $1=April ;;
May ) eval $1=May ;;
Jun ) eval $1=June ;;
Jul ) eval $1=July ;;
Aug ) eval $1=August ;;
Sep ) eval $1=September ;;
Oct ) eval $1=October ;;
Nov ) eval $1=November ;;
Dec ) eval $1=December ;;
* ) eval $1='???' ;;
esac
}
parse_blog_ls_e() {
if [ $# -lt 8 ]
then $ECHO "$0: parse_blog_ls_e: Insufficient arguments" >&2
exit 1
fi
set_long_day weekday $3
set_long_month month $4
date="$weekday, $month $5, $7"
eval $1='$date'
eval $2=$8
}
parse_blog_file() {
if [ $# -lt 4 ]
then $ECHO "$0: parse_blog_file: Insufficient arguments" >&2
exit 1
fi
# <file> ::= ./<category>/<title>.<ext>
base=${4%.*}
path=${base%/*}
category=${path#./}
if [ x"$category" = x"." ]
then eval $1=Main
else eval $1=$category
fi
eval $2=${base##*/}
if [ x"$base" = x"$4" ]
then eval $3=
else eval $3=${4##*.}
fi
}
# Main driver ########################################################
cd $HERE
test -f .title && title=`$CAT .title`
test -f $TIRROOT/.title && site=`$CAT $TIRROOT/.title`
if [ x"${title:=`$$BASENAME $HERE`}" = x"." ]
then title="Site under construction"
fi
$ECHO "<h1>$title</h1>"
$ECHO '<h2>New and Updated</h2>'
$ECHO '<div id="blog">'
articles=`$FIND . -name "[A-Z0-9]*.html"`
prev_date=''
$LS -te $articles | while read blog
do parse_blog_ls_e blog_date blog_file $blog
if [ x"$blog_date" != x"$prev_date" ]
then test "$prev_date" && $ECHO '<hr />'
$ECHO "<h3>$blog_date</h3>"
prev_date="$blog_date"
fi
parse_blog_file blog_category blog_title blog_ext $blog_file
$ECHO "<p style="float:right">(<a href=\"${blog_category}/index.html\">$blog_category</a>)</p>"
$ECHO "<h4><a href=\"$blog_file\">$blog_title</a></h4>"
if [ -f $blog_category/$blog_title.txt ]
then blog_teaser=`$HEAD -n3 $blog_category/$blog_title.txt | $TAIL -1`
elif [ -f $blog_category/$blog_title ]
then blog_teaser=`$HEAD -n3 $blog_category/$blog_title | $TAIL -1 | cut -d' ' -f2-`
else blog_teaser=`$GREP -A10 '<h1>' $blog_file | $TAIL -10 | $GREP -E '<p>|<li>|<td>|<blockquote>|<dt>' | $HEAD -n1 | $SED -e 's/<[^<]*>//g'`
fi
if [ "$blog_teaser" ]
then $ECHO -n "<p style="clear=both">$blog_teaser "
$ECHO "<a href=\"$blog_file\">…</a></p>"
fi
done
test $prev_date && $ECHO '<hr />'
$ECHO '</div><!-- end div blog -->'
exit 0