#!/bin/sh
# $Id: rss-sync.sh 201 2010-10-19 14:35:36Z rick $
#
# Crude attempt to keep torrent in sync from FreeBSD RSS feed
# eq autofeed them into transmission. to be run from cron:
#
# 3 0 */3 * * transmission /data/transmission/rss-sync.sh
#
# Also make sure your transmission daemon is set to to auto-accept torrent
# files put in $TORRENTQUEUE directory
#
# Licence: BSDLike - http://rickvanderzwet.nl/LICENSE
# Rick van der Zwet <info@rickvanderzwet.nl>

TORRENTLIST=/data/transmission/freebsd-rss.txt
TORRENTQUEUE=/data/transmission/upload
TORRENTQUEUE=/tmp/rrs

# Cleanup bits
TMPFILE=`mktemp /tmp/$(basename $0).XXXXX`
trap "rm $TMPFILE; exit 1" 1 2 3 15
trap "rm $TMPFILE; exit 0" 0 

# More sure we can write to the permanent location
touch $TORRENTLIST || exit 1

# Check for new ones
fetch -qo - http://torrents.freebsd.org:8080/rss.xml | awk -F\" '/type="application\/x-bittorrent"/ {print $2}'  > $TMPFILE
if [ \! -s "$TMPFILE" ]; then
  echo "Failed to download RSS feed"
  exit 1
fi

NEWURLS=`comm -13 $TORRENTLIST $TMPFILE`
if [ -z "$NEWURLS" ]; then
  exit 0
fi

# Download the new ones please
fetch -o $TORRENTQUEUE $NEWURLS || exit 1

# All succesfull, lets make that list permanent
cat $TMPFILE > $TORRENTLIST
