#!/bin/sh
######################################################################
# phlogmap - Generate gopher log (phlog) gophermap
# Created 2010-08-28 by David Meyeri <[email protected]>.
######################################################################

# Identification #####################################################

PROGRAM='phlogmap'
VERSION=0.0
COPYRIGHT='Copyright (C) 2010 David Meyer'
DESCRIPTION='Generate gopher log (phlog) gophermap.'
USAGE="Usage: $0 [-fhV] [-o OUTFIILE] [-F FOOTER] [-H HEADER] [-n NUM] [-s SEL] [DIR]"
CONTACT='David Meyer <[email protected]>'

# Initialize environment #############################################

IFS="
"
PATH=/bin:/usr/bin

FORTUNE_PGM=/usr/pkg/games/fortune

# Arguments ##########################################################

while getopts VfhF:H:o:n:s: option
do      case $option in
       f )     ADD_FORTUNE=1
               ;;
       n )     NUM=$OPTARG
               ;;
       s )     SEL=$OPTARG
               ;;
       o )     OUTFILE=$OPTARG
               ;;
       F )     FOOTERF=$OPTARG
               ;;
       H )     HEADERF=$OPTARG
               ;;
       V )     echo "$PROGRAM $VERSION"
               echo $COPYRIGHT
               exit 0
               ;;
       h )     cat << ENDHELP
$USAGE

$DESCRIPTION
DIR [=/ftp/pub/users/$USER] is phlog root directory.

Options:
       -o OUTFILE
               Output gophermap to file OUTFILE [=DIR/gphermap].
       -F FOOTER
               Insert contents of file FOOTER [=DIR/.phlogfoot]
               in gophermap after phlog article list.
       -H HEADER
               Insert contents of file HEADER [=DIR/.phloghead]
               at top of gophermap.
       -f
               Append fortune at bottom of gophermap (after footer).
       -n NUM
               Display links and summaries of NUM most recently
               added or changed files.
       -s SEL
               Gopher selector SEL [=/users/$USER/] corresponding
               to <dir>
       -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 ))

DIR=${1:-/ftp/pub/users/$USER}
: ${NUM:=5}
: ${SEL:=/users/$USER/}
: ${OUTFILE:=$DIR/gophermap}
: ${FOOTERF:=$DIR/.phlogfoot}
: ${HEADERF:=$DIR/.phloghead}

# Functions ##########################################################

ftitle() {
       bn=$(basename $1)
       case $bn in
       *.txt | *.org )
               title=$(head -n 1 $1)
               ;;
       *.html | *.htm )
               title=$(grep -i "\<title\>" $1 | sed -e "s;</*[tT][iI][tT][lL][eE]>;;g")
               ;;
       * )
               title=$bn
               ;;
       esac
       echo $title
}

# Main driver ########################################################
echo "Generating phlog map for $DIR ..."

owd=$PWD
cd $DIR

if [ -f $OUTFILE ]; then
       cp OUTFILE OUTFILE~
fi

if [ -f $HEADF ]; then
       cat $HEADF
fi

# 20100830 jdm BusyBox find (on Zaurus) doesn't support ! (NOT) expression, so use grep -v to filter out gophermap files for portability.
#ls -lt $(find . -type f -name "[^\.]*" -name "*[^~]" \! -name gophermap) | head -n $NUM |\
show_files=$(find . -type f -name "[^\.]*" -name "*[^~]" | grep -v "^gophermap$")
ls -lt $show_files | head -n $NUM |\
while read p lc o g s dt1 dt2 dt3 fn; do
       ft=$(ftitle $fn)
       loc=$(basename $(dirname $fn))
       echo "0$dt1 $dt2 $dt3 $ft [$loc]        $SEL${fn#*/}"
done

if [ -f $FOOTF ]; then
       cat $FOOTF
fi

if [ -n "$ADD_FORTUNE" -a -x $FORTUNE_PGM ]; then
   echo ""
   echo "FORTUNE"
   $FORTUNE_PGM
fi

echo "Phlog map written to $OUTFILE"

cd $owd

exit 0