#!/usr/bin/perl -w

use IO::Socket;
my $sock = new IO::Socket::INET (
				 PeerAddr => $ARGV[0],
				 PeerPort => '80', 
				 Proto => 'tcp',
				 );
die "Could not create socket: $!\n" unless $sock;

print $sock "GET / HTTP/1\.1\r\nHost:$ARGV[0]\r\n\r\n";

@contenu = <$sock>;
close($sock);
chomp(@contenu);
$contenubis=join("",@contenu);
chomp($contenubis);
@img = split(/<img\s+.*\s*src=["']/,$contenubis);
shift @img;

foreach (@img)
{
    ($_) = split(/["']/,$_);
    $_ =~ s/\.\//\//;
}



foreach $img (@img)
{
    print "$img\n";

    $sock = new IO::Socket::INET (
				 PeerAddr => $ARGV[0],
				 PeerPort => '80', 
				 Proto => 'tcp',
				 );
    die "Could not create socket: $!\n" unless $sock;
    print $sock "GET /$img HTTP/1\.1\r\nHost:$ARGV[0]\r\n\r\n";

    @contenu = <$sock>;
    close($sock);
    @chemin = split(/\//,$img);
    $filename = pop(@chemin);
    print "$filename\n";
    open(FIC, ">$filename") || die ">pb ouverture\n";

    $a = shift(@contenu);
    while($a ne "\r\n")
    {
	$a = shift(@contenu);
    }

    $a = shift(@contenu);
    while($a)
    {
	print FIC $a;
	$a = shift(@contenu);
    }
    close(FIC);
}

