-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess-mkv
executable file
·59 lines (49 loc) · 1.75 KB
/
process-mkv
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
#!/usr/bin/env bash
# $@: mkv files.
#
# Performs several actions in sequence:
# 1) Looks for a file that doesn't end with .mkv in $1's directory
# 1.1) If a non-mkv file is found, renames $1 to that file with .mkv added
# 1.2) Remove the non-mkv file
# 2) Calls update-permissions
# 3) Calls tag-mkv
for file in "$@"; do
dir=$(dirname "$file")
file_name=$(basename "$file")
ext=${file##*.}
next_episode=$(find "$dir" -maxdepth 1 -type f -name 'E[0-9][0-9]*' -not -name "*.${ext}" | sort | head -1)
next_episode_name=$(basename "$next_episode")
if [[ -n $next_episode_name ]]; then
new_file="${next_episode}.${ext}"
new_file_name="${next_episode_name}.${ext}"
echo "Renaming $file_name to $new_file_name"
if [[ "$file_name" =~ [eE]0+?([0-9]+) ]]; then
file_episode=${BASH_REMATCH[1]}
else
file_episode=-1
fi
if [[ "$new_file_name" =~ [eE]0+?([0-9]+) ]]; then
next_file_episode=${BASH_REMATCH[1]}
else
next_file_episode=-2
fi
if [[ "$file_episode" != "$next_file_episode" ]]; then
echo "$file_name appears to be episode ${file_episode}, but $next_file_episode appears to be episode ${next_file_episode}."
if ! user_permission "Would you like to continue anyway?"; then
exit 1
fi
fi
mv -i "$file" "$new_file"
if [[ $? != 0 ]]; then
exit 1
fi
file=$new_file
file_name=$new_file_name
echo "Removing placeholder for $file_name"
rm "$next_episode"
fi
echo "Updating permissions of $file_name"
update-permissions "$file"
echo "Tagging $file_name"
tag-mkv "$file"
done