tools-eget 1.87 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/bin/sh
# eget - simply shell on wget for loading directories over http
# Example use:
# eget ftp://ftp.altlinux.ru/pub/security/ssl/*
#
# Copyright (C) 2014-2014  Etersoft
# Copyright (C) 2014-2014  Daniil Mikhailov <danil@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

Vitaly Lipatov's avatar
Vitaly Lipatov committed
23
WGET="wget"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
24

Vitaly Lipatov's avatar
Vitaly Lipatov committed
25 26 27 28 29 30 31
if [ "$1" = "-q" ] ; then
    WGET="wget -q"
    shift
fi

# TODO:
# -P support
Vitaly Lipatov's avatar
Vitaly Lipatov committed
32

Vitaly Lipatov's avatar
Vitaly Lipatov committed
33 34
# If ftp protocol or have no asterisk, jus download
# TODO: use has()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
35
if echo "$1" | grep -q "\(^ftp://\|[^*]$\)" ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
36 37
    $WGET "$1"
    exit
Vitaly Lipatov's avatar
Vitaly Lipatov committed
38 39
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
40 41 42 43 44 45 46 47 48
echo "Fall to http workaround"

URL=$(echo "$1" | grep "/$" || dirname "$1")
# mask allowed only in last part of path
MASK=$(basename "$1")

get_index()
{
    MYTMPDIR="$(mktemp -d)"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
49 50 51 52
    INDEX=$MYTMPDIR/index
    $WGET $URL -O $INDEX
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
53 54 55 56 57 58 59 60 61 62 63 64 65
print_files()
{
    cat $INDEX | grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2
}

create_fake_files()
{
    DIRALLFILES="$MYTMPDIR/files/"
    mkdir -p "$DIRALLFILES"

    print_files | while read line ; do
        touch $DIRALLFILES/$(basename "$line")
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
66 67
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
68 69 70
download_files()
{
    ERROR=0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
71
    for line in $DIRALLFILES/$MASK ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
72
        $WGET $URL/$(basename "$line") || ERROR=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
73
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
74
    return $ERROR
Vitaly Lipatov's avatar
Vitaly Lipatov committed
75 76
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
77 78 79 80
get_index || exit
create_fake_files
download_files || echo "There was some download errors" >&2
rm -rf "$MYTMPDIR"