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 |
66 | elif [ $# -eq 1 ]; then |
67 | if [ -r "$1" ]; then |
68 | file="$1" |
69 | else |
70 | if findfile "$base - " "$1" >/dev/null; then |
71 | ep="$1" |
72 | else |
73 | nextep=y |
74 | base="$base $1" |
75 | fi |
76 | fi |
40777a19 |
77 | elif [ $# -eq 2 ]; then |
529814da |
78 | if findfile "$base - " "$1" "$2" >/dev/null; then |
79 | ep="$1" |
80 | tqual="$2" |
81 | else |
82 | base="$base $1" |
83 | ep="$2" |
84 | fi |
85 | else |
86 | base="$base $1" |
40777a19 |
87 | ep="$2" |
529814da |
88 | tqual="$3" |
89 | fi |
90 | if [ "$ep" = . ]; then nextep=y; fi |
91 | if [ "$nextep" = y -a -r nextep ]; then |
92 | ep="$(<nextep)" |
93 | if ! isnum "$ep"; then |
94 | echo "planime: nextep is non-numeric" >&2 |
95 | exit 1 |
96 | fi |
40777a19 |
97 | fi |
529814da |
98 | file="$(findfile "$base - " "$ep" "$qual")" |
99 | |
4c7a7e70 |
100 | if [ -z "$file" -o ! -r "$file" ]; then |
529814da |
101 | echo "planime: no matching file found" >&2 |
102 | exit 1 |
103 | fi |
104 | |
4c7a7e70 |
105 | case "${file##*.}" in |
106 | ogm) |
40777a19 |
107 | defopts=(-aid 1 -sid 0) |
4c7a7e70 |
108 | ;; |
109 | mkv) |
40777a19 |
110 | defopts=(-slang eng -alang jpn) |
4c7a7e70 |
111 | ;; |
112 | esac |
40777a19 |
113 | |
114 | ifile=".${ep}.info" |
115 | |
116 | if [ -n "$savepar" ]; then |
117 | if [ -r "$ifile" ]; then |
081b510d |
118 | egrep -v "^${savepar}:" "$ifile" >"$ifile.new" |
119 | mv -f "$ifile.new" "$ifile" |
40777a19 |
120 | fi |
121 | echo "$savepar: $saveval" >>"$ifile" |
122 | exit 0 |
123 | fi |
124 | |
125 | unset delay |
126 | |
127 | if [ -r "$ifile" ]; then |
251d97ca |
128 | delay="$(sed -n 's/delay *: *\([^ ]*\)$/\1/p' <"$ifile")" |
40777a19 |
129 | cmdline=("${cmdline[@]}" -delay "$delay") |
130 | else |
131 | cmdline=("${cmdline[@]}" "${defopts[@]}") |
132 | fi |
133 | |
134 | if [ "`hostname`" = pc6 ]; then chwp=y; fi |
135 | |
136 | if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi |
4c7a7e70 |
137 | "${cmdline[@]}" "$file" |
251d97ca |
138 | |
139 | if [ "$nextep" = y ]; then |
140 | echo "0. Save and continue" |
141 | echo "1. Continue without saving" |
142 | echo "2. Save and exit" |
143 | echo "3. Exit without saving" |
144 | IFS= read -sn1 c |
145 | save=n |
146 | cont=n |
147 | case "$c" in |
148 | 0|" ") |
149 | save=y |
150 | cont=y |
151 | ;; |
152 | 1) |
153 | cont=y |
154 | ;; |
155 | 2|$'\e') |
156 | save=y |
157 | ;; |
158 | esac |
159 | if [ "$save" = y ]; then |
160 | echo "$(($ep + 1))" >nextep |
161 | fi |
162 | if [ "$cont" = y ]; then |
163 | exec "${origargs[@]}" |
164 | fi |
165 | fi |