-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript
707 lines (664 loc) · 24.5 KB
/
script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/bin/bash
#####################################
#
# Created by: zacharyd3
#
#####################################
#
# Declare Variables
#
#####################################
#Start Debugging (set as 1 to enable basic info, 2 shows all steps and comparison info)
debug=2
#Enable testing mode to simulate the move without actually moving any files (0 = moves files, 1 = Test)
testing=0
#Start counting the renaming passes made
passNumber=1
#Set the recycle bin locations
binLocation=/mnt/user/Downloads/.Recycle.Bin/
binMovie=/mnt/user/Downloads/.Recycle.Bin/Movies/
binTV=/mnt/user/Downloads/.Recycle.Bin/TV/
#Set the library locations
movieFolderRoot=/mnt/user/Videos/Movies/
tvFolderRoot=/mnt/user/Videos/TV/
#Set Date and Time variables
currentDate=$(date +'%m/%d/%Y')
currentTime=$(date +'%H:%M')
#If testing is enabled, automatically switch to debug mode 2
if [[ $testing -eq 1 && $debug -le 2 ]]; then
debug=1
echo "<font color='blue'><b>Testing is enabled, enabling advanced debugging.</b></font>"
fi
#####################################
#
# Recycle Bin Upkeep
#
#####################################
echo "<hr>"
echo "<font color='blue'><b>Recycle Bin scan started!</b></font>"
echo ""
echo "<hr>"
#Checks if the movies recycle bin exists
if [[ ! -d "${binMovie}" ]]; then
echo "<font color='orange'><b>Movie bin not found!</b></font>"
#Creates directory if it's missing
mkdir "${binMovie}"
echo "<font color='green'><b>Directory created.</b></font>"
else
echo "<font color='green'><b>Movie bin already exists.</b></font>"
fi
#Checks if the tv show recycle bin exists
if [[ ! -d "${binTV}" ]]; then
echo "<font color='orange'><b>TV Show bin not found!</b></font>"
#Creates directory if it's missing
mkdir "${binTV}"
echo "<font color='green'><b>Directory created.</b></font>"
else
echo "<font color='green'><b>TV Show bin already exists.</b></font>"
fi
echo ""
#####################################
#
# Unneeded File Remover
#
#####################################
#Checks that the Recycle Bin is setup
if [[ -d "${binLocation}" && "$testing" -eq 0 ]]; then
cd "${binLocation}"
#Finds all "extra" files and deletes them before moving any videos. (Originally was one line, however userscripts seems to not like that so each filetype needed to be searched for individually.
find * -name "*.nfo" -type f -delete
find * -name "*.srt" -type f -delete
find * -name "*.ass" -type f -delete
find * -name "*.jpg" -type f -delete
find * -name "*.png" -type f -delete
find * -name "*.xml" -type f -delete
if [ $debug -ge "2" ]; then
echo "<font color='green'><b>Removed extra files (subtitles, images, info). </b></font>"
fi
fi
#####################################
#
# Low-res Movie Sorter
#
#####################################
#Declare variables
passNumber=1
inUse=0
rCheck=0
vCheck=0
echo "<hr>"
echo "<font color='blue'><b>Copying Low-res movies back.</b></font>"
echo ""
echo "<hr>"
#Check the files in the recycle bin
for file in "$binMovie"*
do
#Check if the file is a directory, if so, check it for movies
if [ -d "${file}" ]; then
directory=$file/
for movieFile in "${directory}"*
do
if [ -f "${movieFile}" ]; then
#Parse the movie data from the filenames that were found
if [ $debug -ge "2" ]; then
echo "<font color='green'><b>Original File name: </b></font>""$movieFile"
fi
parseNameOld0=$(echo "$movieFile" | cut -d'/' -f7) #<<Change this number
parseNameOld0=$(echo "$parseNameOld0" | cut -d'[' -f1)
parseNameOld1=$(echo "$parseNameOld0" | cut -d'(' -f1) #Saves the original Movie name too (if the folder has a year it breaks things so this is needed)
parseNameOld0=$(echo "$parseNameOld0" | xargs) #xargs removes leading and trailing whitespace.
parseNameOld1=$(echo "$parseNameOld1" | xargs) #xargs removes leading and trailing whitespace.
parseExt=$(echo "$movieFile" | cut -d'.' -f5)
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Original Movie name: </b></font>""$parseNameOld1 | $parseNameOld0"
fi
#Check if the recycled file is in use.
if lsof "$movieFile" > /dev/null; then
echo "<font color='orange'><b>The file is in use, skipping.</b></font>"
echo ""
((inUse=1))
else
((inUse=0))
fi
#Check if the recycled file is a video
if [[ $movieFile == "*mkv" || "*m2ts" || "*mp4" ]]; then
((vCheck=0)) #Resets the Video Check for a new file
else
if [[ $debug -ge "1" && "$inUse" -eq 0 ]]; then
echo "<font color='orange'><b>File isn't a video, skipping</b></font>"
echo ""
fi
((vCheck=1))
fi
#Check if the recycled file is 1080p.
if [[ $movieFile != *"1080p"* && "$vCheck" -eq "0" && "$inUse" -eq 0 ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='orange'><b>The file is not 1080p, skipping.</b></font>"
echo ""
fi
((rCheck=1))
else
((rCheck=0))
fi
#Turn the parsed movie name into a folder
movieFolder0=$movieFolderRoot$parseNameOld0
movieFolder1="${movieFolder0}/"
if [[ $debug -ge "1" && "$vCheck" -eq "0" && "$rCheck" -eq "0" ]]; then
echo "<font color='green'><b>Searching for 4K Movie folder: </b></font>""$movieFolder1"
fi
#Test if the movie folder exists
if [[ -d "${movieFolder1}" && "$vCheck" -eq "0" && "$rCheck" -eq "0" && "$inUse" -eq "0" ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Found matching 4K Movie Folder.</b></font>"
fi
cd "${movieFolder1}" || exit
#Search the movie folder for a 4K copy
foundNew=$(find ./ -type f \( -iname \*2160p*.mkv -o -iname \*2160p*.m2ts -o -iname \*2160p*.mp4 \) -maxdepth 1 -print -quit)
if [[ "$foundNew" != "" && "$vCheck" -eq "0" && "$rCheck" -eq "0" && "$inUse" -eq "0" ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Found matching 4K Movie: </b></font>""$foundNew"
fi
fi
if [[ "$foundNew" = "" && "$vCheck" -eq "0" && "$rCheck" -eq "0" && "$inUse" -eq "0" ]]; then
echo "<font color='orange'><b>No matching 4K file found, skipping.</b></font>"
echo ""
fi
#Parse the found 4K copy name
if [[ "$foundNew" == *"2160p"* && "$vCheck" -eq "0" ]]; then
parseNameNew0=$(echo "$foundNew" | cut -d'[' -f1)
parseNameNew0=$(echo "$parseNameNew0" | cut -d'/' -f2)
parseNameNew0=$(echo "$parseNameNew0" | xargs) #xargs removes leading and trailing whitespace
if [ $debug -ge "2" ]; then
echo "<font color='red'><b>Name Comparison:</b></font> |" "$parseNameOld1" "|" "$parseNameNew0" "|"
fi
if [[ "${parseNameOld1}" != "${parseNameNew0}" && $debug -ge "2" && $inUse -eq "0" ]]; then
echo "<font color='red'><b>No match found. </b></font>"
echo ""
fi
if [ "${parseNameOld1}" == "${parseNameNew0}" ]; then
if [ "${parseExt}" != "nfo" ]; then
if [ $debug -ge "2" ] && [ $inUse -eq "0" ]; then
echo "<font color='red'><b>Files Checked: </b></font>"$passNumber
fi
((passNumber++))
if [ $testing -eq "0" ]; then
mv "${movieFile}" "${movieFolder1}"
/usr/local/emhttp/webGui/scripts/notify -e "Radarr Copy" -s "Copy Notifcation" -d "$parseNameNew0 $parseExt has been copied back." -i "normal"
echo "$currentDate,$currentTime,Movies,1080p,$parseNameNew0," >> "/mnt/user/Backups/4K_Copier_History.csv"
fi
echo "<font color='blue'><b>""$parseNameNew0"" ""$parseExt"" moved.</b></font>"
echo ""
else
echo "<font color='orange'><b>File is not a video, Skipping </b></font>"
echo ""
fi
fi
fi
fi
fi
done
fi
done
if [[ $passNumber -eq "1" ]]; then
echo "No files to process"
echo ""
fi
#####################################
#
# 4K Movie Sorter
#
#####################################
#Declare variables
passNumber=1
inUse=0
rCheck=0
vCheck=0
echo "<hr>"
echo "<font color='blue'><b>Copying 4K Movies back.</b></font>"
echo ""
echo "<hr>"
#Check the files in the recycle bin
for file in "$binMovie"*
do
#Check if the file is a directory, if so, check it for movies
if [ -d "${file}" ]; then
directory=$file/
for movieFile in "${directory}"*
do
if [ -f "${movieFile}" ]; then
#Parse the movie data from the filenames that were found
if [ $debug -ge "2" ]; then
echo "<font color='green'><b>Original File name: </b></font>""$movieFile"
fi
parseNameOld0=$(echo "$movieFile" | cut -d'/' -f7) #<<Change this number
parseNameOld0=$(echo "$parseNameOld0" | cut -d'[' -f1)
parseNameOld1=$(echo "$parseNameOld0" | cut -d'(' -f1) #Saves the original Movie name too (if the folder has a year it breaks things so this is needed)
parseNameOld0=$(echo "$parseNameOld0" | xargs) #xargs removes leading and trailing whitespace.
parseNameOld1=$(echo "$parseNameOld1" | xargs) #xargs removes leading and trailing whitespace.
parseExt=$(echo "$movieFile" | cut -d'.' -f5)
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Original Movie name: </b></font>""$parseNameOld1"
fi
#Check if the recycled file is in use.
if lsof "$movieFile" > /dev/null; then
echo "<font color='orange'><b>The file is in use, skipping.</b></font>"
echo ""
((inUse=1))
else
((inUse=0))
fi
#Check if the recycled file is a video
if [[ $movieFile == "*mkv" || "*m2ts" || "*mp4" ]]; then
((vCheck=0)) #Resets the Video Check for a new file
else
if [[ $debug -ge "1" && "$inUse" -eq 0 ]]; then
echo "<font color='orange'><b>File isn't a video, skipping</b></font>"
echo ""
fi
((vCheck=1))
fi
#Check if the recycled file is 4K.
if [[ $movieFile != *"2160p"* && "$vCheck" -eq "0" && "$inUse" -eq 0 ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='orange'><b>The file is not 4K, skipping.</b></font>"
echo ""
fi
((rCheck=1))
else
((rCheck=0))
fi
#Turn the parsed movie name into a folder
movieFolder0=$movieFolderRoot$parseNameOld0
movieFolder1="${movieFolder0}/"
if [[ $debug -ge "1" && "$vCheck" -eq "0" && "$rCheck" -eq "0" ]]; then
echo "<font color='green'><b>Searching for Low-res Movie folder: </b></font>""$movieFolder1"
fi
#Test if the movie folder exists
if [[ -d "${movieFolder1}" && "$vCheck" -eq "0" && "$rCheck" -eq "0" && "$inUse" -eq "0" ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Found matching Low-res Movie Folder.</b></font>"
fi
cd "${movieFolder1}"
#Search the movie folder for a 1080p copy
foundNew=$(find ./ -type f \( -iname \*1080p*.mkv -o -iname \*1080p*.m2ts -o -iname \*1080p*.mp4 \) -maxdepth 1 -print -quit)
if [[ "$foundNew" != "" && "$vCheck" -eq "0" && "$rCheck" -eq "0" && "$inUse" -eq "0" ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Found matching Low-res Movie: </b></font>""$foundNew"
fi
fi
if [[ "$foundNew" = "" && "$vCheck" -eq "0" && "$rCheck" -eq "0" && "$inUse" -eq "0" ]]; then
echo "<font color='orange'><b>No matching Low-res file found, skipping.</b></font>"
echo ""
fi
#Parse the found 1080p copy name
if [[ "$foundNew" == *"1080p"* && "$vCheck" -eq "0" ]]; then
parseNameNew0=$(echo "$foundNew" | cut -d'[' -f1)
parseNameNew0=$(echo "$parseNameNew0" | cut -d'/' -f2)
parseNameNew0=$(echo "$parseNameNew0" | xargs) #xargs removes leading and trailing whitespace
if [ $debug -ge "2" ]; then
echo "<font color='red'><b>Name Comparison:</b></font> |" "$parseNameOld1" "|" "$parseNameNew0" "|"
fi
if [[ "${parseNameOld1}" != "${parseNameNew0}" && $debug -ge "2" && $inUse -eq "0" ]]; then
echo "<font color='red'><b>No match found. </b></font>"
echo ""
fi
if [ "${parseNameOld1}" == "${parseNameNew0}" ]; then
if [ "${parseExt}" != "nfo" ]; then
if [ $debug -ge "2" ] && [ $inUse -eq "0" ]; then
echo "<font color='red'><b>Files Checked: </b></font>"$passNumber
fi
((passNumber++))
if [ $testing -eq "0" ]; then
mv "${movieFile}" "${movieFolder1}"
/usr/local/emhttp/webGui/scripts/notify -e "Radarr Copy" -s "Copy Notifcation" -d "$parseNameNew0 $parseExt has been copied back." -i "normal"
echo "$currentDate,$currentTime,Movies,4K,$parseNameNew0," >> "/mnt/user/Backups/4K_Copier_History.csv"
fi
echo "<font color='blue'><b>""$parseNameNew0"" ""$parseExt"" moved.</b></font>"
echo ""
fi
fi
fi
fi
fi
done
fi
done
if [[ $passNumber -eq "1" ]]; then
echo "No files to process"
echo ""
fi
#####################################
#
# TV Show Sorter
#
#####################################
passNumber=1 #Counts how many files have been checked
inUse=0 #Saves if the file is in use and cancels
rCheck=0 #Set to 0 if it's ok to continue, 1 if it should end. (stands for Resolution Check)
vCheck=0 #Set to 0 if it's ok to continue, 1 if it skip the current file. (stands for Video Check)
echo "<hr>"
echo "<font color='blue'><b>Copying Low-res TV Show's back.</b></font>"
echo ""
echo "<hr>"
for file in "$binTV"*
do
#Start checking TV shows.
#if [ "$(ls -A "$file")" ]; then
directory=$file/
for tvSeason in "${directory}"*/*
do
if [ -f "${tvSeason}" ]; then
if [ $debug -ge "2" ]; then
echo "<font color='green'><b>Full name: </b></font>""$tvSeason"
fi
#Parse the show name, episode and season
parseNameOld0=$(echo "$tvSeason" | cut -d'/' -f9)
parseNameOld0=$(echo "$parseNameOld0" | cut -d'[' -f1)
tvShowName=$(echo "$tvSeason" | cut -d'/' -f7)
tvShowSeason=$(echo "$tvSeason" | cut -d'/' -f8)
tvShowEpisode0=$(echo "$tvSeason" | cut -d'/' -f9)
tvShowEpisode1=$(echo "$tvShowEpisode0" | cut -d'-' -f1 | xargs)
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Show name: </b></font>""$tvShowName"
echo "<font color='green'><b>Show episode: </b></font>""$tvShowEpisode1"
fi
#Check if the file is in use.
if lsof "$tvSeason" > /dev/null; then
echo "<font color='orange'><b>File is in use, skipping.</b></font>"
echo ""
inUse=1
else
inUse=0
fi
#Check if the file is a video
if [[ $tvSeason == *"mkv" || *"m2ts" ]]; then
((vCheck=0)) #Resets the Video Check for a new file
((rCheck=0)) #Resets the Continue Check for a new file
else
if [[ $debug -ge "1" && "$inUse" -eq 0 ]]; then
echo "<font color='orange'><b>File isn't a video, skipping</b></font>"
echo ""
fi
((vCheck=1))
fi
#Ensure the recycled file is 1080p
if [[ $tvSeason == *"1080p"* && $vCheck -eq 0 && "$inUse" -eq 0 ]]; then
((rCheck=0))
else
if [[ $debug -ge "1" && $vCheck -eq "0" ]]; then
echo "<font color='orange'><b>The file is not 1080p, skipping.</b></font>"
echo ""
fi
((rCheck=1))
fi
#Parse the folder and Extension
tvShowOriginal=$tvFolderRoot$tvShowName/$tvShowSeason/
tvShowExtension=$(echo "$tvSeason" | cut -d'.' -f5)
#Check to see if the file matches a show
if [[ -d "${tvShowOriginal}" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
cd "${tvShowOriginal}" || exit
if [[ $debug -ge "2" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
echo "<font color='green'><b>Searching in folder: </b></font>""$tvShowOriginal"
fi
#Search the folder for 4K shows
if [[ $debug -ge "2" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
echo "<font color='green'><b>Searching for: </b></font>*${tvShowEpisode1}*2160p*"
fi
foundNew=$(find -type f -iname "*${tvShowEpisode1}*2160p*" -print)
if [[ "$foundNew" != "" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Found matching 4K Show: </b></font>""$foundNew"
fi
fi
if [[ $debug -ge "1" && "$foundNew" = "" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
echo "<font color='orange'><b>No matching 4K file found, skipping.</b></font>"
echo ""
fi
#Parse show name only if 4K filename found
if [[ "$foundNew" == *"2160p"* && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
parseNameNew0=$(echo "$foundNew" | cut -d'[' -f1)
parseNameNew0=$(echo "$parseNameNew0" | cut -d'/' -f2)
if [ $debug -ge "2" ]; then
echo "<font color='red'><b>Name Comparison:</b></font> |" "$parseNameOld0" "|" "$parseNameNew0" "|"
fi
if [ "${parseNameOld0}" == "${parseNameNew0}" ]; then
if [ $debug -ge "2" ] && [ $inUse -eq "0" ]; then
echo "<font color='red'><b>Files Checked: </b></font>"$passNumber
fi
((passNumber++))
if [ $testing -eq "0" ]; then
mv "${tvSeason}" "${tvShowOriginal}"
/usr/local/emhttp/webGui/scripts/notify -e "Radarr Copy" -s "Copy Notifcation" -d "$parseNameNew0 has been copied back." -i "normal"
echo "$currentDate,$currentTime,TV Shows,1080p,$tvShowName: $tvShowEpisode1" >> "/mnt/user/Backups/4K_Copier_History.csv"
fi
echo "<font color='blue'><b>""$tvShowName"" ""$tvShowEpisode1"" ""$tvShowExtension"" moved.</b></font>"
echo ""
fi
fi
fi
fi
done
#fi
done
if [[ $passNumber -eq "1" ]]; then
echo "No files to process"
echo ""
fi
#####################################
#
# 4K TV Show Sorter
#
#####################################
passNumber=1 #Counts how many files have been checked
inUse=0 #Saves if the file is in use and cancels
rCheck=0 #Set to 0 if it's ok to continue, 1 if it should end. (stands for Resolution Check)
vCheck=0 #Set to 0 if it's ok to continue, 1 if it skip the current file. (stands for Video Check)
echo "<hr>"
echo "<font color='blue'><b>Copying 4K TV Show's back.</b></font>"
echo ""
echo "<hr>"
for file in "$binTV"*
do
#Start checking TV shows.
#if [ "$(ls -A "$file")" ]; then
directory=$file/
for tvSeason in "${directory}"*/*
do
if [ -f "${tvSeason}" ]; then
if [ $debug -ge "2" ]; then
echo "<font color='green'><b>Full name: </b></font>""$tvSeason"
fi
#Parse the show name, episode and season
parseNameOld0=$(echo "$tvSeason" | cut -d'/' -f9)
parseNameOld0=$(echo "$parseNameOld0" | cut -d'[' -f1)
tvShowName=$(echo "$tvSeason" | cut -d'/' -f7)
tvShowSeason=$(echo "$tvSeason" | cut -d'/' -f8)
tvShowEpisode0=$(echo "$tvSeason" | cut -d'/' -f9)
tvShowEpisode1=$(echo "$tvShowEpisode0" | cut -d'-' -f1)
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Show name: </b></font>""$tvShowName"
echo "<font color='green'><b>Show episode: </b></font>""$tvShowEpisode1"
fi
#Check if the file is in use.
if lsof "$tvSeason" > /dev/null; then
echo "<font color='orange'><b>File is in use, skipping.</b></font>"
echo ""
inUse=1
else
inUse=0
fi
#Check if the file is a video
if [[ $tvSeason == *"mkv" || *"m2ts" ]]; then
((vCheck=0)) #Resets the Video Check for a new file
((rCheck=0)) #Resets the Continue Check for a new file
else
if [[ $debug -ge "1" && "$inUse" -eq 0 ]]; then
echo "<font color='orange'><b>File isn't a video, skipping</b></font>"
echo ""
fi
((vCheck=1))
fi
#Ensure the recycled file is 4K
if [[ $tvSeason == *"2160p"* && $vCheck -eq 0 && "$inUse" -eq 0 ]]; then
((rCheck=0))
else
if [[ $debug -ge "1" && $vCheck -eq "0" ]]; then
echo "<font color='orange'><b>The file is not 4K, skipping.</b></font>"
echo ""
fi
((rCheck=1))
fi
#Parse the folder and Extension
tvShowOriginal=$tvFolderRoot$tvShowName/$tvShowSeason/
tvShowExtension=$(echo "$tvSeason" | cut -d'.' -f5)
#Check to see if the file matches a show
if [ -d "${tvShowOriginal}" ]; then
cd "${tvShowOriginal}" || exit
if [[ $debug -ge "2" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
echo "<font color='green'><b>Searching in folder: </b></font>""$tvShowOriginal"
fi
#Search the new folder for Low-res shows
if [[ $debug -ge "2" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
echo "<font color='green'><b>Searching for: </b></font>*${tvShowEpisode1}*1080p*"
fi
foundNew=$(find -type f -iname "*${tvShowEpisode1}*1080p*" -print)
if [[ "$foundNew" != "" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
if [ $debug -ge "1" ]; then
echo "<font color='green'><b>Found matching Low-res Show: </b></font>""$foundNew"
fi
fi
if [[ $debug -ge "1" && "$foundNew" = "" && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
echo "<font color='orange'><b>No matching Low-res file found, skipping.</b></font>"
echo ""
fi
#Parse name only if 1080p filename found
if [[ "$foundNew" == *"1080p"* && $rCheck -eq "0" && $vCheck -eq "0" ]]; then
parseNameNew0=$(echo "$foundNew" | cut -d'[' -f1)
parseNameNew0=$(echo "$parseNameNew0" | cut -d'/' -f2)
if [ $debug -ge "2" ]; then
echo "<font color='red'><b>Name Comparison:</b></font> |" "$parseNameOld0" "|" "$parseNameNew0" "|"
fi
if [ "${parseNameOld0}" == "${parseNameNew0}" ]; then
if [ $debug -ge "2" ] && [ $inUse -eq "0" ]; then
echo "<font color='red'><b>Files Checked: </b></font>"$passNumber
fi
((passNumber++))
if [ $testing -eq "0" ]; then
mv "${tvSeason}" "${tvShowOriginal}"
/usr/local/emhttp/webGui/scripts/notify -e "Radarr Copy" -s "Copy Notifcation" -d "$parseNameNew0 has been copied back." -i "normal"
echo "$currentDate,$currentTime,TV Shows,4K,$tvShowName: $tvShowEpisode1" >> "/mnt/user/Backups/4K_Copier_History.csv"
fi
echo "<font color='blue'><b>""$tvShowName"" ""$tvShowEpisode1"" ""$tvShowExtension"" moved.</b></font>"
echo ""
fi
fi
fi
fi
done
#fi
done
if [[ $passNumber -eq "1" ]]; then
echo "No files to process"
echo ""
fi
#####################################
#
# Remove Empty Directories
#
#####################################
deletedDirCount=0
echo "<hr>"
echo "<font color='blue'><b>Removing empty directories.</b></font>"
echo ""
echo "<hr>"
#Clear empty movie bins
for emptyDir in "$binMovie"*
do
#Check that the scanned item is a directory
if [ -d "${emptyDir}" ]
then
#Check for empty directories only
if [[ -z $(ls -A "$emptyDir") ]]
then
#Log individual deleted files if advanced debugging is enabled.
if [ $debug -ge "2" ]
then
echo "<font color='green'><b>Deleting: </b></font>""$emptyDir"
fi
#Actually do the removing and count how many directories have been deleted.
if [ $testing -eq 0 ]
then
rmdir "${emptyDir}"
((deletedDirCount++))
fi
fi
fi
done
#Clear empty tv show season bins (This has to be done before trying to remove the show folder)
for emptyDir in "$binTV"*
do
for emptySeason in "$binTV"*/*
do
#Check that the scanned item is a directory
if [ -d "${emptySeason}" ]
then
#Check for empty directories only
if [[ -z $(ls -A "$emptySeason") ]]
then
#Log individual deleted files if advanced debugging is enabled.
if [ $debug -ge "2" ]
then
echo "<font color='green'><b>Deleting: </b></font>""$emptySeason"
fi
#Actually do the removing and add to how many directories have been deleted.
if [ $testing -eq 0 ]
then
rmdir "${emptySeason}"
((deletedDirCount++))
fi
fi
fi
done
done
#Clear empty tv show bins (this has to be done after removing the seasons first [above])
for emptyDir in "$binTV"*
do
#Check that the scanned item is a directory
if [ -d "${emptyDir}" ]
then
#Check for empty directories only
if [[ -z $(ls -A "$emptyDir") ]]
then
#Log individual deleted files if advanced debugging is enabled.
if [ $debug -ge "2" ]
then
echo "<font color='green'><b>Deleting: </b></font>""$emptyDir"
fi
#Actually do the removing and add to how many directories have been deleted.
if [ $testing -eq 0 ]
then
rmdir "${emptyDir}"
((deletedDirCount++))
fi
fi
fi
done
#Log the deleted files and print message if testing is enabled instead.
if [ $testing -eq 0 ]
then
echo "<font color='green'><b>Empty directories deleted: </b></font>""$deletedDirCount"
else
echo "<font color='orange'><b>Testing is enabled, so nothing has been deleted.</b></font>"
fi
#Send a notification with total files copied.
((passNumber--))
#Notification where nothing was deleted but files were copied.
if [[ $passNumber -gt 1 && $testing -eq 0 && $deletedDirCount -eq 0 ]]
then
/usr/local/emhttp/webGui/scripts/notify -e "Radarr Copy" -s "Copy Notifcation" -d "$passNumber files have been copied." -i "warning"
fi
#Notification where directories were deleted and files were copied.
if [[ $passNumber -gt 1 && $testing -eq 0 && $deletedDirCount -gt 0 ]]
then
/usr/local/emhttp/webGui/scripts/notify -e "Radarr Copy" -s "Copy Notifcation" -d "$passNumber files have been copied, and $deletedDirCount empty directories have been deleted." -i "warning"
fi