#!/usr/bin/perl
use strict;
use LWP::UserAgent;
use Compress::Raw::Zlib ;

my $filename = $ARGV[0];
if (!defined($filename) || (length($filename) == 0)) {
	die "usage: eyefi-firmware-fetch <FILENAME>";
}

sub cat
{
        my $filename = shift;;
        my $contents;
        my $line;
        open FILE, "< $filename";
        while ($line = <FILE>) {
                $contents .= $line;
        }
        close FILE;
        return $contents;
}

my $ua = LWP::UserAgent->new;

my $xml;
my $mac;
my $ver = '3.0144'; #'2.0400';
while (1) {
	my $m1 = 0x2d; #rand 0x30;
	my $m2 = rand 256;
	my $m3 = rand 256;
	$mac = sprintf '00-18-56-%02x-%02x-%02x', $m1, $m2, $m3;
	# known good: $mac = '00-18-56-2d-6c-52';
	my $url = sprintf 'http://api.eye.fi/api/rest/eyeserver/v1/getCardFirmware?Card=%s&Version=%s', $mac, $ver;
	print $url."\n";
	my $res = $ua->get($url);
	$xml = $res->content();
	#rint $xml."\n";
	next if $xml =~ /Card not found./;
	next if $xml =~ /File not found./;
	last;
}
$filename .= ".$ver.$mac";
printf STDERR "got %d bytes of xml\n", length($xml);
#strip the XML off:
$xml =~ s/<\?xml.*<Firmware>//s;
printf STDERR "got %d bytes of xml\n", length($xml);
$xml =~ s/<\/Firmware>.*Response>//s;
printf STDERR "got %d bytes of xml\n", length($xml);
my $base64_encoded = $xml;
printf STDERR "got %d bytes of base64 encoded data\n", length($base64_encoded);

use Email::MIME::Encodings;
my $zlib_encoded = Email::MIME::Encodings::decode(base64 => $base64_encoded);

printf STDERR "got %d bytes of zlib encoded data\n", length($zlib_encoded);

my $status;
my $output;

my $i;
($i, $status) = new Compress::Raw::Zlib::Inflate() ;
$status = $i->inflate($zlib_encoded, $output);
$status = $i->inflateSync($zlib_encoded);

open FILE, "> $filename";
print FILE $output;
close FILE;
printf STDERR "done, wrote %d bytes to '$filename'\n", length($output);
