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