Added debug mode and option termination.
[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
25378dd8 17findfile() {
dc508f57 18 if [ -n "$debug" ]; then echo "finding base='$1', ep='$2', qual='$3'" >&2; fi
9fc0aa3b 19 local file tail eq eqt m matches max score
20 matches=()
25378dd8 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
9fc0aa3b 41 matches=("${matches[@]}" "$file")
25378dd8 42 fi
43 fi
44 done
9fc0aa3b 45 if [ ${#matches[@]} -lt 1 ]; then return 1; fi
46 max=
47 for m in "${matches[@]}"; do
48 score="$(scorefile "$m")"
dc508f57 49 if [ -n "$debug" ]; then echo "found \`$m': score $score" >&2; fi
9fc0aa3b 50 if [ -z "$max" ] || [ "$score" -gt "$max" ]; then
51 max="$score"
52 file="$m"
53 fi
54 done
dc508f57 55 if [ -n "$debug" ]; then echo "using \`$file'" >&2; fi
9fc0aa3b 56 echo "$file"
57 return 0
25378dd8 58}
59
251d97ca 60origargs=("$0" "$@")
1c19e28f 61cmdline=(mplayer -fs -ass)
dc508f57 62debug=
4f06ae97 63unset pretend printfile
40777a19 64
65while [ "${1:0:1}" = - ]; do
66 a="$1"
67 shift
68 case "$a" in
529814da 69 -h)
dc508f57 70 echo "usage: planime [-fdhtC] [-A PLAYER-ARGS... ;] [-s PAR VAL] [--] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
529814da 71 exit 0
72 ;;
dc508f57 73 -d)
74 debug=y
75 ;;
40777a19 76 -t)
77 cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
78 ;;
932c90a8 79 -C)
80 pretend=y
81 ;;
5f7e9143 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 ;;
40777a19 92 -s)
93 savepar="$1"
94 shift
95 saveval="$1"
96 shift
97 ;;
4f06ae97 98 -f)
99 printfile=y
100 ;;
dc508f57 101 --)
102 break
103 ;;
104 *)
105 echo "planime: unknown option \`$a'"
106 exit 1
107 ;;
40777a19 108 esac
109done
110
529814da 111base="$(basename "$(pwd)")"
112nextep=n
113file=
114tqual=
115if [ $# -eq 0 ]; then
116 nextep=y
e299213e 117 if [ -r curser ]; then
118 base="$base $(<curser)"
119 fi
529814da 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
40777a19 131elif [ $# -eq 2 ]; then
529814da 132 if findfile "$base - " "$1" "$2" >/dev/null; then
133 ep="$1"
134 tqual="$2"
135 else
e299213e 136 if [ "$1" = . -a -r curser ]; then
137 base="$base $(<curser)"
138 else
139 base="$base $1"
140 fi
529814da 141 ep="$2"
142 fi
143else
e299213e 144 if [ "$1" = . -a -r curser ]; then
145 base="$base $(<curser)"
146 else
147 base="$base $1"
148 fi
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
b8882e5f 161 file="$(findfile "$base - " "$ep" "$tqual")"
90aff0d0 162fi
529814da 163
4c7a7e70 164if [ -z "$file" -o ! -r "$file" ]; then
529814da 165 echo "planime: no matching file found" >&2
166 exit 1
167fi
168
4c7a7e70 169case "${file##*.}" in
170 ogm)
c6fdc541 171 aid=1
172 sid=0
4c7a7e70 173 ;;
174 mkv)
c6fdc541 175 alang=jpn
176 slang=eng
4c7a7e70 177 ;;
178esac
40777a19 179
b8882e5f 180ifile=".${file}.info"
40777a19 181
182if [ -n "$savepar" ]; then
183 if [ -r "$ifile" ]; then
b8882e5f 184 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
081b510d 185 mv -f "$ifile.new" "$ifile"
40777a19 186 fi
b8882e5f 187 echo "$savepar $saveval" >>"$ifile"
40777a19 188 exit 0
189fi
190
191unset delay
192
193if [ -r "$ifile" ]; then
b8882e5f 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")
21e62b99 201 elif [ "$par" = volmod ]; then
202 cmdline=("${cmdline[@]}" -af volume="$arg")
c6fdc541 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"
b8882e5f 215 fi
216 done
217 exec 0<&3
218 exec 3<&-
c6fdc541 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")
40777a19 230fi
231
21e62b99 232if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi
40777a19 233
932c90a8 234if [ -n "$pretend" ]; then
235 echo "${cmdline[@]}" "$file"
4f06ae97 236elif [ -n "$printfile" ]; then
237 echo "$file"
932c90a8 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
251d97ca 271 fi
272fi