#!/bin/sh

linuxfibel_base=${1:-./}
awk_script=/tmp/`basename $0`.$$

trap 'test -e $awk_script && rm $awk_script' 2 15

test -d $linuxfibel_base || { echo "Verzeichnis $linuxfibel_base existiert nicht"; exit 1; }

cd $linuxfibel_base
test -e vorwort.htm || { echo "Falsches Linuxfibel-Verzeichnis?"; exit 1; }
test -d printversion || mkdir printversion
test -L printversion/images || (cd printversion && ln -s ../images)

cat >	$awk_script << EOF
#!/usr/bin/awk -f

BEGIN {
	DoPrint="true"
	IGNORECASE=1
}

	/<script language="JavaScript">/ { DoPrint = "false" }
	/<\/HEAD>/								{ DoPrint = "true" }
	/<BODY bgcolor/						{ print \$0; DoPrint = "false"; }
   /HIER BEGINNT DER TEXT/				{ getline; getline; DoPrint = "true"; }
	/HIER ENDET DER TEXT/				{ getline; print \$0; DoPrint = "false" }
	/<\/BODY>/								{ DoPrint = "true" }
{
	if ( DoPrint == "true") { print \$0 }
}
EOF

chmod +x $awk_script

for i in *.htm; do
	$awk_script $i > printversion/$i
done

kill -2  $$
