#!/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 -q"

if echo "$1" | grep -q "\(^ftp://\|[^*]$\)" ; then
    $WGET $1 && exit 0
fi
URL=$(echo $1 | grep /$ || dirname $1)
MASK=$(basename $1)
MYTMPDIR="$(mktemp -d)"
DIRALLFILES="$MYTMPDIR/files/"

get_index(){
    INDEX=$MYTMPDIR/index
    $WGET $URL -O $INDEX
}

save_temp_files(){
    mkdir -p $DIRALLFILES
    ALLFILES="$MYTMPDIR/allfiles"
    cat $INDEX | grep -o -E 'href="([^\*/"#]+)"' | cut -d'"' -f2 > $ALLFILES 
    while read line ; do
	touch $DIRALLFILES/$line
    done <$ALLFILES
}

sort_files(){
    for line in $DIRALLFILES/$MASK ; do
	$WGET $URL/`basename "$line"` -P $CURRENTDIR/
    done
}

get_index
save_temp_files
sort_files