I should start by saying I'm a coding noob. The script I'm working on is really pushing my limited skills to the limit. Please forgive the length of the post. I have tried to keep it short but I think the level of detail is necessary.
I also just give a very high level overview of what I'm having problems with.
Basically, I need my script to create hardlinks and name / number them based on several criteria but I'm not sure whether I'm going about things the right way and/or using the right tools.
(Too Many) Details.
I'm writing a bash script that will automate the process of creating and naming hardlinks from torrent files. I'm using a loop to set variables based on strings that are extracted from both the torrent folder path and the name of the individual files within the torrent folder. I then use the variables to name the hardlinks according a pattern (that complies with Plex's naming scheme).
The problem is though, that I need to create hardlinks from multiple folders/torrents. Here's how each torrent (Dir's A, B,and C) numbers their files any how I need to number them ($HardlinkDir).
$DirA $DirB $DirC $HardlinkDir
│ │ │ │
01.*.mp4 │ │ The Series Name - s01e01 - Episode Name.mp4
02.*.mp4 │ │ The Series Name - s01e02 - Episode Name.mp4
03.*.mp4 │ │ The Series Name - s01e03 - Episode Name.mp4
04.*.mp4 │ │ The Series Name - s01e04 - Episode Name.mp4
05.*.mp4 │ │ The Series Name - s01e05 - Episode Name.mp4
06.*.mp4 │ │ The Series Name - s01e06 - Episode Name.mp4
07.*.mp4 │ │ The Series Name - s01e07 - Episode Name.mp4
┌ 01.*.mkv │ The Series Name - s01e08 - Episode Name.mkv
08.foo.mp4 ─────┼ 02.*.mkv │ The Series Name - s01e09 - Episode Name.mkv
└ 03.*.mkv │ The Series Name - s01e10 - Episode Name.mkv
09.*.mp4 │ The Series Name - s01e11 - Episode Name.mp4
┌ 01.*.mkv The Series Name - s01e12 - Episode Name.mkv
10.bar.mp4 ────────────────────┼ 02.*.mkv The Series Name - s01e13 - Episode Name.mkv
└ 03.*.mkv The Series Name - s01e14 - Episode Name.mkv
11.*.mp4 The Series Name - s01e15 - Episode Name.mp4
The names of the hardlinks in $HardlinkDir
can be set almost entirely from variables defined before or during the loop. But as the "e##" numbers deviate from e08 onward, the numbers have to be set according to a handful of rules.
A new version of TorrentA is uploaded every few weeks. With it, the number of files preceding the one that contains the string foo
, can vary. As a result, the number within the filename where the string foo
appears can also vary. As too does the number in filename where the string bar
appears. Sometimes, there's more than one file between the files that contain the strings foo
, and bar
... to make things even more complicated, sometimes the foo
and bar
files don't have a file numbered between them.
The number of files before, between, and after the files with strings foo
, and bar
, can vary, as can the numbers in the filenames. But the strings foo
. and bar
are always present.
Torrents / Dirs A and B always have three files, and they're always numbered and naming the exact same thing from week to week.
One last thing to make things that little bit more complicated... TorrentA (DirA) is periodically updated, so version 1 might only have the first four files. Then TorrentB might be uploaded before the new version of TorrentA is released. So the script might try creating hardlinks for DirB based on information derived from filenames in DirA that don't yet exist (it'd be a rare scenario though).
So as far as I can figure, what I need to script to do (roughly in order) is-
- set the name of the file containing the string
foo
as a variable...$foo
. - set the name of the file containing the string
bar
as a variable...$bar
if either the foo
or bar
files don't exist, the variable would not be set.
- When the loop begins, the script needs to check if the name of the file being processed ($filename) matches either
$foo
or$bar
- if it does match, the script skips the file altogether.
- if it doesn't match
$foo
or$bar
, the script extracts the two digit number usingawk
and/orsed
.
That should work for files 01 to 07. 08 would get skipped, then 09 would have to have other arguments.
- if the number in the
$filename
equals the number in$foo
+1, but$filename
doesn't match$bar
then add +2. In other words, if the number in$filename
is 09, add 2 to make it 11.
In order to account for the possibility of multiple files being between the foo
and bar
files, I'm guessing the number set during step 6 needs to iterate as the loop runs, and that the script also needs to check if the number in the filename is higher than that in $bar
. For example....
- if the number in the
$filename
equals the number in$foo
+1, but$filename
doesn't match$bar
or have a number higher that$bar
then add +3. So a theoretical file10.*.mkv
would becomeThe Series Name - s01e11 - Episode Name.mkv
. - if the
$filename
matches$bar
, skip it. - (I'm a little shaky on this one) if the
$filename
(in DirA remember) has a number greater that$bar
, then... I'm not sure what. The number used when naming the hardlink needs to be higher than the numbers that were previously set. Perhaps I could use some kind of counter or output the numbers already in use to a tmp file?
[note] As for the files in DirB
and DirC
, their numbers would be set in a similar fashion but using the strings within the names of the DirA
and DirB
files (if $filename
== 01.*.mkv
; then etc. etc.). I could also implement checks based on files that already exist in $hardlinkDir
.
Questions.
Q1. Is my basic workflow of how the script could work roughly correct?
Q2. Am I overcomplicating things?
Q3. In regards to step 9., would the possibility of using a counter for setting file numbers after $bar
be worth exploring?... I just can't think of how to derive the numbers when the number of files after $foo
, and between $bar
, and then after $bar
might vary... or not exist at all.