Exit properly on errors in icom.
[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
111base="$(basename "$(pwd)")"
112nextep=n
113file=
114tqual=
115if [ $# -eq 0 ]; then
116 nextep=y
117 if [ -r curser ]; then
118 base="$base $(<curser)"
119 fi
120elif [ $# -eq 1 ]; then
121 if [ -r "$1" ]; then
122 file="$1"
123 else
124 if findfile "$base - " "$1" >/dev/null; then
125 ep="$1"
126 else
127 nextep=y
128 base="$base $1"
129 fi
130 fi
131elif [ $# -eq 2 ]; then
132 if findfile "$base - " "$1" "$2" >/dev/null; then
133 ep="$1"
134 tqual="$2"
135 else
136 if [ "$1" = . -a -r curser ]; then
137 base="$base $(<curser)"
138 else
139 base="$base $1"
140 fi
141 ep="$2"
142 fi
143else
144 if [ "$1" = . -a -r curser ]; then
145 base="$base $(<curser)"
146 else
147 base="$base $1"
148 fi
149 ep="$2"
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
159fi
160if [ -z "$file" ]; then
161 file="$(findfile "$base - " "$ep" "$tqual")"
162fi
163
164if [ -z "$file" -o ! -r "$file" ]; then
165 echo "planime: no matching file found" >&2
166 exit 1
167fi
168
169case "${file##*.}" in
170 ogm)
171 aid=1
172 sid=0
173 ;;
174 mkv)
175 alang=jpn
176 slang=eng
177 ;;
178esac
179
180ifile=".${file}.info"
181
182if [ -n "$savepar" ]; then
183 if [ -r "$ifile" ]; then
184 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
185 mv -f "$ifile.new" "$ifile"
186 fi
187 echo "$savepar $saveval" >>"$ifile"
188 exit 0
189fi
190
191unset delay
192
193if [ -r "$ifile" ]; then
194 exec 3<&0
195 exec 0<"$ifile"
196 while read par arg; do
197 if [ "$par" = delay ]; then
198 cmdline=("${cmdline[@]}" -delay "$arg")
199 elif [ "$par" = aspect ]; then
200 cmdline=("${cmdline[@]}" -aspect "$arg")
201 elif [ "$par" = volmod ]; then
202 cmdline=("${cmdline[@]}" -af volume="$arg")
203 elif [ "$par" = alang ]; then
204 unset alang aid
205 alang="$arg"
206 elif [ "$par" = aid ]; then
207 unset alang aid
208 aid="$arg"
209 elif [ "$par" = slang ]; then
210 unset slang sid
211 slang="$arg"
212 elif [ "$par" = sid ]; then
213 unset slang sid
214 sid="$arg"
215 fi
216 done
217 exec 0<&3
218 exec 3<&-
219fi
220
221if [ -n "$alang" ]; then
222 cmdline=("${cmdline[@]}" -alang "$alang")
223elif [ -n "$aid" ]; then
224 cmdline=("${cmdline[@]}" -aid "$aid")
225fi
226if [ -n "$slang" ]; then
227 cmdline=("${cmdline[@]}" -slang "$slang")
228elif [ -n "$sid" ]; then
229 cmdline=("${cmdline[@]}" -sid "$sid")
230fi
231
232if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi
233
234if [ -n "$pretend" ]; then
235 echo "${cmdline[@]}" "$file"
236elif [ -n "$printfile" ]; then
237 echo "$file"
238else
239 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
240 "${cmdline[@]}" "$file"
241
242 if [ "$nextep" = y ]; then
243 echo "0. Save and continue (or Space)"
244 echo "1. Continue without saving"
245 echo "2. Save and exit (or Enter)"
246 echo "3. Exit without saving (or any key)"
247 IFS= read -sn1 c
248 save=n
249 cont=n
250 case "$c" in
251 0|" ")
252 save=y
253 cont=y
254 ;;
255 1)
256 cont=y
257 ;;
258 2|"")
259 save=y
260 ;;
261 esac
262 if [ "$save" = y ]; then
263 let ep++
264 echo "$ep" >nextep
265 fi
266 if [ "$cont" = y ]; then
267 exec "${origargs[@]}"
268 else
269 echo "nextep is $ep"
270 fi
271 fi
272fi