Make info file updates atomic.
[utils.git] / planime
CommitLineData
40777a19 1#!/bin/sh
2
3cmdline=(mplayer -fs)
4
5while [ "${1:0:1}" = - ]; do
6 a="$1"
7 shift
8 case "$a" in
9 -t)
10 cmdline=("${cmdline[@]}" -ao oss:/dev/dsp1)
11 ;;
12 -s)
13 savepar="$1"
14 shift
15 saveval="$1"
16 shift
17 ;;
18 esac
19done
20
21if [ $# -lt 1 ]; then
22 echo "usage: planime [QUAL] EPNUM" >&2
23 exit 1
24fi
25if [ $# -eq 1 ]; then
26 ep="$1"
27 unset qual
28elif [ $# -eq 2 ]; then
29 qual="$1"
30 ep="$2"
31fi
32
33if [ ! -r "$ep" ]; then
34 if [ -z "$qual" ]; then
35 ep="$(basename "$(pwd)") - ${ep}"
36 else
37 ep="$(basename "$(pwd)") ${qual} - ${ep}"
38 fi
39 if [ -r "${ep}.ogm" ]; then
40 ep="${ep}.ogm"
41 defopts=(-aid 1 -sid 0)
42 elif [ -r "${ep}.mkv" ]; then
43 ep="${ep}.mkv"
44 defopts=(-slang eng -alang jpn)
45 elif [ -r "${ep}.mpg" ]; then
46 ep="${ep}.mpg"
47 elif [ -r "${ep}.mp4" ]; then
48 ep="${ep}.mp4"
49 elif [ -r "${ep}.avi" ]; then
50 ep="${ep}.avi"
51 fi
52fi
53
54if [ ! -r "$ep" ]; then
55 echo "no such episode: $ep" >&2
56 exit 1
57fi
58
59ifile=".${ep}.info"
60
61if [ -n "$savepar" ]; then
62 if [ -r "$ifile" ]; then
081b510d 63 egrep -v "^${savepar}:" "$ifile" >"$ifile.new"
64 mv -f "$ifile.new" "$ifile"
40777a19 65 fi
66 echo "$savepar: $saveval" >>"$ifile"
67 exit 0
68fi
69
70unset delay
71
72if [ -r "$ifile" ]; then
73 delay="`sed -n 's/delay *: *\([^ ]*\)$/\1/p' <"$ifile"`"
74 cmdline=("${cmdline[@]}" -delay "$delay")
75else
76 cmdline=("${cmdline[@]}" "${defopts[@]}")
77fi
78
79if [ "`hostname`" = pc6 ]; then chwp=y; fi
80
81if [ "$chwp" = y ]; then (sleep 2; randomwp) & fi
82"${cmdline[@]}" "$ep"