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