#!/usr/bin/perl -wT
use strict;
use warnings;

use XML::LibXSLT;
use XML::LibXML;
use XML::Simple;
use MIME::Base64;

use Bugzilla;

my $cgi = Bugzilla->cgi;
my $xpath = $cgi->param("xpath");
#my $xpath = "//certification-test/device-class[1]/test[1]/run[1]";
my $attach_id = $cgi->param("id");
my $attachment = new Bugzilla::Attachment($attach_id); 
my $results_xml = $attachment->data;
my $results_xml = 'attachment1.xml';
my $xml = XML::LibXML->new();
my $xslt = XML::LibXSLT->new();
my $xslt_path = "attachment.xsl";
my $xslt_path = bz_locations()->{'extensionsdir'} . "/hwcert/css/single.xsl";
my $source = $xml->load_xml(location => $results_xml);
my $style_doc = $xml->load_xml(location => $xslt_path);

my $stylesheet = $xslt->parse_stylesheet($style_doc);
my $results = $stylesheet->transform($source, xpath => $xpath);
#print $stylesheet->output_as_bytes($results);
my $ref2  = XMLin($stylesheet->output_as_bytes($results));
print "Content-Type:application/octet-stream\nContent-Disposition:attachment;filename=". $ref2->{'name'} . "\n\n";
#print $ref2->{'name'};
#print $ref2->{'content'};
print  decode_base64($ref2->{'content'});
