New extension handling.
[utils.git] / planime
CommitLineData
40777a19 1#!/bin/sh
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
40777a19 37cmdline=(mplayer -fs)
38
39while [ "${1:0:1}" = - ]; do
40 a="$1"
41 shift
42 case "$a" in
529814da 43 -h)
44 echo "usage: planime [-ht] [-s PAR VAL] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
45 exit 0
46 ;;
40777a19 47 -t)
48 cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
49 ;;
50 -s)
51 savepar="$1"
52 shift
53 saveval="$1"
54 shift
55 ;;
56 esac
57done
58
529814da 59base="$(basename "$(pwd)")"
60nextep=n
61file=
62tqual=
63if [ $# -eq 0 ]; then
64 nextep=y
65elif [ $# -eq 1 ]; then
66 if [ -r "$1" ]; then
67 file="$1"
68 else
69 if findfile "$base - " "$1" >/dev/null; then
70 ep="$1"
71 else
72 nextep=y
73 base="$base $1"
74 fi
75 fi
40777a19 76elif [ $# -eq 2 ]; then
529814da 77 if findfile "$base - " "$1" "$2" >/dev/null; then
78 ep="$1"
79 tqual="$2"
80 else
81 base="$base $1"
82 ep="$2"
83 fi
84else
85 base="$base $1"
40777a19 86 ep="$2"
529814da 87 tqual="$3"
88fi
89if [ "$ep" = . ]; then nextep=y; fi
90if [ "$nextep" = y -a -r nextep ]; then
91 ep="$(<nextep)"
92 if ! isnum "$ep"; then
93 echo "planime: nextep is non-numeric" >&2
94 exit 1
95 fi
40777a19 96fi
529814da 97file="$(findfile "$base - " "$ep" "$qual")"
98
4c7a7e70 99if [ -z "$file" -o ! -r "$file" ]; then
529814da 100 echo "planime: no matching file found" >&2
101 exit 1
102fi
103
4c7a7e70 104case "${file##*.}" in
105 ogm)
40777a19 106 defopts=(-aid 1 -sid 0)
4c7a7e70 107 ;;
108 mkv)
40777a19 109 defopts=(-slang eng -alang jpn)
4c7a7e70 110 ;;
111esac
40777a19 112
113ifile=".${ep}.info"
114
115if [ -n "$savepar" ]; then
116 if [ -r "$ifile" ]; then
081b510d 117 egrep -v "^${savepar}:" "$ifile" >"$ifile.new"
118 mv -f "$ifile.new" "$ifile"
40777a19 119 fi
120 echo "$savepar: $saveval" >>"$ifile"
121 exit 0
122fi
123
124unset delay
125
126if [ -r "$ifile" ]; then
127 delay="`sed -n 's/delay *: *\([^ ]*\)$/\1/p' <"$ifile"`"
128 cmdline=("${cmdline[@]}" -delay "$delay")
129else
130 cmdline=("${cmdline[@]}" "${defopts[@]}")
131fi
132
133if [ "`hostname`" = pc6 ]; then chwp=y; fi
134
135if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
4c7a7e70 136"${cmdline[@]}" "$file"