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