#!/usr/bin/env perl
#===============================================================================
#
#         FILE: author_title.pl
#
#        USAGE: ./author_title.pl JSON_file YAML_file
#
#  DESCRIPTION: Reads the JSON file generated as a new HPR show is uploaded
#               via the submission form. Converts certain elements to a YAML
#               structure which is written to a file for use by Pandoc when it
#               generates a version of the notes used for local proof-reading.
#
#               This function has been performed by 'jq' in the past, but with
#               mixed success. Also 'yq' has been used experimentally with
#               little success because of the wide variety of the input JSON
#               data. This script has been written to try and achieve parsing,
#               data extraction and YAML generation in a more controllable
#               way.
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: 2024-10-18 Modified the YAML to include the field Pandoc is
#               looking for: 'subtitle', This adds another header to the full
#               notes, which allows another check to be carried out. Also
#               using Pandoc template 'hpr_dev.html' which omits the '<base>'
#               tag.
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.3
#      CREATED: 2023-10-13 22:37:36
#     REVISION: 2024-10-18 16:17:14
#
#===============================================================================

use v5.16;
use strict;
use warnings;
use feature qw{ postderef say signatures state try };
no warnings
    qw{ experimental::postderef experimental::signatures experimental::try };

use JSON;
use YAML::Tiny;
use Data::Dumper;

#
# Version number (Incremented by Vim)
#
our $VERSION = '0.0.3';

#
# Script and directory names
#
( my $PROG = $0 ) =~ s|.*/||mx;
( my $DIR  = $0 ) =~ s|/?[^/]*$||mx;
$DIR = '.' unless $DIR;

#-------------------------------------------------------------------------------
# Declarations
#-------------------------------------------------------------------------------
#
# Constants and other declarations
#
my $basedir = "$ENV{HOME}/HPR/Show_Submission";

#
# Enable Unicode mode
#
binmode STDOUT, ":encoding(UTF-8)";
binmode STDERR, ":encoding(UTF-8)";

#
# Check arguments
#
die "Usage: $PROG JSON_file YAML_file\n" if ( scalar(@ARGV) != 2 );
my $json_file = shift;
my $yaml_file = shift;

die "Unable to find $json_file\n" unless ( -e $json_file );

#
# Read and parse the JSON
#
my $json = JSON->new->utf8;

open( my $jfile, "<:encoding(UTF-8)", $json_file )
    or die "Unable to open $json_file\n";
my $json_text = <$jfile>;
close($jfile);

my $content = decode_json($json_text);

#
# Construct the YAML (is 'subtitle' better?)
#
my $yaml = YAML::Tiny->new(
    {   author   => $content->{host}->{Host_Name},
        title    => $content->{episode}->{Title},
        subtitle => $content->{episode}->{Summary},
        summary  => $content->{episode}->{Summary},
    }
);
#say Dumper(\$yaml);

#
# Output the YAML
#
unless ( $yaml->write($yaml_file) ) {
    warn "Problem writing the YAML to $yaml_file\n";
}

exit;

# vim: syntax=perl:ts=8:sw=4:et:ai:tw=78:fo=tcrqn21:fdm=marker