#!/bin/bash

url=`echo $1 | sed 's/ /%20/g'`
uri=`echo $url | sed 's|http://[^/]\+||'`
server=`echo $url | sed 's|\(http://[^/]\+\).*|\1|'`
name=${uri##*/}

if [ "x$name" == "x" ] ; then

    # directory case; fetch it, and parse it recursively

    echo "Entering $url..."
    mkdir -p ${uri#/*}
    curl -s $url > ${uri#/*}list.txt

    grep "href.*$uri" ${uri#/*}list.txt | cut -f2 -d\" | while read f ; do
        $0 ${server}$f
    done

    rm -f ${uri#/*}list.txt

else

    # normal file case

    echo "Downloading $url..."
    out=`echo ${uri#/*} | sed 's/%20/ /g'`
    mkdir -p `dirname "$out"`
    curl -s -b "url_ok=\"$uri\"; url=\"$uri\"" -e http://standards.iso.org/ittf/licence.html $url > "$out"

fi
