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