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