40777a19 |
1 | #!/bin/sh |
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" "$@") |
40777a19 |
38 | cmdline=(mplayer -fs) |
39 | |
40 | while [ "${1:0:1}" = - ]; do |
41 | a="$1" |
42 | shift |
43 | case "$a" in |
529814da |
44 | -h) |
45 | echo "usage: planime [-ht] [-s PAR VAL] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2 |
46 | exit 0 |
47 | ;; |
40777a19 |
48 | -t) |
49 | cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1) |
50 | ;; |
51 | -s) |
52 | savepar="$1" |
53 | shift |
54 | saveval="$1" |
55 | shift |
56 | ;; |
57 | esac |
58 | done |
59 | |
529814da |
60 | base="$(basename "$(pwd)")" |
61 | nextep=n |
62 | file= |
63 | tqual= |
64 | if [ $# -eq 0 ]; then |
65 | nextep=y |
e299213e |
66 | if [ -r curser ]; then |
67 | base="$base $(<curser)" |
68 | fi |
529814da |
69 | elif [ $# -eq 1 ]; then |
70 | if [ -r "$1" ]; then |
71 | file="$1" |
72 | else |
73 | if findfile "$base - " "$1" >/dev/null; then |
74 | ep="$1" |
75 | else |
76 | nextep=y |
77 | base="$base $1" |
78 | fi |
79 | fi |
40777a19 |
80 | elif [ $# -eq 2 ]; then |
529814da |
81 | if findfile "$base - " "$1" "$2" >/dev/null; then |
82 | ep="$1" |
83 | tqual="$2" |
84 | else |
e299213e |
85 | if [ "$1" = . -a -r curser ]; then |
86 | base="$base $(<curser)" |
87 | else |
88 | base="$base $1" |
89 | fi |
529814da |
90 | ep="$2" |
91 | fi |
92 | else |
e299213e |
93 | if [ "$1" = . -a -r curser ]; then |
94 | base="$base $(<curser)" |
95 | else |
96 | base="$base $1" |
97 | fi |
40777a19 |
98 | ep="$2" |
529814da |
99 | tqual="$3" |
100 | fi |
101 | if [ "$ep" = . ]; then nextep=y; fi |
102 | if [ "$nextep" = y -a -r nextep ]; then |
103 | ep="$(<nextep)" |
104 | if ! isnum "$ep"; then |
105 | echo "planime: nextep is non-numeric" >&2 |
106 | exit 1 |
107 | fi |
40777a19 |
108 | fi |
90aff0d0 |
109 | if [ -z "$file" ]; then |
b8882e5f |
110 | file="$(findfile "$base - " "$ep" "$tqual")" |
90aff0d0 |
111 | fi |
529814da |
112 | |
4c7a7e70 |
113 | if [ -z "$file" -o ! -r "$file" ]; then |
529814da |
114 | echo "planime: no matching file found" >&2 |
115 | exit 1 |
116 | fi |
117 | |
4c7a7e70 |
118 | case "${file##*.}" in |
119 | ogm) |
40777a19 |
120 | defopts=(-aid 1 -sid 0) |
4c7a7e70 |
121 | ;; |
122 | mkv) |
40777a19 |
123 | defopts=(-slang eng -alang jpn) |
4c7a7e70 |
124 | ;; |
125 | esac |
40777a19 |
126 | |
b8882e5f |
127 | ifile=".${file}.info" |
40777a19 |
128 | |
129 | if [ -n "$savepar" ]; then |
130 | if [ -r "$ifile" ]; then |
b8882e5f |
131 | egrep -v "^${savepar} " "$ifile" >"$ifile.new" |
081b510d |
132 | mv -f "$ifile.new" "$ifile" |
40777a19 |
133 | fi |
b8882e5f |
134 | echo "$savepar $saveval" >>"$ifile" |
40777a19 |
135 | exit 0 |
136 | fi |
137 | |
138 | unset delay |
139 | |
140 | if [ -r "$ifile" ]; then |
b8882e5f |
141 | exec 3<&0 |
142 | exec 0<"$ifile" |
143 | while read par arg; do |
144 | if [ "$par" = delay ]; then |
145 | cmdline=("${cmdline[@]}" -delay "$arg") |
146 | elif [ "$par" = aspect ]; then |
147 | cmdline=("${cmdline[@]}" -aspect "$arg") |
148 | fi |
149 | done |
150 | exec 0<&3 |
151 | exec 3<&- |
40777a19 |
152 | else |
153 | cmdline=("${cmdline[@]}" "${defopts[@]}") |
154 | fi |
155 | |
156 | if [ "`hostname`" = pc6 ]; then chwp=y; fi |
157 | |
158 | if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi |
4c7a7e70 |
159 | "${cmdline[@]}" "$file" |
251d97ca |
160 | |
161 | if [ "$nextep" = y ]; then |
1f6c2e48 |
162 | echo "0. Save and continue (or Space)" |
251d97ca |
163 | echo "1. Continue without saving" |
1f6c2e48 |
164 | echo "2. Save and exit (or Enter)" |
165 | echo "3. Exit without saving (or any key)" |
251d97ca |
166 | IFS= read -sn1 c |
167 | save=n |
168 | cont=n |
169 | case "$c" in |
170 | 0|" ") |
171 | save=y |
172 | cont=y |
173 | ;; |
174 | 1) |
175 | cont=y |
176 | ;; |
2d68de16 |
177 | 2|"") |
251d97ca |
178 | save=y |
179 | ;; |
180 | esac |
181 | if [ "$save" = y ]; then |
90aff0d0 |
182 | let ep++ |
183 | echo "$ep" >nextep |
251d97ca |
184 | fi |
185 | if [ "$cont" = y ]; then |
186 | exec "${origargs[@]}" |
90aff0d0 |
187 | else |
188 | echo "nextep is $ep" |
251d97ca |
189 | fi |
190 | fi |