41332ae3c369b71a37dae46b3c6a033ee27bf81f
[utils.git] / planime
1 #!/bin/sh
2
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
37 cmdline=(mplayer -fs)
38
39 while [ "${1:0:1}" = - ]; do
40     a="$1"
41     shift
42     case "$a" in
43         -h)
44             echo "usage: planime [-ht] [-s PAR VAL] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
45             exit 0
46             ;;
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
57 done
58
59 base="$(basename "$(pwd)")"
60 nextep=n
61 file=
62 tqual=
63 if [ $# -eq 0 ]; then
64     nextep=y
65 elif [ $# -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
76 elif [ $# -eq 2 ]; then
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
84 else
85     base="$base $1"
86     ep="$2"
87     tqual="$3"
88 fi
89 if [ "$ep" = . ]; then nextep=y; fi
90 if [ "$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
96 fi
97 file="$(findfile "$base - " "$ep" "$qual")"
98
99 if [ -z "$file" -o ! -r "$file" ]; then
100     echo "planime: no matching file found" >&2
101     exit 1
102 fi
103
104 case "${file##*.}" in
105     ogm)
106         defopts=(-aid 1 -sid 0)
107         ;;
108     mkv)
109         defopts=(-slang eng -alang jpn)
110         ;;
111 esac
112
113 ifile=".${ep}.info"
114
115 if [ -n "$savepar" ]; then
116     if [ -r "$ifile" ]; then
117         egrep -v "^${savepar}:" "$ifile" >"$ifile.new"
118         mv -f "$ifile.new" "$ifile"
119     fi
120     echo "$savepar: $saveval" >>"$ifile"
121     exit 0
122 fi
123
124 unset delay
125
126 if [ -r "$ifile" ]; then
127     delay="`sed -n 's/delay *: *\([^ ]*\)$/\1/p' <"$ifile"`"
128     cmdline=("${cmdline[@]}" -delay "$delay")
129 else
130     cmdline=("${cmdline[@]}" "${defopts[@]}")
131 fi
132
133 if [ "`hostname`" = pc6 ]; then chwp=y; fi
134
135 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
136 "${cmdline[@]}" "$file"