#!/bin/bash

# $Id: nb-build-index.bash,v 1.2 2005/01/02 06:20:57 schwehr Exp $

# nanoblogger extension by Kurt Schwehr

# This script goes through the .txt files in the current directory.
# It takes the list of phrases and for each phrase generates a list of
# links.  In the future, these numbers will hopefully match the blog
# entries, but for now, they are just sequential for each entry.
# It also does not currently handle wrapping or tags between words.

# To get an initial list of words, you might do something like this,
# but know that it will not catch phrases like "San Diego"
#
# cat *.txt | tr '#"=<>,.;:/\\' ' ' | tr ' \t' '\n\n'  | egrep '[a-z]' | sort -u > words

# Sees my "AUTHOR: Kurt" in each.  Nedd to strip out the first 6 lines

phrases=$1

declare -i num=0

cat $phrases | while read phrase; do
    echo -n "$num "
    echo $phrase > tmp-$num.phrase
    # FIX: turn files into one line each
    grep -l "$phrase" *.txt > tmp-$num.matches
    num=$[num+1]
done

echo 
echo "Assempting index from matches"


out=phrase-index.html
rm -f $out
for file in tmp-*.phrase; do
    num=1
    base=${file%%.phrase}
    phrase=`cat $file`
    #echo phrase is $phrase
    echo "<b>$phrase:</b>" >> $out
    cat $base.matches | while read filematch; do
	month=`echo $filematch | cut -c1-7`
	echo "<a href=\"$month.html#e$filematch\">$num</a>" >> $out
	num=$[num+1]
    done
    echo "<br>" >> $out
done
