Wait until the PID file is gone.
[utils.git] / autodlctl
CommitLineData
aba77738
DC
1#!/bin/bash
2
3paths=(/home/pub/video/anime $HOME/dc/autodl/cur)
4
5function getnext
6{
7 echo "checking $tag"
8 sexpr="`cat "$d/.autodl/sexpr"`"
9 unset badsizes
10 if [ -r "$d/.autodl/badlist" ]; then
11 read curep <"$d/.autodl/badlist"
12 echo "downloading bad files, curep $curep"
13 epfrom=badlist
aba77738
DC
14 elif [ -r "$d/.autodl/curep" ]; then
15 curep="`cat "$d/.autodl/curep"`"
16 echo "downloading series, curep $curep"
17 epfrom=curep
18 else
19 echo "no available episode of $tag" >&2
20 echo "$tag" >>"$HOME/dc/autodl/faulty"
21 return 1
22 fi
6c554f39 23 unset badsizesl
24 if [ -r "$d/.autodl/badsizes" ]; then
58a2c684 25 badsizesl="$(sed -n "s/^0*$curep \(.*\)$/\1/p" "$d/.autodl/badsizes")"
6c554f39 26 fi
45f2d3f3
DC
27 if [ -n "$badsizesl" ]; then
28 read -a badsizes <<<"$badsizesl"
29 unset badsizesl
30 echo "found bad size list: ${badsizes[@]}"
31 fi
aba77738
DC
32 unset args
33 fsexpr="`printf "$sexpr" "$curep"`"
34 if [ "${#badsizes[@]}" -gt 0 ]; then
35 for badsize in "${badsizes[@]}"; do
36 fsexpr="$fsexpr & ! S=$badsize"
37 done
38 fi
34091d15
DC
39 infofile="$d/.autodl/rtinfo"
40 args=(-e "$fsexpr" -t "$tag $curep" -I "$infofile")
aba77738
DC
41 if [ -r "$d/.autodl/uarg" ]; then
42 uarg="`cat "$d/.autodl/uarg"`"
43 elif [ -e "$d/.autodl/autouarg" ]; then
44 uarg="rename:$tag - %02i.avi:move:../autodl/cur/$tag"
45 fi
46 if [ -n "$uarg" ]; then
47 fuarg="`printf "$uarg" "$curep"`"
48 args=("${args[@]}" -a "$fuarg")
49 fi
58a2c684 50 outfile="$d/.autodl/output"
aba77738
DC
51 echo "trying to download -- autodl ${args[@]}"
52 intr=n
53 autodl "${args[@]}" >"$outfile" 2>&1 &
54 pid=$!
55 trap "intr=y; kill -INT $pid" USR1 INT
56 wait $pid
57 stat=$?
58 if [ "$intr" = y ]; then
59 echo "$tag interrupted"
60 else
61 if [ "$stat" -ne 0 ]; then
58a2c684 62 if [ "$stat" -eq 1 ]; then
63 echo "Failure for $tag" >>"$HOME/dc/autodl/errorlog"
64 tail -n 20 "$outfile" >>"$HOME/dc/autodl/errorlog"
65 elif [ "$stat" -eq 2 ]; then
66 echo "Connection error on $tag"
67 elif [ "$stat" -eq 3 ]; then
68 echo "Configuration error, disabling $tag"
69 cp "$outfile" "$d/.autodl/conferr"
70 touch "$d/.autodl/disabled"
07878795 71 echo "$tag" >>"$HOME/dc/autodl/faulty"
58a2c684 72 fi
aba77738
DC
73 else
74 echo "episode $curep of $tag done"
75 case "$epfrom" in
76 badlist)
77 echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/baddone"
78 egrep -v "^$curep( |\$)" "$d/.autodl/badlist" >"$d/.autodl/newbadlist"
79 mv -f "$d/.autodl/newbadlist" "$d/.autodl/badlist"
80 if [ `wc -l <"$d/.autodl/badlist"` -eq 0 ]; then
81 rm "$d/.autodl/badlist"
82 if [ -r "$d/.autodl/curep" ]; then
83 if [ -r "$d/.autodl/maxep" ] && [ "`cat "$d/.autodl/curep"`" -gt "`cat "$d/.autodl/maxep"`" ]; then
84 touch "$d/.autodl/disabled"
85 fi
86 else
87 touch "$d/.autodl/disabled"
88 fi
89 echo "$tag has no more bad episodes"
90 echo "$tag" >>"$HOME/dc/autodl/badmaxed"
91 fi
92 ;;
93 curep)
94 echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/done"
95 let curep++
96 echo "$curep" >"$d/.autodl/curep"
97 if [ -r "$d/.autodl/maxep" ]; then
98 if [ "$curep" -gt "`cat "$d/.autodl/maxep"`" ]; then
99 touch "$d/.autodl/disabled"
100 echo "$tag has reached max"
101 echo "$tag" >>"$HOME/dc/autodl/maxed"
102 fi
103 fi
104 ;;
105 esac
106 fi
107 fi
108 rm -f "$outfile"
109 rm -f "$HOME/dc/autodl/run/$tag"
110}
111
112for dir in $HOME/dc/autodl{,/cur,/run}; do
113 if [ -e "$dir" ]; then
114 if [ ! -d "$dir" ]; then
115 echo "$dir is not a directory, please remedy and restart" >&2
116 exit 1
117 fi
118 else
119 mkdir "$dir" || exit 1
120 fi
121done
122
123while [ $# -gt 0 ]; do
124 arg="$1"
125 shift
126 case "$arg" in
127 -k)
128 pid="$(cat "$HOME/dc/autodl/run/master")"
129 if [ -z "$pid" ]; then
130 echo "autodlctl: could not read a PID from $HOME/dc/autodl/run/master" >&2
131 exit 1
132 fi
133 kill "$pid"
134 exit 0
135 ;;
136 *)
137 echo "autodlctl: unrecognized option: \"$arg\"" >&2
138 exit 1
139 ;;
140 esac
141done
142
143lastget=0
144done=n
145trap "done=y" INT QUIT TERM
146echo $$ >"$HOME/dc/autodl/run/master"
147while [ "$done" != y ]; do
148 for pidfile in $HOME/dc/autodl/run/*; do
149 if [ "$pidfile" = "$HOME/dc/autodl/run/*" ]; then break; fi
150 pid="`cat "$pidfile"`"
151 if [ -d /proc/1 -a ! -d "/proc/$pid" ]; then
152 echo "removing stale pidfile $pidfile"
153 rm -f "$pidfile"
154 fi
155 done
156 for p in "${paths[@]}"; do
157 for d in "$p"/*; do
158 if [ -d "$d/.autodl" -a ! -e "$d/.autodl/disabled" ]; then
159 if [ -r "$d/.autodl/tag" ]; then
160 tag="`cat "$d/.autodl/tag"`"
161 else
162 tag="`basename "$d"`"
163 fi
07878795 164 start=y
aba77738
DC
165 if [ -e "$d/.autodl/disable" ]; then
166 echo "disabling $tag per user request"
07878795 167 start=n
aba77738
DC
168 touch "$d/.autodl/disabled"
169 rm -f "$d/.autodl/disable"
170 if [ -r "$HOME/dc/autodl/run/$tag" ]; then
171 pid="`cat "$HOME/dc/autodl/run/$tag"`"
172 echo "sending SIGUSR1 to $pid"
173 kill -USR1 "$pid"
174 else
175 echo "could not find pid for $tag"
176 fi
07878795 177 fi
178 if [ -e "$d/.autodl/restart" ]; then
179 echo "restarting $tag per user request"
180 rm -f "$d/.autodl/restart"
181 if [ -r "$HOME/dc/autodl/run/$tag" ]; then
182 pid="`cat "$HOME/dc/autodl/run/$tag"`"
183 echo "sending SIGUSR1 to $pid"
184 kill -USR1 "$pid"
ccc141a3 185 while [ -e "$HOME/dc/autodl/run/$tag" ]; do
186 echo "waiting for it to exit"
187 sleep 1
188 done
07878795 189 else
190 echo "could not find pid for $tag"
191 fi
192 fi
193 if [ $start = y ]; then
194 if [ ! -r "$d/.autodl/sexpr" ]; then
195 touch "$d/.autodl/disabled"
196 echo "$tag lacks sexpr" >&2
197 echo "$tag" >>"$HOME/dc/autodl/faulty"
198 else
199 if [ ! -e "$HOME/dc/autodl/run/$tag" ]; then
200 if [ $((`date +%s` - $lastget)) -gt 20 ]; then
201 getnext "$d" "$tag" &
202 lastget=`date +%s`
203 pid=$!
204 echo "$pid" >"$HOME/dc/autodl/run/$tag"
205 fi
aba77738
DC
206 fi
207 fi
208 fi
209 fi
210 done
211 done
212 sleep 10
213done
214
215for pidfile in $HOME/dc/autodl/run/*; do
216 if [ "$pidfile" = "$HOME/dc/autodl/run/*" ]; then break; fi
217 if [ "$(basename "$pidfile")" = master ]; then continue; fi
218 pid="`cat "$pidfile"`"
219 echo "sending SIGUSR1 to $pid for `basename "$pidfile"`"
220 kill -USR1 "$pid"
221done
222
223rm -f "$HOME/dc/autodl/run/master"