#!/usr/bin/env perl

use strict;
use warnings;

use version; our $VERSION = qv('1.0.0');

use utf8;
use open qw(:std :utf8);
use Carp;
use Cwd qw(abs_path getcwd);
use English qw(-no_match_vars);
use File::Basename;
use File::Spec;
use File::Path qw(make_path);
use Getopt::Long;
use Pod::Usage;

use LWP::Simple;
use XML::Twig;

my ( $script_name, $script_folder ) = fileparse( abs_path($PROGRAM_NAME) );
my $current_folder = getcwd();

my ( $help, $man );

my $iheart_xml_url =
  'http://www.iheartradio.com/cc-common/iphone/station_list.xml';

my $pls_template = <<'PLS_END';
[playlist]
NumberOfEntries=1

File1=%s
Title1=%s
Length1=-1
PLS_END

my $m3u_template = <<'M3U_END';
#EXTM3U
#EXTINF:-1,%s
%s
M3U_END


GetOptions(
    'help!'        => \$help,
    'man!'         => \$man,
) or pod2usage( -verbose => 1, -exit => 1 );

# add other options to check for here
if( defined $help ) {
    pod2usage( -verbose => 1, -exit => 0 );
}
if( defined $man ) {
    pod2usage( -verbose => 2, -exit => 0 );
}

my $iheart_xml = get($iheart_xml_url);

if (not defined $iheart_xml) {
    croak $OS_ERROR;
}

my $iheart_data = XML::Twig->new()->parse($iheart_xml);

make_path('m3u/iheartradio/by-name','m3u/iheartradio/by-callletters',
          'pls/iheartradio/by-name','pls/iheartradio/by-callletters');

foreach my $iheart_station ($iheart_data->get_xpath('/station_list/station')) {
    my $station = {};
    foreach my $field (qw[
                        name
                        description
                        stream_url_v2
                        callletters
                        ]) {
        $station->{$field} = $iheart_station->first_child_trimmed_text($field);
    }
    make_pls($station);
}

sub make_pls {
    my $station = shift;
    foreach my $type (qw [m3u pls]) {
        foreach my $id (qw[ name callletters ]) {
            my $path = sprintf('%s/iheartradio/by-%s',
                               $type,
                               $id);
            my $file = sanitize_filename($station->{$id}.'.'.$type);
            open (my $pls_fh, ">", $path.'/'.$file) or warn "Unable to open $file";
            print $pls_fh format_pls($type,$station);
            close($pls_fh);
        }
    }
}


sub format_pls {
    my $type = shift;
    my $station = shift;

    if ($type eq "pls") {
        return sprintf($pls_template,
                       $station->{'stream_url_v2'},
                       $station->{'name'}.' '.
                       $station->{'description'}
                      );
    }
    elsif ($type eq "m3u") {
        return sprintf($m3u_template,
                       $station->{'name'}.' '.
                       $station->{'description'},
                       $station->{'stream_url_v2'}
                       );
    }
}

sub sanitize_filename {
    my $filename = shift;
    $filename =~ s/\\|\/|:|\*|\?|"|<|>|\|/_/g;
    return $filename;
}


exit 0;
__END__
=head1 NAME

 pl

=head1 DESCRIPTION

 This is some placeholder text for my actual description.

=head1 USAGE

 pl Arguments options etc

=head1 ARGUMENTS

 --help  Print this help text

=head1 OPTIONS

 This is some placeholder text for options

=cut
# vim:fdm=marker:syntax=perl:cc=80:fenc=utf-8
