#!/usr/bin/perl
#
# This script file has been customized for Unix/Linux
#
# This perl script modifies a Visio generated htm file
#  for the FIU-SCS Course syllabi html custom format
#
#  Usage: perl modifyHtml.pl [input_htm_filename]
#
# Author: Prabakar 4/3/08 (revised on 2/22/19)
#

use strict;

my $filename = "png_1.htm";
# print "Usage: perl $0 [input_htm_filename]\n";
if ($#ARGV >= 0) { # in Perl, argcnt is zero for one argument
    $filename = $ARGV[0];
}

my @line = `cat $filename`;
open( FILEOUT, ">$filename" ) || die "cannot open output $filename: $!";

# strings for replacements
my $hrefprefix = " onClick=\"w=window.open('";
my $hrefpostfix = "', 'mywindow','location=0,menubar=0,scrollbars=1,status=0,titlebar=0,width=300,height=300'); w.focus();";

for (my $idx = 0; $idx <= $#line; $idx++) {
  $line[$idx] =~ s/\s+$//; # strip trailing whitespace
  if (($line[$idx] =~ /^\s+<area/i ) && ($line[$idx] =~ / onfocus/i)) {
    $line[$idx] =~ s/\xe2\x80\xa8/ /g; # replaces special UTF characters with a blank
    $line[$idx] =~ s/ onfocus.*$/>/;
    $line[$idx] =~ s/ HREF="/$hrefprefix/;
    $line[$idx] =~ s/" target="_top/$hrefpostfix/;
    $line[$idx] =~ s/ onClick/ href="#" onClick/;
  }
  print FILEOUT @line[$idx]."\n";
}
close(FILEOUT);

exit(0);
