#!/usr/bin/perl -- # -*- Perl -*-

# A tool for expanding photo URIs in my pseudo-.vcf intermediate format
# Copyright (C) 2007 Norman Walsh
# NO WARRANTY
# Do whatever you like with it
# Version 1.0

use strict;
use MIME::Base64;
use LWP::UserAgent;

my $ua = new LWP::UserAgent;
$ua->agent("nwalsh.com fixvcf/1.0");

while (<>) {
    chop;
    if (/^PHOTO;X-URI:(.*)$/) {
	my $req = new HTTP::Request GET => "$1";
	my $res = $ua->request($req);

	if ($res->code() == 200) {
	    $_ = $res->content();
	    print "PHOTO;BASE64:\n";
	    foreach my $line (split(/\n/, encode_base64($_))) {
		print "  $line\n";
	    }
	} else {
	    warn "Failed to get photo (" . $res->code() . "): $1\n";
	}
    } else {
	print "$_\n";
    }
}

