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