Can someone make a simple zenity script for zsync which just takes the location of the file to be synced and the zsync file and show progress ?
1 Answer
zsync has terrible output for parsing. You'd be crazy to attempt that.
That said, I'm apparently crazy.
#!/bin/bash
url=$1
seedfile=$2
tempdir=$(mktemp -d)
msgfile=$tempdir/message
trap 'rm -rf "$tempdir"' EXIT
down_filter() {
local message line percent speed
echo "Downloading zsync file" > "$msgfile"
read -r -n1 _
while read -r line; do
if [[ $line = [-#]* ]]; then
read -r _ percent speed <<< "$line"
echo "#$(<"$msgfile")\n$speed"
if (( ${percent%%.*} < 100 )); then
echo "$percent"
else
echo "99.9%"
fi
fi
done < <(awk 'BEGIN{RS="[\r\n]"} {print;fflush()}')
}
seed_filter() {
local message file count size point
while read -r -d '*' line; do
file=${line%:*}
message+="\n$file"
echo "$message" > "$msgfile"
file=${file#reading seed file }
count=1
size=$(( $(wc -c < "$file") / 1000000 + 1 ))
echo "#$message"
while read -r -n1 point; do
[[ $point = '*' ]] || break
((count++))
echo "$(( 100 * count / size ))%"
done
read -r message;
echo "$message" > "$msgfile"
done
}
if [[ -z $url ]]; then
url=$(zenity --entry \
--title=zsync \
--text="Enter URL to zsync file" \
--width=500 \
--height=100 \
) || exit
fi
if [[ -z $seedfile || ! -e $seedfile ]]; then
seedfile=$(zenity --file-selection \
--title="zsync $url" \
--text="Choose a seed-file" \
)
fi
{
zsync ${seedfile:+-i "$seedfile"} "$url" \
> >(down_filter >&3) 2> >(seed_filter >&3)
} 3> >(zenity --progress \
--title="zsync $url" \
--width=500 \
--height=100 \
)
The script will have a lot of weaknesses. Any slight change in the output of zsync in a newer version would likely break it. (Only tested against Ubuntu 10.04's zsync v0.6.1). For some odd reason it outputs some of the progress to stdout, and some to stderr, which makes it extra hard to parse.
Also, it won't handle certain odd characters in filenames, like \
, *
or newlines.
Expect bugs; I haven't really tested it that much.
Anyway, I had never heard of zsync before. It's a neat tool, so thanks for letting me know about it at least. :)
Screenshots :