use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';

# Command Line Arguments:
my $fileName = shift (@ARGV);
if ($fileName eq "") { $fileName = "C:\\Program Files\\Apache Group\\Apache\\htdocs\\goo.php"; }
my $isHeb = 1;
my $silent = 1;
foreach (@ARGV)
{
if ($_ eq "heb") 
   { $isHeb = 1; }
if ($_ eq "silent")
   { $silent = 1; }
}

# Get the active Word Document:
my $Word = Win32::OLE->GetActiveObject('Word.Application');
if (defined($Word)) { print "Word is there." }
else { print "word is not there."; exit; }
# $Word->{'Visible'} = 1;         # if you want to see what's going on
my $parag;
my ($str, $text, $attributes);
if ($isHeb) { $attributes = "align=right"; };
my $i = 0;
my $doc = $Word->ActiveDocument;
print "Title: ", $doc->{Name}, "\n";
print "total # of paragraphes: ", $doc->{Paragraphs}->{count}, "\n";
for ($i=0; $i < $doc->{Paragraphs}->{count}; $i++)
   {
   	$parag = $doc->{Paragraphs}->Item($i);
   	$text = $parag->{Range}->{Text};
   	chomp($text);
   	$text =~ s/\s*$//g;
   	$text =~ s/^\s*//g;
   	next if ($text eq ""); 
   	my $style = $parag->{Style};
   	if ($isHeb) { 
   	$text =~ s/\"/\\\"/g; 
   	$text =  '<?php $mytext = hebrevc("' . $text . '", 80); echo ($mytext); ?> '; 
   	} ;
   	$str .= tagIt(getHTMLStyle($style), $text, $attributes) . "\n";
   	print "\n $str \n";
   }
open (FILE, ">$fileName") && print "opened file $fileName \n";
print FILE <<END
<html>
	<head>
		<title> $doc->{Name} </title>
END
;
if ($isHeb) {
	print FILE "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=iso-8859-8\">";
	};
print FILE <<END
	</head>
	<body>
	$str
	</body>
</html>

END

;
close(FILE);

system ("http://dotanpc/goo.php");
#############################################################
sub tagIt {
my ($tag, $text, $attributes) = @_;
if ($attributes ne "") {
	return "<$tag $attributes >" . $text . "</$tag>"; }
else {
	return "<$tag>" . $text . "</$tag>"; } 
}

sub getHTMLStyle {
my ($wordStyle) = @_;
my %styleMap = (	
			"Heading 1" => "h1",
			"Heading 2" => "h2",
			"Heading 3" => "h3",
			"Heading 4" => "h4",
			"Heading 5" => "h5",
			"Normal" => "p"
		);
my $styleName = $wordStyle->{NameLocal};
if ($styleMap{$styleName} ne "") 
{
	return $styleMap{$styleName}; 
}
else
{
	return "p";
}
}