#!/bin/sh

usage() {
    echo "This is default download recipe for leech"
}

if [ -z "$LEECH_DOWNLOADS_DIR" ]; then
    usage
    exit 1
fi

OUTPUT="$LEECH_DOWNLOADS_DIR/$LEECH_URL_MD5.torrent"
echo $OUTPUT

# optional cookie
[ ! -z "$COOKIE" ] && CURL_COOKIE_OPTS="-b $COOKIE"

# -s for silent, no output
# -f for not outputting failed downloads to files
# -C - to continue incomplete downloads
# --connect_timeout and --max-time for timeouting, default value (30 secs) should be enough for torrent files, otherwise download will continue on next try
# -k to ignore invalid SSL certificates
# -L to correctly handle redirects
# -g to disable globbing in URLs
#
cd "$LEECH_DOWNLOADS_DIR" && exec curl -s -f -k -L -g \
    --connect-timeout "$LEECH_TIMEOUT" --max-time "$LEECH_TIMEOUT" \
    -b "$COOKIE" \
    -C - \
    "$LEECH_URL" >"$OUTPUT"
