#!/bin/bash
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details. */


cd /home/gnunux/

IDFILE=dijon.fr.id
TMPFILE=dijon.fr.tmp
CHANNEL=dijon.fr.tmp.channel
CONTENT=dijon.fr.tmp.content
TPL_FILE=dijon.sh.template.file
TPL_CONTENT=dijon.sh.template.item
RSS=/var/www/dijon.fr.rss
rm -f cookie.txt

# recuperation d'un cookie de session
wget http://www.dijon.fr/actualites.php -q -O $TMPFILE --save-cookies=cookie.txt --keep-session-cookies

echo -n "s@CHANNEL@"> $CHANNEL
echo -n "s@CONTENT@"> $CONTENT

extract_news ()
{
	cat $TMPFILE | grep '<span class="trebubold11noir">'|while read a
	do
		# recuperation du commentaire
		a=${a#*<noscript>}
		a=${a%</noscript>*}
		# recuperation de l'url
		url=${a#*href=\"}
		url=${url%%\">*}
		# recuperation du contenu
		content=${a%</A>}
		content=${content#*\">}
		# recuperation de l'id
		b=${a%.php*}
		b=${b#*www.dijon.fr/fiche/*.}
		# recuperation de la date
		DATE=`grep "^$b " $IDFILE|awk '{ print $2 }'`
		if [ "$DATE" = "" ]
		then
			DATE=`date +%FT%T%:z`
			echo "$b $DATE" >> $IDFILE
		fi
		echo "    <rdf:li rdf:resource=\"$url\" /> \\" >> $CHANNEL
		sed "s@TITLE@$content@g" $TPL_CONTENT > $CONTENT.tmp
		sed "s@CONTENT@$content@g" -i $CONTENT.tmp
		sed "s@ADDRESS@$url@g" -i $CONTENT.tmp
		sed "s@DATE@$DATE@g" -i $CONTENT.tmp
		iconv -f iso8859-1 -t utf-8 $CONTENT.tmp -o $CONTENT.2.tmp
		cat $CONTENT.2.tmp >> $CONTENT
		rm $CONTENT.tmp $CONTENT.2.tmp
	done
}

extract_news
page=1
while `grep "http://www.dijon.fr/charte/suivant.gif" $TMPFILE > /dev/null`
do
	page=$((page+1))
	wget http://www.dijon.fr/actualites.0.$page.php -q -O $TMPFILE --load-cookies=cookie.txt
	extract_news
done
echo "@g" >> $CHANNEL
echo "@g" >> $CONTENT
sed -f $CHANNEL $TPL_FILE > $TMPFILE
sed -f $CONTENT $TMPFILE > $RSS
sed -i 's/TITLE/Dijon/g' $RSS
sed -i "s/DATE/`date +%FT%T%:z`/g" $RSS
sed -i 's@SITEADDRESS@http://www.dijon.fr/@g' $RSS
rm $TMPFILE
