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