planime: Added base aliases.
[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
53 if [ "$eqt" = "$3" -o "$eqt" = "($3)" ]; 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" "$@")
1c19e28f 74cmdline=(mplayer -fs -ass)
dc508f57 75debug=
4f06ae97 76unset pretend printfile
40777a19 77
78while [ "${1:0:1}" = - ]; do
79 a="$1"
80 shift
81 case "$a" in
529814da 82 -h)
dc508f57 83 echo "usage: planime [-fdhtC] [-A PLAYER-ARGS... ;] [-s PAR VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
529814da 84 exit 0
85 ;;
dc508f57 86 -d)
87 debug=y
88 ;;
40777a19 89 -t)
90 cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
91 ;;
932c90a8 92 -C)
93 pretend=y
94 ;;
5f7e9143 95 -A)
96 while :; do
97 a="$1"
98 shift
99 if [ "$a" = \; ]; then
100 break;
101 fi
102 cmdline=("${cmdline[@]}" "$a")
103 done
104 ;;
40777a19 105 -s)
106 savepar="$1"
107 shift
108 saveval="$1"
109 shift
110 ;;
4f06ae97 111 -f)
112 printfile=y
113 ;;
dc508f57 114 --)
115 break
116 ;;
117 *)
118 echo "planime: unknown option \`$a'"
119 exit 1
120 ;;
40777a19 121 esac
122done
123
44423004
FT
124dirbase="$(basename "$(pwd)")"
125base=.
529814da 126nextep=n
127file=
128tqual=
129if [ $# -eq 0 ]; then
130 nextep=y
131elif [ $# -eq 1 ]; then
132 if [ -r "$1" ]; then
133 file="$1"
134 else
44423004
FT
135 base=
136 ep="$1"
529814da 137 fi
40777a19 138elif [ $# -eq 2 ]; then
44423004
FT
139 if findfile "$dirbase - " "$1" "$2" >/dev/null; then
140 base=""
529814da 141 ep="$1"
142 tqual="$2"
143 else
44423004 144 base="$1"
529814da 145 ep="$2"
146 fi
147else
44423004 148 base="$1"
40777a19 149 ep="$2"
529814da 150 tqual="$3"
151fi
152if [ "$ep" = . ]; then nextep=y; fi
153if [ "$nextep" = y -a -r nextep ]; then
154 ep="$(<nextep)"
155 if ! isnum "$ep"; then
156 echo "planime: nextep is non-numeric" >&2
157 exit 1
158 fi
40777a19 159fi
90aff0d0 160if [ -z "$file" ]; then
44423004
FT
161 if [ "$base" = . ]; then
162 if [ -r curser ]; then
163 curser="$(<curser)"
164 else
165 curser=
166 fi
167 else
d67a5cdd 168 curser="$(findbase "$base")"
44423004
FT
169 fi
170 if [ -n "$curser" ]; then
171 file="$(findfile "$dirbase $curser - " "$ep" "$tqual")" || \
172 file="$(findfile "$curser - " "$ep" "$tqual")"
173 else
174 file="$(findfile "$dirbase - " "$ep" "$tqual")"
175 fi
90aff0d0 176fi
529814da 177
4c7a7e70 178if [ -z "$file" -o ! -r "$file" ]; then
529814da 179 echo "planime: no matching file found" >&2
180 exit 1
181fi
182
4c7a7e70 183case "${file##*.}" in
184 ogm)
c6fdc541 185 aid=1
186 sid=0
4c7a7e70 187 ;;
188 mkv)
c6fdc541 189 alang=jpn
190 slang=eng
4c7a7e70 191 ;;
192esac
40777a19 193
b8882e5f 194ifile=".${file}.info"
40777a19 195
196if [ -n "$savepar" ]; then
197 if [ -r "$ifile" ]; then
b8882e5f 198 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
081b510d 199 mv -f "$ifile.new" "$ifile"
40777a19 200 fi
b8882e5f 201 echo "$savepar $saveval" >>"$ifile"
40777a19 202 exit 0
203fi
204
205unset delay
206
207if [ -r "$ifile" ]; then
b8882e5f 208 exec 3<&0
209 exec 0<"$ifile"
210 while read par arg; do
211 if [ "$par" = delay ]; then
212 cmdline=("${cmdline[@]}" -delay "$arg")
213 elif [ "$par" = aspect ]; then
214 cmdline=("${cmdline[@]}" -aspect "$arg")
21e62b99 215 elif [ "$par" = volmod ]; then
216 cmdline=("${cmdline[@]}" -af volume="$arg")
c6fdc541 217 elif [ "$par" = alang ]; then
218 unset alang aid
219 alang="$arg"
220 elif [ "$par" = aid ]; then
221 unset alang aid
222 aid="$arg"
223 elif [ "$par" = slang ]; then
224 unset slang sid
225 slang="$arg"
226 elif [ "$par" = sid ]; then
227 unset slang sid
228 sid="$arg"
b8882e5f 229 fi
230 done
231 exec 0<&3
232 exec 3<&-
c6fdc541 233fi
234
235if [ -n "$alang" ]; then
236 cmdline=("${cmdline[@]}" -alang "$alang")
237elif [ -n "$aid" ]; then
238 cmdline=("${cmdline[@]}" -aid "$aid")
239fi
240if [ -n "$slang" ]; then
241 cmdline=("${cmdline[@]}" -slang "$slang")
242elif [ -n "$sid" ]; then
243 cmdline=("${cmdline[@]}" -sid "$sid")
40777a19 244fi
245
21e62b99 246if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi
40777a19 247
932c90a8 248if [ -n "$pretend" ]; then
249 echo "${cmdline[@]}" "$file"
4f06ae97 250elif [ -n "$printfile" ]; then
251 echo "$file"
932c90a8 252else
253 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
254 "${cmdline[@]}" "$file"
255
256 if [ "$nextep" = y ]; then
257 echo "0. Save and continue (or Space)"
258 echo "1. Continue without saving"
259 echo "2. Save and exit (or Enter)"
260 echo "3. Exit without saving (or any key)"
261 IFS= read -sn1 c
262 save=n
263 cont=n
264 case "$c" in
265 0|" ")
266 save=y
267 cont=y
268 ;;
269 1)
270 cont=y
271 ;;
272 2|"")
273 save=y
274 ;;
275 esac
276 if [ "$save" = y ]; then
277 let ep++
278 echo "$ep" >nextep
279 fi
280 if [ "$cont" = y ]; then
281 exec "${origargs[@]}"
282 else
283 echo "nextep is $ep"
284 fi
251d97ca 285 fi
286fi