-
Notifications
You must be signed in to change notification settings - Fork 0
/
rnsubs
executable file
·101 lines (95 loc) · 1.93 KB
/
rnsubs
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
#!/bin/bash
labels=(SDH FOREIGN)
linkCommand=mv
shouldRename=true
if [ "x${1}" = "x-l" ]; then
linkCommand=ln
shift
fi
if [ "x${1}" = "x-c" ]; then
linkCommand=cp
shift
fi
if [ "x${1}" = "x-r" ]; then
shouldRename=false
echo not renaming
shift
fi
if [ ! -d [Ss][Uu][Bb][Ss] ]; then
cat <<-EOF
# Usage: rnsubs [-l | -c | -r]
#
# -l -- use "ln" instead of mv
# -c -- use "cp" instead of mv
# -r -- don't try to rename subtitle file, just move it
#
# rnsubs: no such directory: 'Subs'
#
# rnsubs is means for a directory structure like:
# - file1
# - file2
#
# - Subs/file1/2_English.srt
# - Subs/file1/3_English.srt
#
# - Subs/file2/2_English.srt
# - Subs/file2/3_English.srt
# - Subs/file2/4_English.srt
# - Subs/file2/5_English.srt
#
# When run in the main directory, it will move the *_English.srt files under Subs
# to the top level like so:
#
# - file1.srt
# - file1.SDH.srt
#
# - file2.srt
# - file2.SDH.srt
# - file2.FOREIGN.srt
# - file2.4.srt
#
EOF
else
parent="$PWD"
cd [Ss][Uu][Bb][Ss]
for dir in *; do
[ -d "$dir" ] || continue
cd "$dir"
index=1
all=(*)
if [ ${#all[@]} -gt 2 ]; then
all=(*[Ee][Nn][Gg]*)
fi
for srt in "${all[@]}"; do
if [ $index = 1 ]; then
if $shouldRename; then
"${linkCommand}" "$srt" "$parent/${dir}.srt"
else
echo not renaming
"${linkCommand}" "$srt" "$parent"
fi
else
labelIndex=$((index - 2))
label="${labels[$labelIndex]}"
if [ "$label" = "" ]; then
if $shouldRename; then
"${linkCommand}" "$srt" "$parent/${dir}.${index}.srt"
else
echo not renaming
"${linkCommand}" "$srt" "$parent"
fi
else
if $shouldRename; then
"${linkCommand}" "$srt" "$parent/${dir}.${label}.srt"
else
echo not renaming
"${linkCommand}" "$srt" "$parent"
fi
fi
fi
((index++))
done
cd ..
done
cd ..
fi