#!/dcs/bin/bash2

echo Running web-extract at `date`

PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH:~/trees/rhel-3-i686/bin:$HOME/bin:/dcs/bin:/dcs/packages/gnu/bin

# to use this script:
# you'll need par (which is part of the prc.tgz package) and txt2html
# (which is a nice python script for an obvious purpose).  And of
# course, we're assuming you're sync'ing your palm via jpilot from time
# to time at least.

# the following patch to txt2html should make it better suited for
# converting palm memos; after this patch, the first line of the memo
# will be used as the title, rather than the filename:

#--- txt2html.t  2004-10-28 15:58:34.000000000 -0700
#+++ txt2html    2004-10-28 15:59:12.000000000 -0700
#@@ -34,8 +34,9 @@
#   return '<img src="%s" alt="%s"/ >' % (img, img)
# 
# def main(filename):
#-  title = re.sub('(.*)[.].*$', '\\1', os.path.basename(filename))
#+  #title = re.sub('(.*)[.].*$', '\\1', os.path.basename(filename))
#   file = open(filename)
#+  title = file.readline()
# 
#   prev_blank = 1
#   img = None

# the script has a special case as a sort of safety check:
# It won't add a memo whose title is less than 5 characters long.
# This should hopefully prevent the mishap where you accidentally
# add a blank line to the titles list, and suddenly find every last one
# of your memos on the internet.  :)

# the script is O(n^2).  This isn't a problem until I have a huge number
# of memos though.  :)

mkdir -p ~/.jpilot/web-extract/files

cd ~/.jpilot/web-extract/files || exit 1

rm -f *

par x ../../Memo32DB.pdb

cp ~/public_html/theme.html ~/public_html/palm-memos.html.new
sort -f ~/.jpilot/web-extract/titles | \
	while read desired_title
	do
		found=false
		for file in *.pdr
		do
			if [ "$(echo $desired_title | wc -c)" -lt 5 ]
			then
				echo Title too short, skipping: "$desired_title" 1>&2
				continue
			fi
			# the title of the document is just the first line of the file,
			# in the PalmOS world
			read possible_title < $file
			if [ "$possible_title" = "$desired_title" ]
			then
				found=true
				fn="$(echo $desired_title | \
					sed \
						-e 's#[^ /A-Za-z0-9\.=\+-]*##g' \
						-e 's#[ /]#-#g' \
						-e 's/$/.html/')"
				(
				txt2html "$file" | tr -d '\0' | sed 's/^------*$/<hr>/' | \
					while read line
					do
						# Reformat long lines.  This probably should be
						# a command line option, or better, an option specified
						# in the titles file.
						if [ "$(echo $line | wc -c)" -gt 80 ]
						then
							echo "$line" | fmt
						else
							echo "$line"
						fi
					done
				echo "<br><br><a href="palm-memos.html">Back</a> to Dan's palm memos</a>"
# this didn't work - probably because it covered multiple URL's
#				echo "<br><img src="cgi-bin/counter.cgi?3" alt="Hit Count"><br>"
				) > ~/public_html/"$fn"
				echo '<br><a href="'$fn'">'$fn'</a>' \
					>> ~/public_html/palm-memos.html.new
				echo "$fn"
			fi
		done
		if [ "$found" != true ]
		then
			echo "$desired_title" not found!!!!!!!!!!!!!!!!!!!!!!!!!!!
		fi
	done

(
	echo '<?php'
	echo '$countind=6;'
	echo "include 'footer-count.php';"
	echo '?>'

) >> ~/public_html/palm-memos.html.new

mv ~/public_html/palm-memos.html.new ~/public_html/palm-memos.html

echo Done with web-extract at `date`
