summaryrefslogtreecommitdiff
path: root/perl/contrib/recursiveftpget.pl.in
blob: 7d9cf8eb8142b254c074cc4e8fc8d7dbae313011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!@PERL@
#
# Author:  Daniel Stenberg <Daniel.Stenberg@sth.frontec.se>
# Date:    August 25 1998
# Version: 0.1
#
# This is just meant as an example of why we wrote curl in the first place.
# Quick n' easy scripting use.
#

$dir = $ARGV[0];

$target = $ARGV[1];

$maxdepth = $ARGV[2];

if($dir eq "" || $target eq "") {
    print "Usage: <URL> <dir> [max depth level] \n";
    print " End the URL with a slash if a directory is specified, please\n";
    exit;
}

if(($maxdepth ne "") && ($maxdepth == 0)) {
    # reached maximum depth, die
    print "Reached maximum recursive depth level ($maxdepth), exiting...\n";
    exit;
}

# get dir
@all = `curl -s $dir`;

if($all[0] ne "") {
    print "Got the main $dir dir\n";
}

line:
for(@all) {
    chop; # cut off newline
    @linep= split(" ", $_);

    $name = $linep[$#linep];

    $firstletter=substr($linep[0], 0, 1);

    if($firstletter eq "d") {
        # this is a subdir, recurse
        # if not . or .. of course

        if(($name eq ".") || ($name eq "..")) {
            next line;
        }
        print "Recursing for dir $dir$name in target $target/$name\n";

	$nextdepth=$maxdepth-1;
        print `$0 $dir$name/ $target/$name $nextdepth`;
    }
    elsif($firstletter eq "-") {
        # this is a file, get it
        # oh, make sure the target dir exists first

        if(! -r $target ) {
            mkdir($target,0777);
        }
        print "Getting file $dir$name in target $target/$name\n";
        print `curl -s $dir$name >$target/$name`;
    }
}