#!/bin/sh
# Fix StarOffice slides in this directory
# Usage: sofix [<name of link to place to parent directory> \
#		<in-document title (what follows after the above name> \
#		<title for the document>]
#	If they are not provided, locally sensible defaults are assumed :-)
chap=`pwd | sed 's,.*/,,'`
dirlast="$chap"
imgurl=/images
parname="${1-Rechnersysteme: }"
h1="${2-:$chap}"
title="${3-Rechnersysteme: $chap}"
for i in *.htm
do
	(sed -e 's/\.htm\>/.html/g' \
		-e 's,index.html,./,g' \
		-e 's,img src="first,img src="'$imgurl'/first,' \
		-e 's,img src="last,img src="'$imgurl'/last,' \
		-e 's,img src="next,img src="'$imgurl'/next,' \
		-e 's,img src="prev,img src="'$imgurl'/prev,' \
		-e 's,img src="text,img src="'$imgurl'/text,' \
		-e 's,img src="index,img src="'$imgurl'/index,' | 
		tr -d \\015; echo ""
	) < $i > $i"l" && rm $i
done
#(sed -e 's/\.htm\>/.html/g' \
#     -e '/<h1>/s,.*,<h1><A HREF="../">'"$parname</A>$h1</h1>," \
#     -e "/<title>/s,.*,<title>$title</title>," \
#     -e '/<p align=left>/s,</p>,<BR>,' \
#     -e 's/<p align=left>//' | tr -d \\015; echo ""
#) < index.html > index.html+ && mv index.html+ index.html
# Change in capitalization of <h1> is necessary to make the script idempotent
(sed -e 's/\.htm\>/.html/g' \
     -e 's,<h1>,<H1><A HREF="../">'"$parname</A>," \
     -e "/<title>/s,.*,<title>$title</title>," \
     -e '/<p align=left>/s,</p>,<BR>,' \
     -e 's,mailto:http:,http:,' \
     -e '/Download presentation/s,.*,<a href="../'"$dirlast"'.sdp">Download StarOffice presentation</a><BR><A HREF="../'"$dirlast"'.pdf">Download PDF handouts,' \
     -e 's/<p align=left>//' | tr -d \\015; echo ""
) < index.html > index.html+ && mv index.html+ index.html
[ -f index.gif ] && rm first?.gif last?.gif prev?.gif next?.gif text.gif index.gif
[ -f sologo.gif ] && rm sologo.gif
[ -f index.sdd ] && rm index.sdd
[ -d CVS ] && chmod 700 CVS

# Add ALT and META tags
for file in text*.html
do
    alt="`sed < $file -e '/<li>/!d' -e 's/<[^>]*>//g' -e 's/&#[0-9]*;/ /g' -e 's/"/&quot;/g' -e 's/#/ /g' -e 's/&/%/g' -e 's/$/./' | tr '\012' ' '`"
    num=`echo $file | sed -e 's/^text//' -e 's/\.html$//'`
    sed \
	-e 's&<center><img src=.*&<center><img src="img'$num'.gif" alt="'"$alt"'"></center>&' \
	-e 's&<head>&<HEAD><META NAME="description" CONTENT="'"$alt"'">&' \
	< img$num.html | tr '%' '&' > img$num.html+ &&
	mv img$num.html+ img$num.html
done
