Merge branch 'master' of git.dolda2000.com:/srv/git/r/utils
[utils.git] / btadd
1 #!/bin/sh
2
3 if [ $# -lt 1 ]; then
4     echo "usage: btadd [-d DIR] URL..." >&2
5     exit 1
6 fi
7
8 dir="$HOME/bt"
9 rv=0
10 while [ "$#" -gt 0 ]; do
11     arg="$1"
12     shift
13     if [ "$arg" = -d ]; then
14         dir="$1"
15         shift
16     elif [ "$arg" = -o ]; then
17         dir="$HOME/bt/$(date "+%y%m%d-%H%M%S")"
18         mkdir "$dir"
19     else
20         tfile="$(mktemp /tmp/torrentXXXXXX)"
21         if [ "$arg" = - ]; then
22             cat >"$tfile"
23         else
24             if ! wget -qO "$tfile" "$arg"; then
25                 echo "btadd: could not fetch $arg" >&2
26                 rv=1
27                 rm "$tfile"
28                 continue
29             fi
30         fi
31         if ! btcli add -d "$dir" "$tfile"; then
32             rv=1
33         fi
34         rm "$tfile"
35     fi
36 done
37
38 exit $rv