#!/bin/sh
###########################################################################
# script to monitor a selection of directories for new .torrent files.
# if found, queue them up in transmission 
# and when done downloading, pause the torrent  
# w 10/20/08 horto
# u 10/29/08 horto - implmented "make transmission stop after seeding 1:1"
# u 10/29/08 horto - v2.0, do away with /torrents and /incoming dirs, and
#                    just monitor /media/movies, /media/TV and /media/mp3
#                    directories for torrents so I don't have to manually
#                    organize everything later!
# u 11/06/08 horto - fixed bug with awk not being found via cron
###########################################################################
TORRENTDIRS="/mnt/HD_a2/media/movies /mnt/HD_a2/media/tv /mnt/HD_a2/media/mp3"
LOG=/mnt/HD_a2/logs/torrentwd.log
TRANSMISSION_REMOTE=/ffp/bin/transmission-remote

# check each incoming TORRENTDIR for new .torrents
for DIR in $TORRENTDIRS; do
  for FILE in $DIR/*.torrent; do
  
    # set download dir to current working dir
    $TRANSMISSION_REMOTE -w $DIR
    
    # pick out .torrent files and queue them up
    if [ "$FILE" != "$DIR/*.torrent" ]; then
        echo [`date`] "$FILE" added to queue. >> $LOG
        $TRANSMISSION_REMOTE -a "$FILE"
        rm "$FILE"
        echo [`date`] "$FILE" deleted. >> $LOG
        sleep 1
    fi
  done
done

# stop completed downloads
for i in `/ffp/bin/transmission-remote -l | grep Done | grep Seeding | grep -v Ratio | /ffp/bin/awk '{print $1}'"`; do
  echo [`date`] stopping torrent id $i >> $LOG
  $TRANSMISSION_REMOTE -t $i -S
done

exit 0

