2 # Author: Alan J. Pippin
3 # Description: Find videos from a watched dir and move them to the video processing destination directory
12 ####################################################################################################
13 # Configuration parameters
14 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
15 # Default configuration values
16 require "organize_videos.conf";
17 # Override defaults with local customizations
18 if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
20 ####################################################################################################
23 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
28 print "usage: $0 [-v] [-t]\n";
29 print " -v verbose; print file being moved (to).\n";
30 print " -f force it to run by ignoring the \$minage setting in organize_videos.conf\n";
31 print " -t test; print what will happen, but don't do anything\n";
35 # Sanity checks / Option processing
36 if(defined $opt_h) { usage(); exit 1; }
38 # Only one instance of this script running at a time
39 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
40 print "pid_file: $pid_file\n" if($opt_v);
41 exit if $pidfile->running();
44 print "watchpathname: $watchpathname\n" if($opt_v);
45 foreach $watchpath (split(';', $watchpathname)) {
46 if(! -d $watchpath) { print "-E- Can't find watchpath: $watchpath\n"; exit 1; }
47 print "checking $watchpath\n" if($opt_v);
49 # Our srcpathname is actually our dstpathname
50 $new_find_changed_cmd = $find_changed_cmd;
51 $new_find_cmd_with_mkv = $find_cmd_with_mkv;
52 $new_find_changed_cmd =~ s/$srcpathname/$watchpath/g;
53 $new_find_cmd_with_mkv =~ s/$srcpathname/$watchpath/g;
54 $dstpathname = $srcpathname;
56 # Only proceed if no files have changed in the past $cmin minutes
57 $changed_files_found=`$new_find_changed_cmd`;
58 print "finding videos: $new_find_changed_cmd\n" if($opt_v);
59 if(!$opt_f && $changed_files_found) { next; }
61 # Only proceed if there are video files to organize
62 $video_files_found=`$new_find_cmd_with_mkv`;
63 print "finding videos: $new_find_cmd_with_mkv\n" if($opt_v);
64 if(!$video_files_found) { next; }
70 # Move the videos over to the destination path
72 print "found: $video_files_found\n" if($opt_v);
73 foreach $file (`$new_find_cmd_with_mkv`) {
76 $srcdir = dirname($file);
77 $file = basename($file);
79 $dstfile = "$dstpathname/$file";
81 # Make sure we have a unique dstfile
83 $video_ext = $srcfile;
84 $video_ext =~ s/.*\.(\S+)$/$1/;
86 $newdstfile_base = $dstfile;
87 $newdstfile_base =~ s/\.[^.]*$//;
89 $newdstfile = "${newdstfile_base}_${suffix}.${video_ext}";
91 } while(-f "$newdstfile");
92 $dstfile = $newdstfile;
95 print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
98 # Make sure the dstfile doesn't exist, if it does, add a unique number to the end
100 $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
101 if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
103 print "-E- Unable to mv $srcdir/$srcfile -> $dstfile because it already exists\n";
105 # Fix the permissions
106 system("chown $owner \"$dstfile\"");
107 system("chgrp $group \"$dstfile\"");
108 system("chmod $mode \"$dstfile\"");