New argument parsing.
[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" ]; then
100     echo "planime: no matching file found" >&2
101     exit 1
102 fi
103
104 echo "$file"
105 exit 0
106
107 if [ ! -r "$ep" ]; then
108     if [ -z "$qual" ]; then
109         ep="$(basename "$(pwd)") - ${ep}"
110     else
111         ep="$(basename "$(pwd)") ${qual} - ${ep}"
112     fi
113     if [ -r "${ep}.ogm" ]; then
114         ep="${ep}.ogm"
115         defopts=(-aid 1 -sid 0)
116     elif [ -r "${ep}.mkv" ]; then
117         ep="${ep}.mkv"
118         defopts=(-slang eng -alang jpn)
119     elif [ -r "${ep}.mpg" ]; then
120         ep="${ep}.mpg"
121     elif [ -r "${ep}.mp4" ]; then
122         ep="${ep}.mp4"
123     elif [ -r "${ep}.avi" ]; then
124         ep="${ep}.avi"
125     fi
126 fi
127
128 if [ ! -r "$ep" ]; then
129     echo "no such episode: $ep" >&2
130     exit 1
131 fi
132
133 ifile=".${ep}.info"
134
135 if [ -n "$savepar" ]; then
136     if [ -r "$ifile" ]; then
137         egrep -v "^${savepar}:" "$ifile" >"$ifile.new"
138         mv -f "$ifile.new" "$ifile"
139     fi
140     echo "$savepar: $saveval" >>"$ifile"
141     exit 0
142 fi
143
144 unset delay
145
146 if [ -r "$ifile" ]; then
147     delay="`sed -n 's/delay *: *\([^ ]*\)$/\1/p' <"$ifile"`"
148     cmdline=("${cmdline[@]}" -delay "$delay")
149 else
150     cmdline=("${cmdline[@]}" "${defopts[@]}")
151 fi
152
153 if [ "`hostname`" = pc6 ]; then chwp=y; fi
154
155 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
156 "${cmdline[@]}" "$ep"