+#!/usr/bin/perl
+# Author: Alan J. Pippin
+# Description: Find videos from a watched dir and move them to the video processing destination directory
+
+use File::Copy;
+use File::Basename;
+use Getopt::Std;
+use File::stat;
+use Time::localtime;
+use File::Pid;
+
+####################################################################################################
+# Configuration parameters
+$mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir); unshift @INC,("$mydir");
+# Default configuration values
+require "organize_videos.conf";
+# Override defaults with local customizations
+if( -f "$mydir/organize_videos.conf.local") { require "organize_videos.conf.local"; }
+
+####################################################################################################
+
+# Sanity check
+if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
+if(! -d $watchpathname) { print "-E- Can't find watchpath: $watchpathname\n"; exit 1; }
+
+getopts("hfvt");
+
+sub usage {
+ print "usage: $0 [-v] [-t]\n";
+ print " -v verbose; print file being moved (to).\n";
+ print " -f force it to run by ignoring the \$minage setting in organize_videos.conf\n";
+ print " -t test; print what will happen, but don't do anything\n";
+ return 1;
+}
+
+# Sanity checks / Option processing
+if(defined $opt_h) { usage(); exit 1; }
+
+# Our srcpathname is actually our dstpathname
+$find_changed_cmd =~ s/$srcpathname/$watchpathname/g;
+$find_cmd_with_mkv =~ s/$srcpathname/$watchpathname/g;
+$dstpathname = $srcpathname;
+$srcpathname = $watchpathname;
+
+# Only proceed if no files have changed in the past $cmin minutes
+$changed_files_found=`$find_changed_cmd`;
+if(!$opt_f && $changed_files_found) { exit 0; }
+
+# Only proceed if there are video files to organize
+$video_files_found=`$find_cmd_with_mkv`;
+if(!$video_files_found) { exit 0; }
+
+# Only one instance of this script running at a time
+my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
+print "pid_file: $pid_file\n" if($opt_v);
+exit if $pidfile->running();
+$pidfile->write();
+
+# Print the date
+system("date");
+
+# Merge videos prior to copying them over to the destination path
+my $errno = 0;
+
+# Move the videos over to the destination path
+chdir "$srcpathname";
+print "$video_files_found\n" if($opt_v);
+foreach $file (`$find_cmd_with_mkv`) {
+
+ chomp($file);
+ $srcdir = dirname($file);
+ $file = basename($file);
+ $srcfile = $file;
+ $dstfile = "$dstpathname/$file";
+
+ # only move files that have been organized
+ if($srcfile !~ /\d\d\d\d-\d\d-\d\d/) { next; }
+
+ print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
+
+ if(!defined $opt_t) {
+ # Make sure the dstfile doesn't exist, if it does, don't do the move
+ if(! -f "$dstfile") {
+ $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
+ if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
+ } else {
+ print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
+ }
+ # Fix the permissions
+ system("chown $owner \"$dstfile\"");
+ system("chgrp $group \"$dstfile\"");
+ system("chmod $mode \"$dstfile\"");
+ }
+}
+
+$pidfile->remove();
+
+print "\n\n";