#!/bin/bash
# export http_proxy=http://wr-linux01.rki.ivbb.bund.de:3128/
source /etc/local/profile.local > /dev/null

SCRIPT=`basename $0`
NEWS=`echo $SCRIPT | sed "s/2mail.*//"`
SUBJECT=`echo ${NEWS} | perl -ne 'print uc($_);'`

touch ${HOME}/tmp/${NEWS}-links.new
touch ${HOME}/tmp/${NEWS}-links.old
[ -s ${HOME}/tmp/${NEWS}-links.old ] && cp -a ${HOME}/tmp/${NEWS}-links.old ${HOME}/tmp/${NEWS}-links.old2
[ -s ${HOME}/tmp/${NEWS}-links.new ] && mv ${HOME}/tmp/${NEWS}-links.new ${HOME}/tmp/${NEWS}-links.old

case ${NEWS} in
    sz) lynx -dump http://www.sueddeutsche.de/ | \
	    grep -e "http://www.sueddeutsche.de/deutschland/schwerpunkt/" \
		 -e "http://www.sueddeutsche.de/ausland/artikel/" | \
	    cut -c 7- >  ${HOME}/tmp/${NEWS}-links.new
	;;
    tp) lynx -dump http://www.heise.de/tp/ | \
	    grep -e "http://www.heise.de/tp/deutsch/" | \
	    cut -c 7- >  ${HOME}/tmp/${NEWS}-links.new
	;;
    *)	echo "Unbekanntes Script"
	exit 1
	;;
esac

#echo start1

# die alten Index-Dateien werden doppelt genannt, um das reposten von alten Meldungen zu verhindern
cat ${HOME}/tmp/${NEWS}-links.new ${HOME}/tmp/${NEWS}-links.old ${HOME}/tmp/${NEWS}-links.old ${HOME}/tmp/${NEWS}-links.old2 ${HOME}/tmp/${NEWS}-links.old2 |sort|uniq -u|while read a
do
	#echo $a
	w3m -dump_source "$a" 2> /dev/null | zgrep ".*" | ${HOME}/bin/${NEWS}.pl > /tmp/${NEWS}_$$.html
	#echo 1
	w3m -dump /tmp/${NEWS}_$$.html > /tmp/${NEWS}_$$_.html
	#echo 2
	(echo $a; echo ; cat /tmp/${NEWS}_$$_.html)| mail -s "${SUBJECT}: `head -1 /tmp/${NEWS}_$$_.html`" tillea@localhost
	#echo 3
	rm /tmp/${NEWS}_$$.html /tmp/${NEWS}_$$_.html
	#echo 4
done
