#!/bin/bash
# By Linc 10/1/2004
# Find the latest script at http://lincgeek.org/bashpodder
# Revision 1.21 12/04/2008 - Many Contributers!
# If you use this and have made improvements or have comments
# drop me an email at linc dot fessenden at gmail dot com
# and post your changes to the forum at http://lincgeek.org/lincware
# I'd appreciate it!

# Make script crontab friendly:
cd $(dirname $0)

# datadir is the directory you want podcasts saved to:
#datadir=$(date +%Y-%m-%d)
datadir=/media/hd0
BP=/etc/sysconfig/version/bp.conf
XSL=/etc/sysconfig/version/parse_enclosure.xsl
LOG=${datadir}/log/version.log
TEMPLOG=${datadir}/log/temp.log


# Delete any temp file:
rm -f $TEMPLOG

# Read the bp.conf file and wget any url not already in the podcast.log file:
while read podcast
	do
	file=$(/usr/bin/xsltproc --stringparam product $PRODUCT ${XSL} $podcast 2> /dev/null || /usr/local/bin/wget -t 1 -T 10 -q $podcast -O - | tr '\r' '\n' | tr \' \" | /bin/sed -n 's/.*urlODOD="\([^"]*\)".*/\1/p')
	for url in $file
		do
		echo $url >> ${TEMPLOG}
		if ! /bin/grep "$url" ${LOG} > /dev/null
			then
			/usr/local/bin/wget -t 1 -T 10 -U BashPodder -c -q -O $datadir/$(echo "$url" | /bin/awk -F'/' {'print $NF'} | /bin/awk -F'=' {'print $NF'} | /bin/awk -F'?' {'print $1'}) "$url"
		fi
		done
	done < ${BP}
	
# Move dynamically created log file to permanent log file:
cat ${LOG} >> ${TEMPLOG}
/bin/sort ${TEMPLOG} | uniq > ${LOG}
/bin/rm ${TEMPLOG}

# Create an m3u playlist:
#ls $datadir | grep -v m3u > $datadir/podcast.m3u
