#!/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/>.
#

WGET="wget"

if [ "$1" = "-q" ] ; then
    WGET="wget -q"
    shift
fi

# TODO:
# -P support

# If ftp protocol or have no asterisk, jus download
# TODO: use has()
if echo "$1" | grep -q "\(^ftp://\|[^*]$\)" ; then
    $WGET "$1"
    exit
fi

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)"
    INDEX=$MYTMPDIR/index
    $WGET $URL -O $INDEX
}

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
}

download_files()
{
    ERROR=0
    for line in $DIRALLFILES/$MASK ; do
        $WGET $URL/$(basename "$line") || ERROR=1
    done
    return $ERROR
}

get_index || exit
create_fake_files
download_files || echo "There was some download errors" >&2
rm -rf "$MYTMPDIR"