Check in ordershare.
[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
061f96f3 22 tail="${file#"$1"}"
25378dd8 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
44423004
FT
111dirbase="$(basename "$(pwd)")"
112base=.
529814da 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
44423004
FT
122 base=
123 ep="$1"
529814da 124 fi
40777a19 125elif [ $# -eq 2 ]; then
44423004
FT
126 if findfile "$dirbase - " "$1" "$2" >/dev/null; then
127 base=""
529814da 128 ep="$1"
129 tqual="$2"
130 else
44423004 131 base="$1"
529814da 132 ep="$2"
133 fi
134else
44423004 135 base="$1"
40777a19 136 ep="$2"
529814da 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
40777a19 146fi
90aff0d0 147if [ -z "$file" ]; then
44423004
FT
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
90aff0d0 163fi
529814da 164
4c7a7e70 165if [ -z "$file" -o ! -r "$file" ]; then
529814da 166 echo "planime: no matching file found" >&2
167 exit 1
168fi
169
4c7a7e70 170case "${file##*.}" in
171 ogm)
c6fdc541 172 aid=1
173 sid=0
4c7a7e70 174 ;;
175 mkv)
c6fdc541 176 alang=jpn
177 slang=eng
4c7a7e70 178 ;;
179esac
40777a19 180
b8882e5f 181ifile=".${file}.info"
40777a19 182
183if [ -n "$savepar" ]; then
184 if [ -r "$ifile" ]; then
b8882e5f 185 egrep -v "^${savepar} " "$ifile" >"$ifile.new"
081b510d 186 mv -f "$ifile.new" "$ifile"
40777a19 187 fi
b8882e5f 188 echo "$savepar $saveval" >>"$ifile"
40777a19 189 exit 0
190fi
191
192unset delay
193
194if [ -r "$ifile" ]; then
b8882e5f 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")
21e62b99 202 elif [ "$par" = volmod ]; then
203 cmdline=("${cmdline[@]}" -af volume="$arg")
c6fdc541 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"
b8882e5f 216 fi
217 done
218 exec 0<&3
219 exec 3<&-
c6fdc541 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")
40777a19 231fi
232
21e62b99 233if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi
40777a19 234
932c90a8 235if [ -n "$pretend" ]; then
236 echo "${cmdline[@]}" "$file"
4f06ae97 237elif [ -n "$printfile" ]; then
238 echo "$file"
932c90a8 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
251d97ca 272 fi
273fi