Updated for Ubuntu 22.04. Also fixed merge videos cmin check
[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
25 getopts("hfvt");
26
27 sub usage {
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";
32     return 1;
33 }
34
35 # Sanity checks / Option processing
36 if(defined $opt_h) { usage(); exit 1; }
37
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();
42 $pidfile->write();
43
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);
48     
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;
55
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; }
60
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; }
65
66     # Print the date
67     system("date");
68     my $errno = 0;
69
70     # Move the videos over to the destination path
71     chdir "$watchpath";
72     print "found: $video_files_found\n" if($opt_v);
73     foreach $file (`$new_find_cmd_with_mkv`) {
74
75         chomp($file);
76         $srcdir = dirname($file);
77         $file = basename($file);
78         $srcfile = $file;
79         $dstfile = "$dstpathname/$file";
80
81         # Make sure srcdir is group writable
82         if(not -w "$srcdir") {
83            print "-W- srcdir is not group writable, can't move videos out of it: $srcdir\n";
84            next;
85         } 
86
87         # Make sure we have a unique dstfile
88         if($overwrite_dest == 0 && -f "$dstfile") {
89             $video_ext = $srcfile;
90             $video_ext =~ s/.*\.(\S+)$/$1/;
91             $suffix = 0;
92             $newdstfile_base = $dstfile;
93             $newdstfile_base =~ s/\.[^.]*$//;
94             do {
95                 $newdstfile = "${newdstfile_base}_${suffix}.${video_ext}";
96                 $suffix++; 
97             } while(-f "$newdstfile");
98             $dstfile = $newdstfile;
99         }
100         
101         print "-> Moving \"$srcdir/$srcfile\" to \"$dstfile\"\n";
102
103         if(!defined $opt_t) {
104             # Make sure the dstfile doesn't exist, if it does, add a unique number to the end
105             #if(! -f "$dstfile") {
106             $errno=system("mv \"$srcdir/$srcfile\" \"$dstfile\" 2>/dev/null");
107             if($errno) { print "-E- Error moving srcfile to dstfile: $srcdir/$srcfile -> $dstfile\n"; next; }
108             #} else {
109             #   print "-E- Unable to mv $srcdir/$srcfile -> $dstfile because it already exists\n";
110             #}
111             # Fix the permissions
112             system("chown $owner \"$dstfile\"");
113             system("chgrp $group \"$dstfile\"");
114             system("chmod $mode \"$dstfile\"");
115         }
116     }
117
118     # Update nextcloud file cache so it knows what files we have moved
119     $watchpath =~ s/$nextcloudbase//g;
120     system("$occ files:scan --path \"$watchpath\" > /dev/null");
121 }
122                     
123 $pidfile->remove();
124
125 #print "\n\n";