#!/usr/bin/perl
use LWP::Simple;
$user="lucag73";
$pass="streetsw391";

sub get_rom
{
my $rom=shift(@_);

$urlbase="http://$user:$pass\@www.mame.dk/download/rom";
$page = get "$urlbase/$rom/$rom" or die "URL not found\n";

ITER:
for (split /\n/,$page)
{
$ginfoline = $_;
if (/href/)
 {
  /rom\/$rom/ and last ITER;
 }
}
$ginfoline =~ /href=\"([^\"]*)\"/;
($gameinfourl=$1) =~ s {http://} {http://$user:$pass\@};
print "\n\nURL=$gameinfourl\n";

if (!($gameinfo = get $gameinfourl) )
{
  print "Something is wrong...";
  next;
}

# evita download files gia' presenti
{
GAME:
for(split /\n/, $gameinfo)
{
 $downurl=$_;
 /if your download/ and last GAME;
}
$downurl =~ /href=\"([^\"]*)\"/;
$downloadurl=$1;
print "Real url: $downloadurl\n";
getstore $downloadurl, "$rom.zip";
}
}

### MAIN
while(<>)
{
 if ( m =/gameinfo/([^/]*)/=gc )
{
 print("Game: $1\n");
if (-s "$1.zip")
 { print "Game already downloaded ... \n"; }
else
 {
   print "downloading ... \n";
   get_rom($1);
 }
}
}
