Handle HTTP redirections automatically.
[doldaconnect.git] / config / dc-filter
CommitLineData
a6478045 1#!/bin/bash
2
d3372da9 3trap "" SIGHUP SIGPIPE
a6478045 4
d3372da9 5if [ ! -d $HOME/dc ]; then mkdir $HOME/dc; fi
6if [ ! -d $HOME/dc/done ]; then mkdir $HOME/dc/done; fi
7if [ ! -d $HOME/dc/resume ]; then mkdir $HOME/dc/resume; fi
8if [ ! -d $HOME/dc/users ]; then mkdir $HOME/dc/users; fi
9exec 2>>$HOME/dc/filterlog
10cd $HOME/dc
a6478045 11
12unset speedrecpath
7ebc7902
FT
13IFS=: binpath=($PATH)
14lepath=()
15for dir in "${binpath[@]}"; do
16 lepath=("${lepath[@]}" "${dir%/bin}/libexec")
17done
18for dir in "${lepath[@]}" /usr/libexec /usr/local/libexec; do
a6478045 19 if [ -x "${dir}/speedrec" ]; then
20 speedrecpath="${dir}/speedrec"
21 fi
22done
23if [ -z "$speedrecpath" ]; then
d3372da9 24 echo "could not find speedrec - using cat instead" >&2
25fi
a6478045 26
d3372da9 27maxsize=0
28unset resfile
29unset infofile
30found=y
31while [ -z "$resfile" -a "$found" = y ]; do
32 unset found
33 for file in resume/*.info; do
34 if [ ! -r "$file" ]; then continue; fi
35 . "$file"
36 if [ "$filesize" -eq "$2" ]; then
37 thisfile="${file%.info}"
38 if [ ! -e "${thisfile}.lock" ]; then
39 size="$(wc -c <"$thisfile")"
40 found=y
41 if [ "$size" -gt "$maxsize" ]; then
42 maxsize="$size"
43 resfile="$thisfile"
44 infofile="$file"
45 fi
46 fi
47 fi
48 done
49 if [ -n "$resfile" ]; then
50 if ! locktouch "${resfile}.lock"; then
51 unset resfile
52 fi
53 fi
54done
a6478045 55
d3372da9 56unset "${!dcarg_@}"
57if [ -n "$resfile" ]; then
58 . "${resfile}.info"
59fi
a6478045 60
d3372da9 61origname="$1"
62shift
63filesize="$1"
64shift
65peername="$1"
66shift
67while [ $# -gt 1 ]; do
68 rec="$1"
69 shift
70 val="$1"
71 shift
72 declare "dcarg_$rec"="$val"
73done
a6478045 74
d3372da9 75if [ -z "$resfile" ]; then
76 resfile="$(mktemp resume/resXXXXXX)"
77 chmod 644 "$resfile"
78 maxsize=0
79 >"${resfile}.lock"
80fi
81declare -p origname filesize "${!dcarg_@}" >"${resfile}.info"
a6478045 82
83echo "resume $maxsize" 2>/dev/null # Avoid EPIPE warning if the server has already closed the transfer.
84
85if [ -z "$speedrecpath" ]; then
86 speedrec=(cat)
d3372da9 87else
a6478045 88 speedrec=("$speedrecpath" "$HOME/dc/users/$(tr / _ <<<"$peername")")
d3372da9 89 peerfile="$(tr / _ <<<"$peername")"
d3372da9 90fi
a6478045 91if ! which tthsum >/dev/null 2>&1; then
92 tthsum=(cat)
93else
94 tthsum=(tthsum -6 -F 3 -s "${resfile}.tthstate" -f)
95fi
96"${speedrec[@]}" | "${tthsum[@]}" 3>"${resfile}.tthfinal" >>"$resfile"
97
d3372da9 98size="$(wc -c <"$resfile")"
a6478045 99
d3372da9 100if [ "$size" -eq 0 ]; then
a6478045 101 rm -f "$resfile" "${resfile}.info" "${resfile}.lock" "${resfile}.tthstate" "${resfile}.tthfinal"
d3372da9 102 exit 1
103fi
a6478045 104
d3372da9 105if [ "$size" -lt "$filesize" ]; then
a6478045 106 rm -f "${resfile}.lock" "${resfile}.tthfinal"
d3372da9 107 exit 1 # Exit code != 0 means restart transfer
108fi
a6478045 109
110tth="$(cat "${resfile}.tthfinal")"
111if [ -z "$tth" ]; then unset tth; fi
112
113rm -f "${resfile}.info" "${resfile}.lock" "${resfile}.tthstate" "${resfile}.tthfinal"
d3372da9 114
115destdir="$HOME/dc/done"
116
117newname="$destdir"/"$origname"
118if [ -e "$newname" ]; then
119 newname="$(mktemp "${newname}XXXXXX")"
120fi
121mv "$resfile" "$newname"
122if [ -x $HOME/dc/complete ]; then
038be21c 123 export "${!dcarg_@}" tth filesize
d3372da9 124 exec $HOME/dc/complete "$newname"
125fi
126exit 0