Prioritize correctly between different matching files.
[utils.git] / planime
... / ...
CommitLineData
1#!/bin/bash
2
3isnum() {
4 grep -xq '[0-9]\+' <<<"$1"
5}
6
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
17findfile() {
18 local file tail eq eqt m matches max score
19 matches=()
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
40 matches=("${matches[@]}" "$file")
41 fi
42 fi
43 done
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
55}
56
57origargs=("$0" "$@")
58cmdline=(mplayer -fs -ass)
59unset pretend
60
61while [ "${1:0:1}" = - ]; do
62 a="$1"
63 shift
64 case "$a" in
65 -h)
66 echo "usage: planime [-htC] [-s PAR VAL] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
67 exit 0
68 ;;
69 -t)
70 cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
71 ;;
72 -C)
73 pretend=y
74 ;;
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 ;;
85 -s)
86 savepar="$1"
87 shift
88 saveval="$1"
89 shift
90 ;;
91 esac
92done
93
94base="$(basename "$(pwd)")"
95nextep=n
96file=
97tqual=
98if [ $# -eq 0 ]; then
99 nextep=y
100 if [ -r curser ]; then
101 base="$base $(<curser)"
102 fi
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
114elif [ $# -eq 2 ]; then
115 if findfile "$base - " "$1" "$2" >/dev/null; then
116 ep="$1"
117 tqual="$2"
118 else
119 if [ "$1" = . -a -r curser ]; then
120 base="$base $(<curser)"
121 else
122 base="$base $1"
123 fi
124 ep="$2"
125 fi
126else
127 if [ "$1" = . -a -r curser ]; then
128 base="$base $(<curser)"
129 else
130 base="$base $1"
131 fi
132 ep="$2"
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
142fi
143if [ -z "$file" ]; then
144 file="$(findfile "$base - " "$ep" "$tqual")"
145fi
146
147if [ -z "$file" -o ! -r "$file" ]; then
148 echo "planime: no matching file found" >&2
149 exit 1
150fi
151
152case "${file##*.}" in
153 ogm)
154 aid=1
155 sid=0
156 ;;
157 mkv)
158 alang=jpn
159 slang=eng
160 ;;
161esac
162
163ifile=".${file}.info"
164
165if [ -n "$savepar" ]; then
166 if [ -r "$ifile" ]; then
167 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
168 mv -f "$ifile.new" "$ifile"
169 fi
170 echo "$savepar $saveval" >>"$ifile"
171 exit 0
172fi
173
174unset delay
175
176if [ -r "$ifile" ]; then
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")
184 elif [ "$par" = volmod ]; then
185 cmdline=("${cmdline[@]}" -af volume="$arg")
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"
198 fi
199 done
200 exec 0<&3
201 exec 3<&-
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")
213fi
214
215if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi
216
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
252 fi
253fi