#!/usr/bin/bash
#
# txt2rss - convert a simple txt file to a rss feed file
# Authors:   
# Carlos Roberto do Nascimento Costa (crncosta@gmail.com)
# Paulo Ricardo Paz Vital (pvital@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.  

genDescription(){
    LINES=$@
    NUM=`echo $LINES | wc -w`
    if [[ $NUM -gt "30" && $ENTIRE_DESC == "FALSE" ]]
    then
        COUNT=0
        TEXT=" "
        for i in `echo $LINES | tr -cs [:graph:] '\n'`
        do
            if [ $COUNT -lt "30" ]
            then
               TEXT="${TEXT} ${i}"
               COUNT=$((COUNT+1))
            fi
        done
        LINES="${TEXT}... (<a href=\"${LINK}\">see more</a>)"
    fi
    echo "<description>$LINES</description>" >> $destname 
    return

}

insertHeader(){
	if [ -f $destname ]; then
		echo "removing oldest destination file..."
		rm --verbose $destname
	fi
	LINK=$LINK"/"$destname
	cat << EOF > $destname
<?xml version="1.0" encoding="$ENCODING"?>
<?xml-stylesheet type="text/css" href="http://txt2rss.googlecode.com/files/feed.css"?>
<rss version="2.0" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/">
<channel>

<title>$TITLE</title>
<link>$LINK</link>
<description>$DESCRIPTION</description>
<webMaster>$WEBMASTER</webMaster>

<!-- Recent feed -->
EOF
}

insertMiddle(){
	count=0
	cat $filename | while read line; do
		if [ "${line:0:1}" != "+" ]; then
			count=$[count+1]
			case $count in
				( 1 ) echo "<item>" >> $destname
					  pubDate=`echo $line |  sed -e 's/.*in: //g' -e 's/)//g'`
					  title=`echo $line | sed -e 's/ (P.*//g'`
				      echo "<title>$title</title>" >> $destname
					  echo "<link>$PERMANLINK</link>" >> $destname
				      echo "<guid>$PERMANLINK</guid>" >> $destname ;;
				( 2 ) genDescription $(echo $line)
				      echo "<pubDate>$pubDate</pubDate>" >> $destname
					  echo "</item>" >> $destname
					  count=0;;
			esac
		fi
	done
}

insertFooter(){
	cat << EOF >> $destname

</channel>
</rss>
EOF
}

### MAIN ###
# if configfile exist, export variables, else exit!
# Reading configuration file
if [ -e "/etc/txt2rss/txt2rss.conf" ]; then
	. /etc/txt2rss/txt2rss.conf
	export TITLE=$TITLE
	export LINK=$LINK
	export DESCRIPTION=$DESCRIPTION
	export WEBMASTER=$WEBMASTER
	export PERMANLINK=$PERMANLINK
    export ENTIRE_DESC=$ENTIRE_DESC
    export ENCODING=$ENCODING
else
	printf "ERROR: I can't find the file %s\n" "txt2rss.conf"
	exit 666
fi
# doing the parser of commandline options and 
# try to found filename, error in other case.
if [ $# == 2 ]; then
	filename=$1
	destname=$2
	if [ -f $filename ]; then
		# do the sequence: header->middle->footer
		insertHeader
		insertMiddle
		insertFooter
	else
		printf "ERROR: I can't find the file %s\n" $filename
		exit 666
	fi
else
    echo "txt2rss"
	echo "USAGE: $0 <input file> <output file>"
	echo "	The input needs to be in documented format..."
	echo "	(see documentation before use it)"
	echo "	and the output will be in RSS format!"
	exit 0
fi

