#!perl
use strict;
use warnings;
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;

use Template;
use File::Util;
use CGI;
use JSON;

use TCE::Config;

# create the JSON object
my $output = {};

# create the CGI object
my $cgi = CGI->new();

# get the parameters
my $get = $cgi->param('get') || undef;
my $item_id = $cgi->param('itemid') || undef; 

# read the data and transform it into a json object representation
if ((($get eq 'struct') || ($get eq 'pages') || ($get eq 'images')) && (-d $TCE::Config::ITEMS.$item_id))
{
  open FILE, '<'.$TCE::Config::ITEMS.$item_id.'/'.$TCE::Config::DATA_STORAGE{$get};
  my @lines = <FILE>;
  close FILE;
  my $object = join('',@lines);
  my $VAR1;
  eval ($object);
  $output = $VAR1;
  
}

print $cgi->header( -type=>"application/json; charset=UTF-8" );
print to_json($output);

exit(1);