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