#!/usr/bin/perl use strict; use File::Copy; use Getopt::Std; my %opt; getopts( 'vsd', \%opt ) or usage(); usage() if ($opt{h}); my $verbose = $opt{v}; my $simulate = $opt{s}; my $daemon = $opt{d}; my $file_delay = 3; # seconds before moving a file - hack to reduce instances of moving files-in-progress my $loop_delay = 5; # seconds between each check my $src_path = '/x/dropbox'; my %dest_path = ( 'torrent' => '/x/torrent/dropbox', 'nzb' => '/x/usenet/dropbox', ); while (1) { for (glob("$src_path/*")) { my $type = get_type($_); my $dest = $dest_path{$type}; $verbose and printf "%-10s %s $dest\n",$type,$_,$dest; if (!$simulate && $dest) { sleep $file_delay; # hack to reduce instances of moving files-in-progress move($_,$dest) or warn "move($_,$dest): $!\n"; } } last unless $daemon; sleep $loop_delay; } sub get_type { my ($f) = @_; $f=~/\.part$/ and return; # automatically skip firefox partial transfers $f=~/\.nzb$/ and return 'nzb'; $f=~/\.torrent$/ and return 'torrent'; my $t = `file -b $f`; $t=~/BitTorrent/ and return 'torrent'; $t=~/XML\s+document\s+text/i and return 'nzb'; # right now this will report any XML document as NZB # to fix, we'd open the file and scan # but this is an unlikely problem, so i'll be lazy and not do that # no type found? undef return undef; } sub usage { die "Syntax: $0 [-v] [-s] [-d]\n"; }