Fixed up scripts to support importing videos from phones
[videoscripts/.git] / move_videos_from_watchdir
1 #!/usr/bin/perl
2 # Author: Alan J. Pippin
3 # Description: Find videos from a watched dir and move them to the video processing destination directory
4
5 use File::Copy;
6 use File::Basename;
7 use Getopt::Std;
8 use File::stat;
9 use Time::localtime;
10 use File::Pid;
11
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"; }
19
20 ####################################################################################################
21
22 # Sanity check
23 if(! -d $srcpathname) { print "-E- Can't find srcpath: $srcpathname\n"; exit 1; }
24 if(! -d $watchpathname) { print "-E- Can't find watchpath: $watchpathname\n"; exit 1; }
25
26 getopts("hfvt");
27
28 sub usage {
29     print "usage: $0 [-v] [-t]\n";
30     print "   -v        verbose; print file being moved (to).\n";
31     print "   -f        force it to run by ignoring the \$minage setting in organize_videos.conf\n";
32     print "   -t        test; print what will happen, but don't do anything\n";
33     return 1;
34 }
35
36 # Sanity checks / Option processing
37 if(defined $opt_h) { usage(); exit 1; }
38
39 # Our srcpathname is actually our dstpathname
40 $find_changed_cmd =~ s/$srcpathname/$watchpathname/g;
41 $find_cmd_with_mkv =~ s/$srcpathname/$watchpathname/g;
42 $dstpathname = $srcpathname;
43 $srcpathname = $watchpathname;
44
45 # Only proceed if no files have changed in the past $cmin minutes
46 $changed_files_found=`$find_changed_cmd`;
47 if(!$opt_f && $changed_files_found) { exit 0; }
48
49 # Only proceed if there are video files to organize
50 $video_files_found=`$find_cmd_with_mkv`;
51 if(!$video_files_found) { exit 0; }
52
53 # Only one instance of this script running at a time
54 my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
55 print "pid_file: $pid_file\n" if($opt_v);
56 exit if $pidfile->running();
57 $pidfile->write();
58
59 # Print the date
60 system("date");
61
62 # Merge videos prior to copying them over to the destination path
63 my $errno = 0;
64
65 # Move the videos over to the destination path
66 chdir "$srcpathname";
67 print "$video_files_found\n" if($opt_v);
68 foreach $file (`$find_cmd_with_mkv`) {
69
70     chomp($file);
71     $srcdir = dirname($file);
72     $file = basename($file);
73     $srcfile = $file;
74     $dstfile = "$dstpathname/$file";
75
76     # only move files that have been organized
77     if($srcfile !~ /\d\d\d\d-\d\d-\d\d/) { next; }
78     
79     print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
80
81     if(!defined $opt_t) {
82         # Make sure the dstfile doesn't exist, if it does, don't do the move
83         if(! -f "$dstfile") {
84             $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
85             if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
86         } else {
87             print "-> Skipping \"$srcdir/$srcfile\". Destfile \"$dstfile\" already exists.\n";
88         }
89         # Fix the permissions
90         system("chown $owner \"$dstfile\"");
91         system("chgrp $group \"$dstfile\"");
92         system("chmod $mode \"$dstfile\"");
93     }
94 }
95
96 $pidfile->remove();
97
98 print "\n\n";