Revert "Replaced rsc with a bash version without remote dependencies."
[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
d67a5cdd
FT
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
25378dd8 29findfile() {
dc508f57 30 if [ -n "$debug" ]; then echo "finding base='$1', ep='$2', qual='$3'" >&2; fi
d67a5cdd 31 local base file tail eq eqt m matches max score
9fc0aa3b 32 matches=()
d67a5cdd
FT
33 base="$(findbase "$1")"
34 for file in "$base"*; do
35 tail="${file#"$base"}"
25378dd8 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
620488ca 53 if [ "$eqt" = "$3" -o "$eqt" = "($3)" -o "${eqt:0:2}" = "- " ]; then
9fc0aa3b 54 matches=("${matches[@]}" "$file")
25378dd8 55 fi
56 fi
57 done
9fc0aa3b 58 if [ ${#matches[@]} -lt 1 ]; then return 1; fi
59 max=
60 for m in "${matches[@]}"; do
61 score="$(scorefile "$m")"
dc508f57 62 if [ -n "$debug" ]; then echo "found \`$m': score $score" >&2; fi
9fc0aa3b 63 if [ -z "$max" ] || [ "$score" -gt "$max" ]; then
64 max="$score"
65 file="$m"
66 fi
67 done
dc508f57 68 if [ -n "$debug" ]; then echo "using \`$file'" >&2; fi
9fc0aa3b 69 echo "$file"
70 return 0
25378dd8 71}
72
251d97ca 73origargs=("$0" "$@")
620488ca 74cmdline=(mpv -fs)
dc508f57 75debug=
091622ed 76log=y
4f06ae97 77unset pretend printfile
40777a19 78
79while [ "${1:0:1}" = - ]; do
80 a="$1"
81 shift
82 case "$a" in
529814da 83 -h)
091622ed 84 echo "usage: planime [-fdhtCL] [-A PLAYER-ARGS... ;] [-s PAR VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
529814da 85 exit 0
86 ;;
dc508f57 87 -d)
88 debug=y
89 ;;
40777a19 90 -t)
620488ca 91 cmdline=("${cmdline[@]}" -ao 'alsa:device=[hw:3,7]')
22365705
FT
92 DISPLAY=:1
93 chwp=y
40777a19 94 ;;
932c90a8 95 -C)
96 pretend=y
97 ;;
091622ed
FT
98 -L)
99 log=
100 ;;
5f7e9143 101 -A)
102 while :; do
f03b6a08
FT
103 if [ $# -lt 1 ]; then
104 echo "planime: unterminated argument list" >&2
105 exit 1
106 fi
5f7e9143 107 a="$1"
108 shift
109 if [ "$a" = \; ]; then
110 break;
111 fi
112 cmdline=("${cmdline[@]}" "$a")
113 done
114 ;;
40777a19 115 -s)
116 savepar="$1"
117 shift
118 saveval="$1"
119 shift
120 ;;
4f06ae97 121 -f)
122 printfile=y
123 ;;
620488ca
FT
124 -v)
125 cmdline=("${cmdline[@]}" -v)
126 ;;
dc508f57 127 --)
128 break
129 ;;
130 *)
131 echo "planime: unknown option \`$a'"
132 exit 1
133 ;;
40777a19 134 esac
135done
136
44423004
FT
137dirbase="$(basename "$(pwd)")"
138base=.
529814da 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
44423004
FT
148 base=
149 ep="$1"
529814da 150 fi
40777a19 151elif [ $# -eq 2 ]; then
44423004
FT
152 if findfile "$dirbase - " "$1" "$2" >/dev/null; then
153 base=""
529814da 154 ep="$1"
155 tqual="$2"
156 else
44423004 157 base="$1"
529814da 158 ep="$2"
159 fi
160else
44423004 161 base="$1"
40777a19 162 ep="$2"
529814da 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
40777a19 172fi
90aff0d0 173if [ -z "$file" ]; then
44423004
FT
174 if [ "$base" = . ]; then
175 if [ -r curser ]; then
176 curser="$(<curser)"
177 else
178 curser=
179 fi
180 else
d67a5cdd 181 curser="$(findbase "$base")"
44423004
FT
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
90aff0d0 189fi
529814da 190
4c7a7e70 191if [ -z "$file" -o ! -r "$file" ]; then
529814da 192 echo "planime: no matching file found" >&2
193 exit 1
194fi
195
4c7a7e70 196case "${file##*.}" in
197 ogm)
c6fdc541 198 aid=1
199 sid=0
4c7a7e70 200 ;;
201 mkv)
c6fdc541 202 alang=jpn
203 slang=eng
4c7a7e70 204 ;;
205esac
40777a19 206
b8882e5f 207ifile=".${file}.info"
40777a19 208
209if [ -n "$savepar" ]; then
210 if [ -r "$ifile" ]; then
b8882e5f 211 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
081b510d 212 mv -f "$ifile.new" "$ifile"
40777a19 213 fi
b8882e5f 214 echo "$savepar $saveval" >>"$ifile"
40777a19 215 exit 0
216fi
217
218unset delay
219
220if [ -r "$ifile" ]; then
b8882e5f 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")
21e62b99 228 elif [ "$par" = volmod ]; then
229 cmdline=("${cmdline[@]}" -af volume="$arg")
c6fdc541 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"
b8882e5f 242 fi
243 done
244 exec 0<&3
245 exec 3<&-
c6fdc541 246fi
247
248if [ -n "$alang" ]; then
249 cmdline=("${cmdline[@]}" -alang "$alang")
250elif [ -n "$aid" ]; then
620488ca 251 cmdline=("${cmdline[@]}" -aid "$((aid + 1))")
c6fdc541 252fi
253if [ -n "$slang" ]; then
254 cmdline=("${cmdline[@]}" -slang "$slang")
255elif [ -n "$sid" ]; then
620488ca 256 cmdline=("${cmdline[@]}" -sid "$((sid + 1))")
40777a19 257fi
258
932c90a8 259if [ -n "$pretend" ]; then
260 echo "${cmdline[@]}" "$file"
4f06ae97 261elif [ -n "$printfile" ]; then
262 echo "$file"
932c90a8 263else
264 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
091622ed
FT
265 if [ "$log" = y ]; then
266 if [ -d $HOME/.anime ]; then
267 echo "$(date) $(basename "$file")" >>$HOME/.anime/plog
268 fi
269 fi
932c90a8 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
251d97ca 301 fi
302fi