#!/usr/bin/perl
use LWP::Simple;

my ($base_url, $base_file, $num_list) = @ARGV;

if (($base_url =~ /\-[h?]/) || (scalar(@ARGV) != 3)) {
    print "$0 <base_url> <base_file> <num_list>\n";
    print "    This routine grabs pages from sequential sets of URLs\n";
    print "    and saves them to disk.\n";
    print "\n";
    print "    <base_url> is the url to grab from.\n";
    print "    <base_file> is the filename to store that which is\n";
    print "        pointed at by <base_url>.\n";
    print "    <num_list> is a list of numbers and number sequences\n";
    print "        to be plugged into <base_url> and <base_file>.\n";
    print "\n";
    print "    <base_url> and <base_file> should each include one printf\n";
    print "    style escape code ('%' followed by options and a type).\n";
    print "    Since only integers will be passed in, probably one of\n";
    print "    the integer types like %d or with modifiers like %03d.\n";
    print "    $0 will iterate over each number represented by\n";
    print "    <num_list> and use that to format <base_url> and\n";
    print "    <base_file>.  <num_list> is a comma separated list of\n";
    print "    integers and integer sequences with no spaces included.\n";
    print "    An integer sequence is two integers separated by a hyphen.\n";
    print "\n";
    print "    Examples:\n";
    print "    Let's say we want to get: \n";
    print "        http://abc.de/blah/do/img02.jpg\n";
    print "        http://abc.de/blah/do/img05.jpg\n";
    print "        http://abc.de/blah/do/img06.jpg\n";
    print "        http://abc.de/blah/do/img07.jpg\n";
    print "        http://abc.de/blah/do/img12.jpg\n";
    print "        http://abc.de/blah/do/img13.jpg\n";
    print "        http://abc.de/blah/do/img14.jpg\n";
    print "        http://abc.de/blah/do/img15.jpg\n";
    print "        http://abc.de/blah/do/img16.jpg\n";
    print "        http://abc.de/blah/do/img20.jpg\n";
    print "    and save them as do_img02.jpg, do_img05.jpg, do_img06.jpg,\n";
    print "    do_img07.jpg, do_img12.jpg, do_img13.jpg, do_img14.jpg,\n";
    print "    do_img15.jpg, do_img16.jpg, and do_img20.jpg.\n";
    print "    We could use the command:\n";
    print "\n";
    print "    $0 http://abc.de/blah/do/img%02d.jpg do_img%02d.jpg 2,5-7,12-16,20\n";
    
}
$| = 1;  # set autoflush

# when I worked in win32, $ worked better on the command line
# on *nix % seems to work better
#$base_url =~ s/\$/\%/g;
#$base_file =~ s/\$/\%/g;

while ($num_list=~/^(\d+)(.*)$/) {
    my ($lo_num, $rest) = ($1, $2);
    my $hi_num = $lo_num;
    if ($rest=~/^\-(\d+)(.*)$/) {
	($hi_num, $rest) = ($1, $2);
    }
    
    for (my $a = $lo_num; $a <= $hi_num; ++$a) {
	my $url = sprintf($base_url, $a);
	my $file = sprintf($base_file, $a);
	
	print "$url...";
	print getstore($url, $file), "\n";
	open URL_MAP, ">>url_map.txt";
	print URL_MAP "$url $file\n";
	close URL_MAP;
    }
    last unless $rest =~ /^\,(\d+.*)$/;
    $num_list = $1;
}
