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