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