anndl: Fixed a couple of bugs.
[utils.git] / autodlctl
index e5a19f4..134fb8d 100755 (executable)
--- a/autodlctl
+++ b/autodlctl
@@ -2,27 +2,49 @@
 
 paths=(/home/pub/video/anime $HOME/dc/autodl/cur)
 
+function findcurep
+{
+    if [ -r badlist ]; then
+       echo badlist `head -n 1 badlist`
+       return 0
+    fi
+    if [ -r curep ]; then
+       if [ -r maxep ]; then
+           if [ "`cat curep`" -le "`cat maxep`" ]; then
+               echo curep `cat curep`
+               return 0
+           fi
+       else
+           echo curep `cat curep`
+           return 0
+       fi
+    fi
+    echo none
+    return 1
+}
+
 function getnext
 {
+    cd "$1/.autodl/"
+    tag="$2"
     echo "checking $tag"
-    sexpr="`cat "$d/.autodl/sexpr"`"
+    sexpr="`cat sexpr`"
     unset badsizes
-    if [ -r "$d/.autodl/badlist" ]; then
-       read curep <"$d/.autodl/badlist"
-       echo "downloading bad files, curep $curep"
-       epfrom=badlist
-    elif [ -r "$d/.autodl/curep" ]; then
-       curep="`cat "$d/.autodl/curep"`"
-       echo "downloading series, curep $curep"
-       epfrom=curep
-    else
+    epinfo=(`findcurep`)
+    if [ "${epinfo[0]}" = none ]; then
        echo "no available episode of $tag" >&2
        echo "$tag" >>"$HOME/dc/autodl/faulty"
+       touch disabled
        return 1
     fi
-    badsizesl="$(sed -n "s/^$curep \(.*\)$/\1/p" "$d/.autodl/badsizes")"
+    epfrom="${epinfo[0]}"
+    curep="${epinfo[1]}"
+    unset badsizesl
+    if [ -r badsizes ]; then
+       badsizesl="$(sed -n "s/^0*$curep \([^#]*\)\( *#.*\)\?$/\1/p" badsizes)"
+    fi
     if [ -n "$badsizesl" ]; then
-       read -a badsizes <<<"$badsizesl"
+       read -d/ -a badsizes <<<"$badsizesl"
        unset badsizesl
        echo "found bad size list: ${badsizes[@]}"
     fi
@@ -33,18 +55,22 @@ function getnext
            fsexpr="$fsexpr & ! S=$badsize"
        done
     fi
-    infofile="$d/.autodl/rtinfo"
-    args=(-e "$fsexpr" -t "$tag $curep" -I "$infofile")
-    if [ -r "$d/.autodl/uarg" ]; then
-       uarg="`cat "$d/.autodl/uarg"`"
-    elif [ -e "$d/.autodl/autouarg" ]; then
-       uarg="rename:$tag - %02i.avi:move:../autodl/cur/$tag"
+    infofile=rtinfo
+    estatfile=estat
+    args=(-e "$fsexpr" -t "$tag $curep" -I "$infofile" -E "$estatfile" -x "curep=$curep")
+    if [ -e wait ]; then
+       args=("${args[@]}" -w)
+    fi
+    if [ -r uarg ]; then
+       uarg="`cat uarg`"
+    elif [ -e autouarg ]; then
+       uarg="rename:$tag - %02i.ext:move:../autodl/cur/$tag"
     fi
     if [ -n "$uarg" ]; then
        fuarg="`printf "$uarg" "$curep"`"
        args=("${args[@]}" -a "$fuarg")
     fi
-    outfile="`mktemp /tmp/autodlXXXXXX`"
+    outfile=output
     echo "trying to download -- autodl ${args[@]}"
     intr=n
     autodl "${args[@]}" >"$outfile" 2>&1 &
@@ -52,45 +78,68 @@ function getnext
     trap "intr=y; kill -INT $pid" USR1 INT
     wait $pid
     stat=$?
+    if [ -r "$estatfile" ]; then
+       estat=(`cat "$estatfile"`)
+       rm -f "$estatfile"
+    fi
     if [ "$intr" = y ]; then
        echo "$tag interrupted"
     else
        if [ "$stat" -ne 0 ]; then
-           echo "Failure for $tag" >>"$HOME/dc/autodl/errorlog"
-           tail -n 20 "$outfile" >>"$HOME/dc/autodl/errorlog"
+           if [ "$stat" -eq 1 ]; then
+               echo "Failure for $tag" >>"$HOME/dc/autodl/errorlog"
+               tail -n 20 "$outfile" >>"$HOME/dc/autodl/errorlog"
+           elif [ "$stat" -eq 2 ]; then
+               echo "Connection error on $tag"
+           elif [ "$stat" -eq 3 ]; then
+               echo "Configuration error, disabling $tag"
+               cp "$outfile" conferr
+               touch disabled
+               echo "$tag" >>"$HOME/dc/autodl/faulty"
+           fi
        else
-           echo "episode $curep of $tag done"
+           echo "episode $curep of $tag done (estat: \"$estat\")"
            case "$epfrom" in
                badlist)
                    echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/baddone"
-                   egrep -v "^$curep( |\$)" "$d/.autodl/badlist" >"$d/.autodl/newbadlist"
-                   mv -f "$d/.autodl/newbadlist" "$d/.autodl/badlist"
-                   if [ `wc -l <"$d/.autodl/badlist"` -eq 0 ]; then
-                       rm "$d/.autodl/badlist"
-                       if [ -r "$d/.autodl/curep" ]; then
-                           if [ -r "$d/.autodl/maxep" ] && [ "`cat "$d/.autodl/curep"`" -gt "`cat "$d/.autodl/maxep"`" ]; then
-                               touch "$d/.autodl/disabled"
-                           fi
-                       else
-                           touch "$d/.autodl/disabled"
-                       fi
+                   egrep -v "^$curep( |\$)" badlist >newbadlist
+                   mv -f newbadlist badlist
+                   if [ `wc -l <badlist` -eq 0 ]; then
+                       rm badlist
                        echo "$tag has no more bad episodes"
                        echo "$tag" >>"$HOME/dc/autodl/badmaxed"
                    fi
                    ;;
                curep)
-                   echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/done"
-                   let curep++
-                   echo "$curep" >"$d/.autodl/curep"
-                   if [ -r "$d/.autodl/maxep" ]; then
-                       if [ "$curep" -gt "`cat "$d/.autodl/maxep"`" ]; then
-                           touch "$d/.autodl/disabled"
+                   if [ "$estat" = dbl ]; then
+                       echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/done"
+                       echo -en "${tag}\n$((${curep} + 1))\n" >>"$HOME/dc/autodl/done"
+                       let "nextep=curep+2"
+                   else
+                       echo -en "${tag}\n${curep}\n" >>"$HOME/dc/autodl/done"
+                       let "nextep=curep+1"
+                   fi
+                   echo "$nextep" >curep
+                   if [ -r maxep ]; then
+                       if [ "$nextep" -gt "`cat maxep`" ]; then
                            echo "$tag has reached max"
                            echo "$tag" >>"$HOME/dc/autodl/maxed"
                        fi
                    fi
                    ;;
            esac
+           if [ "$estat" = bad ]; then
+               echo "episode reported as bad, adding to badlist"
+               echo "$curep" >>badlist
+           elif [ "${estat[0]}" = badsize ]; then
+               echo "size reported as bad"
+               echo "$curep" >>badlist
+               echo "$curep ${estat[1]}" >>badsizes
+           fi
+           if ! findcurep; then
+               echo "no more episodes to download from $tag"
+               touch disabled
+           fi
        fi
     fi
     rm -f "$outfile"
@@ -149,8 +198,10 @@ while [ "$done" != y ]; do
                else
                    tag="`basename "$d"`"
                fi
+               start=y
                if [ -e "$d/.autodl/disable" ]; then
                    echo "disabling $tag per user request"
+                   start=n
                    touch "$d/.autodl/disabled"
                    rm -f "$d/.autodl/disable"
                    if [ -r "$HOME/dc/autodl/run/$tag" ]; then
@@ -160,17 +211,35 @@ while [ "$done" != y ]; do
                    else
                        echo "could not find pid for $tag"
                    fi
-               elif [ ! -r "$d/.autodl/sexpr" ]; then
-                   touch "$d/.autodl/disabled"
-                   echo "$tag lacks sexpr" >&2
-                   echo "$tag" >>"$HOME/dc/autodl/faulty"
-               else
-                   if [ ! -e "$HOME/dc/autodl/run/$tag" ]; then
-                       if [ $((`date +%s` - $lastget)) -gt 20 ]; then
-                           getnext "$d" "$tag" &
-                           lastget=`date +%s`
-                           pid=$!
-                           echo "$pid" >"$HOME/dc/autodl/run/$tag"
+               fi
+               if [ -e "$d/.autodl/restart" ]; then
+                   echo "restarting $tag per user request"
+                   rm -f "$d/.autodl/restart"
+                   if [ -r "$HOME/dc/autodl/run/$tag" ]; then
+                       pid="`cat "$HOME/dc/autodl/run/$tag"`"
+                       echo "sending SIGUSR1 to $pid"
+                       kill -USR1 "$pid"
+                       while [ -e "$HOME/dc/autodl/run/$tag" ]; do
+                           echo "waiting for it to exit"
+                           sleep 1
+                       done
+                   else
+                       echo "could not find pid for $tag"
+                   fi
+               fi
+               if [ $start = y ]; then
+                   if [ ! -r "$d/.autodl/sexpr" ]; then
+                       touch "$d/.autodl/disabled"
+                       echo "$tag lacks sexpr" >&2
+                       echo "$tag" >>"$HOME/dc/autodl/faulty"
+                   else
+                       if [ ! -e "$HOME/dc/autodl/run/$tag" ]; then
+                           if [ $((`date +%s` - $lastget)) -gt 20 ]; then
+                               getnext "$d" "$tag" &
+                               lastget=`date +%s`
+                               pid=$!
+                               echo "$pid" >"$HOME/dc/autodl/run/$tag"
+                           fi
                        fi
                    fi
                fi