Use bash explicitly.
[utils.git] / planime
1 #!/bin/bash
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 origargs=("$0" "$@")
38 cmdline=(mplayer -fs)
39
40 while [ "${1:0:1}" = - ]; do
41     a="$1"
42     shift
43     case "$a" in
44         -h)
45             echo "usage: planime [-ht] [-s PAR VAL] [NAME-QUAL] [EP|.] [TYPE-QUAL]" >&2
46             exit 0
47             ;;
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
60 base="$(basename "$(pwd)")"
61 nextep=n
62 file=
63 tqual=
64 if [ $# -eq 0 ]; then
65     nextep=y
66     if [ -r curser ]; then
67         base="$base $(<curser)"
68     fi
69 elif [ $# -eq 1 ]; then
70     if [ -r "$1" ]; then
71         file="$1"
72     else
73         if findfile "$base - " "$1" >/dev/null; then
74             ep="$1"
75         else
76             nextep=y
77             base="$base $1"
78         fi
79     fi
80 elif [ $# -eq 2 ]; then
81     if findfile "$base - " "$1" "$2" >/dev/null; then
82         ep="$1"
83         tqual="$2"
84     else
85         if [ "$1" = . -a -r curser ]; then
86             base="$base $(<curser)"
87         else
88             base="$base $1"
89         fi
90         ep="$2"
91     fi
92 else
93     if [ "$1" = . -a -r curser ]; then
94         base="$base $(<curser)"
95     else
96         base="$base $1"
97     fi
98     ep="$2"
99     tqual="$3"
100 fi
101 if [ "$ep" = . ]; then nextep=y; fi
102 if [ "$nextep" = y -a -r nextep ]; then
103     ep="$(<nextep)"
104     if ! isnum "$ep"; then
105         echo "planime: nextep is non-numeric" >&2
106         exit 1
107     fi
108 fi
109 if [ -z "$file" ]; then
110     file="$(findfile "$base - " "$ep" "$tqual")"
111 fi
112
113 if [ -z "$file" -o ! -r "$file" ]; then
114     echo "planime: no matching file found" >&2
115     exit 1
116 fi
117
118 case "${file##*.}" in
119     ogm)
120         defopts=(-aid 1 -sid 0)
121         ;;
122     mkv)
123         defopts=(-slang eng -alang jpn)
124         ;;
125 esac
126
127 ifile=".${file}.info"
128
129 if [ -n "$savepar" ]; then
130     if [ -r "$ifile" ]; then
131         egrep -v "^${savepar} " "$ifile" >"$ifile.new"
132         mv -f "$ifile.new" "$ifile"
133     fi
134     echo "$savepar $saveval" >>"$ifile"
135     exit 0
136 fi
137
138 unset delay
139
140 if [ -r "$ifile" ]; then
141     exec 3<&0
142     exec 0<"$ifile"
143     while read par arg; do
144         if [ "$par" = delay ]; then
145             cmdline=("${cmdline[@]}" -delay "$arg")
146         elif [ "$par" = aspect ]; then
147             cmdline=("${cmdline[@]}" -aspect "$arg")
148         fi
149     done
150     exec 0<&3
151     exec 3<&-
152 else
153     cmdline=("${cmdline[@]}" "${defopts[@]}")
154 fi
155
156 if [ "`hostname`" = pc6 ]; then chwp=y; fi
157
158 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
159 "${cmdline[@]}" "$file"
160
161 if [ "$nextep" = y ]; then
162     echo "0. Save and continue (or Space)"
163     echo "1. Continue without saving"
164     echo "2. Save and exit (or Enter)"
165     echo "3. Exit without saving (or any key)"
166     IFS= read -sn1 c
167     save=n
168     cont=n
169     case "$c" in
170         0|" ")
171             save=y
172             cont=y
173             ;;
174         1)
175             cont=y
176             ;;
177         2|"")
178             save=y
179             ;;
180     esac
181     if [ "$save" = y ]; then
182         let ep++
183         echo "$ep" >nextep
184     fi
185     if [ "$cont" = y ]; then
186         exec "${origargs[@]}"
187     else
188         echo "nextep is $ep"
189     fi
190 fi