94bf9c94 |
1 | #!/bin/bash |
40777a19 |
2 | |
25378dd8 |
3 | isnum() { |
4 | grep -xq '[0-9]\+' <<<"$1" |
5 | } |
6 | |
7 | findfile() { |
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 |
37 | origargs=("$0" "$@") |
1c19e28f |
38 | cmdline=(mplayer -fs -ass) |
932c90a8 |
39 | unset pretend |
40777a19 |
40 | |
41 | while [ "${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 |
72 | done |
73 | |
529814da |
74 | base="$(basename "$(pwd)")" |
75 | nextep=n |
76 | file= |
77 | tqual= |
78 | if [ $# -eq 0 ]; then |
79 | nextep=y |
e299213e |
80 | if [ -r curser ]; then |
81 | base="$base $(<curser)" |
82 | fi |
529814da |
83 | elif [ $# -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 |
94 | elif [ $# -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 |
106 | else |
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" |
114 | fi |
115 | if [ "$ep" = . ]; then nextep=y; fi |
116 | if [ "$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 |
122 | fi |
90aff0d0 |
123 | if [ -z "$file" ]; then |
b8882e5f |
124 | file="$(findfile "$base - " "$ep" "$tqual")" |
90aff0d0 |
125 | fi |
529814da |
126 | |
4c7a7e70 |
127 | if [ -z "$file" -o ! -r "$file" ]; then |
529814da |
128 | echo "planime: no matching file found" >&2 |
129 | exit 1 |
130 | fi |
131 | |
4c7a7e70 |
132 | case "${file##*.}" in |
133 | ogm) |
c6fdc541 |
134 | aid=1 |
135 | sid=0 |
4c7a7e70 |
136 | ;; |
137 | mkv) |
c6fdc541 |
138 | alang=jpn |
139 | slang=eng |
4c7a7e70 |
140 | ;; |
141 | esac |
40777a19 |
142 | |
b8882e5f |
143 | ifile=".${file}.info" |
40777a19 |
144 | |
145 | if [ -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 |
152 | fi |
153 | |
154 | unset delay |
155 | |
156 | if [ -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 |
182 | fi |
183 | |
184 | if [ -n "$alang" ]; then |
185 | cmdline=("${cmdline[@]}" -alang "$alang") |
186 | elif [ -n "$aid" ]; then |
187 | cmdline=("${cmdline[@]}" -aid "$aid") |
188 | fi |
189 | if [ -n "$slang" ]; then |
190 | cmdline=("${cmdline[@]}" -slang "$slang") |
191 | elif [ -n "$sid" ]; then |
192 | cmdline=("${cmdline[@]}" -sid "$sid") |
40777a19 |
193 | fi |
194 | |
21e62b99 |
195 | if [ "`hostname`" = pc6 -o "`hostname`" = pc7 -a "$DISPLAY" = :1 ]; then chwp=y; fi |
40777a19 |
196 | |
932c90a8 |
197 | if [ -n "$pretend" ]; then |
198 | echo "${cmdline[@]}" "$file" |
199 | else |
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 |
233 | fi |