#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper qw(Dumper);
use Scalar::Util qw(looks_like_number);
use XML::LibXML;

sub _slurp_xml_attrs {
        my $element = shift;
        my $ret = {};
        for my $attr ($element->attributes){
                $ret->{$attr->nodeName} = $attr->value;
        }

        return $ret;
}

# NB: discards all attributes; tvshow.nfo only seems to use them for the uniqueid elements (which contain URLs we
#     don't care about here).
sub _slurp_xml_element {
        my $dom = shift;
        my @children;
        @children = $dom->childNodes();
        if (@children){
                my %return;
                for my $child (@children){
                        my $name = $child->nodeName;
                        my $value = $child->textContent;
                        if (!defined($value) || $value =~ /^\s*$/){
                                next;
                        }
                        $value = _slurp_xml_element($child);
                        if ($name eq '#text'){
                                if ($dom->hasAttributes){
                                        return {
                                                _value => $value,
                                                _attributes => _slurp_xml_attrs($dom),
                                        };
                                } else {
                                        return $value;
                                }
                        } elsif ($name eq '#cdata-section'){
                                if ($dom->hasAttributes){
                                        return {
                                                _value => $value,
                                                _attributes => _slurp_xml_attrs($dom),
                                                _type => 'cdata',
                                        };
                                } else {
                                        return {
                                                _value => $value,
                                                _type => 'cdata',
                                        };
                                }
                        }
                        # my $value = _slurp_xml_element($child);
                        if (exists($return{$name})){
                                if (ref($return{$name}) eq 'ARRAY'){
                                        push(@{$return{$name}}, $value);
                                } else {
                                        $return{$name} = [$return{$name}, $value];
                                }
                        } else {
                                $return{$name} = $value || 'empty';
                        }
                }
                return \%return;
        } else {
                my $content = $dom->textContent;
                $content =~ s/^\s*|\s*$//g;
                return $content;
        }
}
sub _slurp_xml {
        my ($filename) = @_;
        my $xml = "";
        eval {
                my $dom = XML::LibXML->load_xml(location => $filename);
                $xml = _slurp_xml_element($dom->documentElement);
        };
        if ($@){
                return $@;
        }
        return $xml;
}