#!/usr/bin/perl -w

use strict;

my $htmlfile = "psbe4e_test_ch2.html";

# supplemental window skeleton code
my $supp_win = "<!DOCTYPE HTML>
<html>
<head>
    <meta charset=\"utf-8\" />
    <title>DL_REPLACE_TITLE</title>
    <link href=\"../../../../css/psbe4e_test.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" >
    <link href=\"../../../../css/psbe4e_test_ch2.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" >
</head>
<body id=\"supp_win\">
    DL_REPLACE_HTML



    <script type=\"text/javascript\" src=\"../../../../js/utilities.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/query_types.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/player.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/jquery.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/jquery-ui-1.8.16.custom.min.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/jquery_extensions.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/swfobject.js\"></script>
    <script type=\"text/javascript\" src=\"http://admin.brightcove.com/js/BrightcoveExperiences.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/digfir_ebook_fw.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/psbe4e_test.js\"></script>
    <script type=\"text/javascript\" src=\"../../../../js/psbe4e_test_ch2.js\"></script>
    <script type=\"text/javascript\">

    //<!--
    \$(window).ready(function () {
            player.initialize('52c6e83c757a2ef70c000000');
    });
    //-->
</script>
</body>
</html>";

# read in the HTML file
open my $fh, "<$htmlfile" or die;
my @html_lines = <$fh>;
close $fh;

# now collapse lines array into a single string
my $html_line = "@html_lines";

# show all figure divs

my $figure_cnt = 0;
# note that other div blocks may intervene between opening figure div and final matching </div>
# also note that figures use data-type, not data-block_type
my @figure_divs;

while ($html_line =~ m/<div .*?data-type=\"figure\"/) {
	push @figure_divs, getWholeTagBlock();
	$figure_cnt++;
}

print STDERR "$figure_cnt figures found\n";

foreach my $figure (@figure_divs) {
    # my $supp_win_code;
    my $supp_win_name;
    (my $new_fig_code = $supp_win) =~ s/DL_REPLACE_HTML/$figure/;
    # No, do not use ID
    # if ($new_fig_code =~ m/id="(moore4e_ch[^"]+)"/) {
    if ($new_fig_code =~ m/<span data-type="number">Figure ([\d\.]+)/) {
        my $fignum = $1;
        $supp_win_name = "figure_$fignum.html";
        # Need to give sane title here IF it is displayed
        $new_fig_code =~ s/DL_REPLACE_TITLE/Figure $fignum/;
        $new_fig_code =~ s`src="asset/ch\d+/images/(fig_.*?\.jpg)`src="../../images/large/$1"`;
    } else {
	$supp_win_name = "UNKNOWN_ID.html";
	# probably need to flag and exit here
    }
    ###################################################################################
    # NOTE: Need to change img src, shortening path and using larger version of image #
    ###################################################################################
    
    # create the new file
    open my $fh2, ">$supp_win_name" or die;
    print $fh2 $new_fig_code;
    close $fh2;
}

sub getWholeTagBlock {
    my $tag_block;
    my $indent_level = 0;
    
    # capture opening tag (NOTE: needed to change first + to * in following regex
    $html_line =~ s/(.*?)(<div[^>]* data-type=\"figure\"[^>]+>)//si;
    $tag_block = $2;
    
    # increment indention level counter
    $indent_level++;
    
    # search for the closing </div> based on indention level, gather all HTML in-between
    while ($indent_level > 0) {
        
        (my $next_div) = $html_line =~ s/(.*?(<\/?div[^>]*>))//si;
        # append gathered HTML
        $tag_block .= $1;
        
        if ($2 eq "</div>") {
            $indent_level--;
        } else {
            $indent_level++;
        }
    }

    # return the gather HTML code block
    return $tag_block;
}