#!/bin/bash
###############################################################################
# wgetmirror.bash
# Chris Cera
# Written: 4/8/2001
###############################################################################
# 'cd's into a directory and wgets everything at specified depth
###############################################################################
# This worked for moss results on the rpn test:
# wgetmirror http://moss.cs.berkeley.edu/~moss/results/48644267 . 2
###############################################################################

wget="/usr/bin/wget"

# check if not equal to 3 and 5
if [ "$#" -ne 3 ] && [ "$#" -ne 5 ]; then
    echo "Usage: $0 downloadFrom downloadTo maxDepth [user] [pass]"
    exit 1

# do this step whether 3 or 5 args
else

    # get the site, downloadFrom (page or directory)
    site=$1

    # get the downloadTo directory
    dir=$2

    # make sure dir downloadTo exists
    if [ ! -d $dir ]; then
        echo "$dir does not exist, please create, then start $0 again"
        exit 1
    fi

    # then get the depth
    depth=$3

    if [ $# -eq 5 ]; then
        user=($4)
        pass=($5)
        strAuth="--http-user=$user --http-passwd=$pass"
    fi

    web_suck="$wget --mirror --quiet -nH --convert-links -l$depth $strAuth"

    echo $web_suck $site
    $web_suck $site
fi
