#!/bin/bash # Root dir (for finding "misguided" torrents) root="/torrents" # Loading dir (for inactives) loading="/torrents/loading" # Watching dir (for actives) watch="/torrents/watch" # Archive dir - leave blank (archive="") if you don't use one archive="/torrents/.archive" # max # torrents at once N=3 # max-1 for comparisons N=$(($N-1)) # Move all "misguided" torrents (thrown into the root dir) to the "loading" dir misguided=`ls "$root" | grep .*\.torrent | replace " " "~"` for torrent in $misguided; do torrent=`echo $torrent | replace "~" " "` # Make all .torrent files rw/r/r chmod 644 "$root/$torrent" mv "$root/$torrent" "$loading/" done # If N or more active torrents, don't do anything active=`ls "$watch" | grep .*\.torrent | grep ".*" -c` if [ $active -gt $N ]; then exit 0; fi # If no inactive torrents, do nothing inactive=`ls "$loading" | grep .*\.torrent | grep ".*" -c` if [ $inactive -eq 0 ]; then exit 0; fi # While we have inactive torrents and less than 3 active, copy over inactives tostart=`ls "$loading" | grep .*\.torrent | replace " " "~"` for torrent in $tostart; do torrent=`echo "$loading/$torrent" | replace "~" " "` active=`ls "$watch" | grep .*\.torrent | grep ".*" -c` if [ $active -gt $N ]; then exit 0; fi if [ -n $archive ]; then cp "$torrent" "$archive/" fi mv "$torrent" "$watch/" done