Prioritize correctly between different matching files.
[utils.git] / planime
CommitLineData
94bf9c94 1#!/bin/bash
40777a19 2
25378dd8 3isnum() {
4 grep -xq '[0-9]\+' <<<"$1"
5}
6
9fc0aa3b 7scorefile() {
8 if [ "${1##*.}" = mkv ]; then
9 echo 10
10 elif [ "${1##*.}" = ogm ]; then
11 echo 5
12 else
13 echo 0
14 fi
15}
16
25378dd8 17findfile() {
9fc0aa3b 18 local file tail eq eqt m matches max score
19 matches=()
25378dd8 20 for file in "$1"*; do
21 tail="${file#$1}"
22 eq="${tail%.*}"
23 m=n
24 if [ "${eq%% *}" -eq "$2" ] 2>/dev/null; then
25 if [[ "$eq" == *\ * ]]; then
26 eqt="${eq#* }"
27 else
28 eqt=
29 fi
30 m=y
31 elif [ "${eq:0:${#2}}" = "$2" ]; then
32 eqt="${eq:${#2}}"
33 if [ "${eqt:0:1}" = " " -o -z "$eqt" ]; then
34 eqt="${eqt# }"
35 m=y
36 fi
37 fi
38 if [ "$m" = y ]; then
39 if [ "$eqt" = "$3" -o "$eqt" = "($3)" ]; then
9fc0aa3b 40 matches=("${matches[@]}" "$file")
25378dd8 41 fi
42 fi
43 done
9fc0aa3b 44 if [ ${#matches[@]} -lt 1 ]; then return 1; fi
45 max=
46 for m in "${matches[@]}"; do
47 score="$(scorefile "$m")"
48 if [ -z "$max" ] || [ "$score" -gt "$max" ]; then
49 max="$score"
50 file="$m"
51 fi
52 done
53 echo "$file"
54 return 0
25378dd8 55}
56
251d97ca 57origargs=("$0" "$@")
1c19e28f 58cmdline=(mplayer -fs -ass)
932c90a8 59unset pretend
40777a19 60
61while [ "${1:0:1}" = - ]; do
62 a="$1"
63 shift
64 case "$a" in
529814da 65 -h)
932c90a8 66 echo "usage: planime [-htC] [-s PAR VAL] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
529814da 67 exit 0
68 ;;
40777a19 69 -t)
70 cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
71 ;;
932c90a8 72 -C)
73 pretend=y
74 ;;
5f7e9143 75 -A)
76 while :; do
77 a="$1"
78 shift
79 if [ "$a" = \; ]; then
80 break;
81 fi
82 cmdline=("${cmdline[@]}" "$a")
83 done
84 ;;
40777a19 85 -s)
86 savepar="$1"
87 shift
88 saveval="$1"
89 shift
90 ;;
91 esac
92done
93
529814da 94base="$(basename "$(pwd)")"
95nextep=n
96file=
97tqual=
98if [ $# -eq 0 ]; then
99 nextep=y
e299213e 100 if [ -r curser ]; then
101 base="$base $(<curser)"
102 fi
529814da 103elif [ $# -eq 1 ]; then
104 if [ -r "$1" ]; then
105 file="$1"
106 else
107 if findfile "$base - " "$1" >/dev/null; then
108 ep="$1"
109 else
110 nextep=y
111 base="$base $1"
112 fi
113 fi
40777a19 114elif [ $# -eq 2 ]; then
529814da 115 if findfile "$base - " "$1" "$2" >/dev/null; then
116 ep="$1"
117 tqual="$2"
118 else
e299213e 119 if [ "$1" = . -a -r curser ]; then
120 base="$base $(<curser)"
121 else
122 base="$base $1"
123 fi
529814da 124 ep="$2"
125 fi
126else
e299213e 127 if [ "$1" = . -a -r curser ]; then
128 base="$base $(<curser)"
129 else
130 base="$base $1"
131 fi
40777a19 132 ep="$2"
529814da 133 tqual="$3"
134fi
135if [ "$ep" = . ]; then nextep=y; fi
136if [ "$nextep" = y -a -r nextep ]; then
137 ep="$(<nextep)"
138 if ! isnum "$ep"; then
139 echo "planime: nextep is non-numeric" >&2
140 exit 1
141 fi
40777a19 142fi
90aff0d0 143if [ -z "$file" ]; then
b8882e5f 144 file="$(findfile "$base - " "$ep" "$tqual")"
90aff0d0 145fi
529814da 146
4c7a7e70 147if [ -z "$file" -o ! -r "$file" ]; then
529814da 148 echo "planime: no matching file found" >&2
149 exit 1
150fi
151
4c7a7e70 152case "${file##*.}" in
153 ogm)
c6fdc541 154 aid=1
155 sid=0
4c7a7e70 156 ;;
157 mkv)
c6fdc541 158 alang=jpn
159 slang=eng
4c7a7e70 160 ;;
161esac
40777a19 162
b8882e5f 163ifile=".${file}.info"
40777a19 164
165if [ -n "$savepar" ]; then
166 if [ -r "$ifile" ]; then
b8882e5f 167 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
081b510d 168 mv -f "$ifile.new" "$ifile"
40777a19 169 fi
b8882e5f 170 echo "$savepar $saveval" >>"$ifile"
40777a19 171 exit 0
172fi
173
174unset delay
175
176if [ -r "$ifile" ]; then
b8882e5f 177 exec 3<&0
178 exec 0<"$ifile"
179 while read par arg; do
180 if [ "$par" = delay ]; then
181 cmdline=("${cmdline[@]}" -delay "$arg")
182 elif [ "$par" = aspect ]; then
183 cmdline=("${cmdline[@]}" -aspect "$arg")
21e62b99 184 elif [ "$par" = volmod ]; then
185 cmdline=("${cmdline[@]}" -af volume="$arg")
c6fdc541 186 elif [ "$par" = alang ]; then
187 unset alang aid
188 alang="$arg"
189 elif [ "$par" = aid ]; then
190 unset alang aid
191 aid="$arg"
192 elif [ "$par" = slang ]; then
193 unset slang sid
194 slang="$arg"
195 elif [ "$par" = sid ]; then
196 unset slang sid
197 sid="$arg"
b8882e5f 198 fi
199 done
200 exec 0<&3
201 exec 3<&-
c6fdc541 202fi
203
204if [ -n "$alang" ]; then
205 cmdline=("${cmdline[@]}" -alang "$alang")
206elif [ -n "$aid" ]; then
207 cmdline=("${cmdline[@]}" -aid "$aid")
208fi
209if [ -n "$slang" ]; then
210 cmdline=("${cmdline[@]}" -slang "$slang")
211elif [ -n "$sid" ]; then
212 cmdline=("${cmdline[@]}" -sid "$sid")
40777a19 213fi
214
21e62b99 215if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi
40777a19 216
932c90a8 217if [ -n "$pretend" ]; then
218 echo "${cmdline[@]}" "$file"
219else
220 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
221 "${cmdline[@]}" "$file"
222
223 if [ "$nextep" = y ]; then
224 echo "0. Save and continue (or Space)"
225 echo "1. Continue without saving"
226 echo "2. Save and exit (or Enter)"
227 echo "3. Exit without saving (or any key)"
228 IFS= read -sn1 c
229 save=n
230 cont=n
231 case "$c" in
232 0|" ")
233 save=y
234 cont=y
235 ;;
236 1)
237 cont=y
238 ;;
239 2|"")
240 save=y
241 ;;
242 esac
243 if [ "$save" = y ]; then
244 let ep++
245 echo "$ep" >nextep
246 fi
247 if [ "$cont" = y ]; then
248 exec "${origargs[@]}"
249 else
250 echo "nextep is $ep"
251 fi
251d97ca 252 fi
253fi