Checked in anndl.
[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
17findbase() {
18 if [ -r aliases ]; then
19 while read alias rest; do
20 if [ "$alias" = "$1" ]; then
21 echo "$rest"
22 return
23 fi
24 done <aliases
25 fi
26 echo "$1"
27}
28
29findfile() {
30 if [ -n "$debug" ]; then echo "finding base='$1', ep='$2', qual='$3'" >&2; fi
31 local base file tail eq eqt m matches max score
32 matches=()
33 base="$(findbase "$1")"
34 for file in "$base"*; do
35 tail="${file#"$base"}"
36 eq="${tail%.*}"
37 m=n
38 if [ "${eq%% *}" -eq "$2" ] 2>/dev/null; then
39 if [[ "$eq" == *\ * ]]; then
40 eqt="${eq#* }"
41 else
42 eqt=
43 fi
44 m=y
45 elif [ "${eq:0:${#2}}" = "$2" ]; then
46 eqt="${eq:${#2}}"
47 if [ "${eqt:0:1}" = " " -o -z "$eqt" ]; then
48 eqt="${eqt# }"
49 m=y
50 fi
51 fi
52 if [ "$m" = y ]; then
53 if [ "$eqt" = "$3" -o "$eqt" = "($3)" -o "${eqt:0:2}" = "- " ]; then
54 matches=("${matches[@]}" "$file")
55 fi
56 fi
57 done
58 if [ ${#matches[@]} -lt 1 ]; then return 1; fi
59 max=
60 for m in "${matches[@]}"; do
61 score="$(scorefile "$m")"
62 if [ -n "$debug" ]; then echo "found \`$m': score $score" >&2; fi
63 if [ -z "$max" ] || [ "$score" -gt "$max" ]; then
64 max="$score"
65 file="$m"
66 fi
67 done
68 if [ -n "$debug" ]; then echo "using \`$file'" >&2; fi
69 echo "$file"
70 return 0
71}
72
73origargs=("$0" "$@")
74cmdline=(mpv -fs)
75debug=
76log=y
77unset pretend printfile
78
79while [ "${1:0:1}" = - ]; do
80 a="$1"
81 shift
82 case "$a" in
83 -h)
84 echo "usage: planime [-fdhtCL] [-A PLAYER-ARGS... ;] [-s PAR VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
85 exit 0
86 ;;
87 -d)
88 debug=y
89 ;;
90 -t)
91 cmdline=("${cmdline[@]}" -audio-device 'alsa/hdmi:CARD=PCH,DEV=0')
92 DISPLAY=:1
93 chwp=y
94 ;;
95 -C)
96 pretend=y
97 ;;
98 -L)
99 log=
100 ;;
101 -A)
102 while :; do
103 if [ $# -lt 1 ]; then
104 echo "planime: unterminated argument list" >&2
105 exit 1
106 fi
107 a="$1"
108 shift
109 if [ "$a" = \; ]; then
110 break;
111 fi
112 cmdline=("${cmdline[@]}" "$a")
113 done
114 ;;
115 -s)
116 savepar="$1"
117 shift
118 saveval="$1"
119 shift
120 ;;
121 -f)
122 printfile=y
123 ;;
124 -v)
125 cmdline=("${cmdline[@]}" -v)
126 ;;
127 --)
128 break
129 ;;
130 *)
131 echo "planime: unknown option \`$a'"
132 exit 1
133 ;;
134 esac
135done
136
137dirbase="$(basename "$(pwd)")"
138base=.
139nextep=n
140file=
141tqual=
142if [ $# -eq 0 ]; then
143 nextep=y
144elif [ $# -eq 1 ]; then
145 if [ -r "$1" ]; then
146 file="$1"
147 else
148 base=
149 ep="$1"
150 fi
151elif [ $# -eq 2 ]; then
152 if findfile "$dirbase - " "$1" "$2" >/dev/null; then
153 base=""
154 ep="$1"
155 tqual="$2"
156 else
157 base="$1"
158 ep="$2"
159 fi
160else
161 base="$1"
162 ep="$2"
163 tqual="$3"
164fi
165if [ "$ep" = . ]; then nextep=y; fi
166if [ "$nextep" = y -a -r nextep ]; then
167 ep="$(<nextep)"
168 if ! isnum "$ep"; then
169 echo "planime: nextep is non-numeric" >&2
170 exit 1
171 fi
172fi
173if [ -z "$file" ]; then
174 if [ "$base" = . ]; then
175 if [ -r curser ]; then
176 curser="$(<curser)"
177 else
178 curser=
179 fi
180 else
181 curser="$(findbase "$base")"
182 fi
183 if [ -n "$curser" ]; then
184 file="$(findfile "$dirbase $curser - " "$ep" "$tqual")" || \
185 file="$(findfile "$curser - " "$ep" "$tqual")"
186 else
187 file="$(findfile "$dirbase - " "$ep" "$tqual")"
188 fi
189fi
190
191if [ -z "$file" -o ! -r "$file" ]; then
192 echo "planime: no matching file found" >&2
193 exit 1
194fi
195
196case "${file##*.}" in
197 ogm)
198 aid=1
199 sid=0
200 ;;
201 mkv)
202 alang=jpn
203 slang=eng
204 ;;
205esac
206
207ifile=".${file}.info"
208
209if [ -n "$savepar" ]; then
210 if [ -r "$ifile" ]; then
211 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
212 mv -f "$ifile.new" "$ifile"
213 fi
214 echo "$savepar $saveval" >>"$ifile"
215 exit 0
216fi
217
218unset delay
219
220if [ -r "$ifile" ]; then
221 exec 3<&0
222 exec 0<"$ifile"
223 while read par arg; do
224 if [ "$par" = delay ]; then
225 cmdline=("${cmdline[@]}" -delay "$arg")
226 elif [ "$par" = aspect ]; then
227 cmdline=("${cmdline[@]}" -aspect "$arg")
228 elif [ "$par" = volmod ]; then
229 cmdline=("${cmdline[@]}" -af volume="$arg")
230 elif [ "$par" = alang ]; then
231 unset alang aid
232 alang="$arg"
233 elif [ "$par" = aid ]; then
234 unset alang aid
235 aid="$arg"
236 elif [ "$par" = slang ]; then
237 unset slang sid
238 slang="$arg"
239 elif [ "$par" = sid ]; then
240 unset slang sid
241 sid="$arg"
242 fi
243 done
244 exec 0<&3
245 exec 3<&-
246fi
247
248if [ -n "$alang" ]; then
249 cmdline=("${cmdline[@]}" -alang "$alang")
250elif [ -n "$aid" ]; then
251 cmdline=("${cmdline[@]}" -aid "$((aid + 1))")
252fi
253if [ -n "$slang" ]; then
254 cmdline=("${cmdline[@]}" -slang "$slang")
255elif [ -n "$sid" ]; then
256 cmdline=("${cmdline[@]}" -sid "$((sid + 1))")
257fi
258
259if [ -n "$pretend" ]; then
260 echo "${cmdline[@]}" "$file"
261elif [ -n "$printfile" ]; then
262 echo "$file"
263else
264 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
265 if [ "$log" = y ]; then
266 if [ -d $HOME/.anime ]; then
267 echo "$(date) $(basename "$file")" >>$HOME/.anime/plog
268 fi
269 fi
270 "${cmdline[@]}" "$file"
271
272 if [ "$nextep" = y ]; then
273 echo "0. Save and continue (or Space)"
274 echo "1. Continue without saving"
275 echo "2. Save and exit (or Enter)"
276 echo "3. Exit without saving (or any key)"
277 IFS= read -sn1 c
278 save=n
279 cont=n
280 case "$c" in
281 0|" ")
282 save=y
283 cont=y
284 ;;
285 1)
286 cont=y
287 ;;
288 2|"")
289 save=y
290 ;;
291 esac
292 if [ "$save" = y ]; then
293 let ep++
294 echo "$ep" >nextep
295 fi
296 if [ "$cont" = y ]; then
297 exec "${origargs[@]}"
298 else
299 echo "nextep is $ep"
300 fi
301 fi
302fi