planime can now load {a,s}{lang,id} from info file.
[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 -ass)
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         -A)
52             while :; do
53                 a="$1"
54                 shift
55                 if [ "$a" = \; ]; then
56                     break;
57                 fi
58                 cmdline=("${cmdline[@]}" "$a")
59             done
60             ;;
61         -s)
62             savepar="$1"
63             shift
64             saveval="$1"
65             shift
66             ;;
67     esac
68 done
69
70 base="$(basename "$(pwd)")"
71 nextep=n
72 file=
73 tqual=
74 if [ $# -eq 0 ]; then
75     nextep=y
76     if [ -r curser ]; then
77         base="$base $(<curser)"
78     fi
79 elif [ $# -eq 1 ]; then
80     if [ -r "$1" ]; then
81         file="$1"
82     else
83         if findfile "$base - " "$1" >/dev/null; then
84             ep="$1"
85         else
86             nextep=y
87             base="$base $1"
88         fi
89     fi
90 elif [ $# -eq 2 ]; then
91     if findfile "$base - " "$1" "$2" >/dev/null; then
92         ep="$1"
93         tqual="$2"
94     else
95         if [ "$1" = . -a -r curser ]; then
96             base="$base $(<curser)"
97         else
98             base="$base $1"
99         fi
100         ep="$2"
101     fi
102 else
103     if [ "$1" = . -a -r curser ]; then
104         base="$base $(<curser)"
105     else
106         base="$base $1"
107     fi
108     ep="$2"
109     tqual="$3"
110 fi
111 if [ "$ep" = . ]; then nextep=y; fi
112 if [ "$nextep" = y -a -r nextep ]; then
113     ep="$(<nextep)"
114     if ! isnum "$ep"; then
115         echo "planime: nextep is non-numeric" >&2
116         exit 1
117     fi
118 fi
119 if [ -z "$file" ]; then
120     file="$(findfile "$base - " "$ep" "$tqual")"
121 fi
122
123 if [ -z "$file" -o ! -r "$file" ]; then
124     echo "planime: no matching file found" >&2
125     exit 1
126 fi
127
128 case "${file##*.}" in
129     ogm)
130         aid=1
131         sid=0
132         ;;
133     mkv)
134         alang=jpn
135         slang=eng
136         ;;
137 esac
138
139 ifile=".${file}.info"
140
141 if [ -n "$savepar" ]; then
142     if [ -r "$ifile" ]; then
143         egrep -v "^${savepar} " "$ifile" >"$ifile.new"
144         mv -f "$ifile.new" "$ifile"
145     fi
146     echo "$savepar $saveval" >>"$ifile"
147     exit 0
148 fi
149
150 unset delay
151
152 if [ -r "$ifile" ]; then
153     exec 3<&0
154     exec 0<"$ifile"
155     while read par arg; do
156         if [ "$par" = delay ]; then
157             cmdline=("${cmdline[@]}" -delay "$arg")
158         elif [ "$par" = aspect ]; then
159             cmdline=("${cmdline[@]}" -aspect "$arg")
160         elif [ "$par" = alang ]; then
161             unset alang aid
162             alang="$arg"
163         elif [ "$par" = aid ]; then
164             unset alang aid
165             aid="$arg"
166         elif [ "$par" = slang ]; then
167             unset slang sid
168             slang="$arg"
169         elif [ "$par" = sid ]; then
170             unset slang sid
171             sid="$arg"
172         fi
173     done
174     exec 0<&3
175     exec 3<&-
176 fi
177
178 if [ -n "$alang" ]; then
179     cmdline=("${cmdline[@]}" -alang "$alang")
180 elif [ -n "$aid" ]; then
181     cmdline=("${cmdline[@]}" -aid "$aid")
182 fi
183 if [ -n "$slang" ]; then
184     cmdline=("${cmdline[@]}" -slang "$slang")
185 elif [ -n "$sid" ]; then
186     cmdline=("${cmdline[@]}" -sid "$sid")
187 fi
188
189 if [ "`hostname`" = pc6 ]; then chwp=y; fi
190
191 if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
192 "${cmdline[@]}" "$file"
193
194 if [ "$nextep" = y ]; then
195     echo "0. Save and continue (or Space)"
196     echo "1. Continue without saving"
197     echo "2. Save and exit (or Enter)"
198     echo "3. Exit without saving (or any key)"
199     IFS= read -sn1 c
200     save=n
201     cont=n
202     case "$c" in
203         0|" ")
204             save=y
205             cont=y
206             ;;
207         1)
208             cont=y
209             ;;
210         2|"")
211             save=y
212             ;;
213     esac
214     if [ "$save" = y ]; then
215         let ep++
216         echo "$ep" >nextep
217     fi
218     if [ "$cont" = y ]; then
219         exec "${origargs[@]}"
220     else
221         echo "nextep is $ep"
222     fi
223 fi