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