tools_eget 2.08 KB
Newer Older
Vitaly Lipatov's avatar
Vitaly Lipatov committed
1 2 3 4 5
#!/bin/sh
# eget - simply shell on wget for loading directories over http
# Example use:
# eget ftp://ftp.altlinux.ru/pub/security/ssl/*
#
6 7 8
# Copyright (C) 2014-2014, 2016  Etersoft
# Copyright (C) 2014 Daniil Mikhailov <danil@etersoft.ru>
# Copyright (C) 2016 Vitaly Lipatov <lav@etersoft.ru>
Vitaly Lipatov's avatar
Vitaly Lipatov committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# 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
24
WGET="wget"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
25

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

31 32 33 34 35 36 37 38 39
# TODO:
# download to this file
WGET_OPTION_TARGET=
if [ "$1" = "-O" ] ; then
    TARGETFILE="$2"
    WGET_OPTION_TARGET="-O $2"
    shift 2
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
40 41
# TODO:
# -P support
Vitaly Lipatov's avatar
Vitaly Lipatov committed
42

43
# If ftp protocol or have no asterisk, just download
Vitaly Lipatov's avatar
Vitaly Lipatov committed
44
# TODO: use has()
Vitaly Lipatov's avatar
Vitaly Lipatov committed
45
if echo "$1" | grep -q "\(^ftp://\|[^*]\)" ; then
46
    $WGET $WGET_OPTION_TARGET "$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
47
    exit
Vitaly Lipatov's avatar
Vitaly Lipatov committed
48 49
fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
50 51 52 53 54 55 56 57 58
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
59 60 61 62
    INDEX=$MYTMPDIR/index
    $WGET $URL -O $INDEX
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
63 64 65 66 67 68 69 70 71 72 73 74 75
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
76 77
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
78 79 80
download_files()
{
    ERROR=0
Vitaly Lipatov's avatar
Vitaly Lipatov committed
81
    for line in $DIRALLFILES/$MASK ; do
Vitaly Lipatov's avatar
Vitaly Lipatov committed
82
        $WGET $URL/$(basename "$line") || ERROR=1
Vitaly Lipatov's avatar
Vitaly Lipatov committed
83
    done
Vitaly Lipatov's avatar
Vitaly Lipatov committed
84
    return $ERROR
Vitaly Lipatov's avatar
Vitaly Lipatov committed
85 86
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
87 88 89 90
get_index || exit
create_fake_files
download_files || echo "There was some download errors" >&2
rm -rf "$MYTMPDIR"