#!/perl/bin

#  reads text file directory.txt and formats into word
#  document
#   ver. 1.1

use Win32::OLE qw(in valof with);
use Win32::OLE::Const('Microsoft Word');

$Win32::OLE::Warn = 2; 	# Send all OLE errors to STDERR

$word = Win32::OLE->new('Word.Application', sub {$_[0]->Quit})
	  or die "Couldn't create new instance of Word App!! - $!";


$outtarg = "C:\\Directory_formatting_script";

$item = "output.txt";

$outputfile = $item;
$working_dir = $outtarg;
$outputfile =~ s/\.txt/\.doc/;
$doc = $word->Documents->add();
$doc->Activate();

$doc->PageSetup->{Orientation} = wdOrientLandscape;
$doc->PageSetup->{TopMargin} = 40;
$doc->PageSetup->{BottomMargin} = 40;
$doc->PageSetup->{LeftMargin} = 40;
$doc->PageSetup->{RightMargin} = 406;



$word->{'Visible'} = 1;



$buffer = "";
open I, "<$item" or die "could not open $item - $!";
while (<I>){
    $buffer .= join " ", "$_";
}
close I;
my $cnt=0;
$Content = $doc->{Content};		# Select entire document
$Content->{Text} = $buffer;
$Content->{Font}->{Name}="Helvetica Narrow";
$Content->{Font}->{Size}=9;

$Content->Collapse({Direction => wdCollapseEnd});

# Set Indexes format
$doc->{Indexes}->{Format} = wdIndexFancy;
$doc->{Indexes}->Add( {Range => $Content, Type => wdIndexIndent,
                       NumberOfColumns => 2});
$doc->Indexes(1)->{Range}->{Font}->{Name}="Helvetica Narrow";
$doc->Indexes(1)->{Range}->{Font}->{Size}=9;
$doc->Indexes(1)->{TabLeader} = wdTabLeaderDots;
$doc->Indexes(1)->{RightAlignPageNumbers} = 1;

$cnt=0;
foreach $para (in $doc->{Paragraphs})
   {
   ++$cnt;
   $xtext = $doc->Paragraphs($cnt)->{Range}->{text};
   $text = $para->{Range}->{text};
   chomp $xtext;
   chomp $text;
   if ($xtext =~ /^\s*$/) { next }
   elsif  ($text =~ /^\s*$/) { next };


   for ($text)
      {


      /^%h1/   and do {&prep; $doc->Paragraphs($cnt)->{Format}->{Style}= "Heading 1"; &center; last};
      /^%h2/   and do {&prep; $doc->Paragraphs($cnt)->{Format}->{Style}= "Heading 2"; &center; last};
      /^%h3/   and do {&prep; $doc->Paragraphs($cnt)->{Format}->{Style}= "Heading 3"; last};
      /^%h4/   and do {&prep; $doc->Paragraphs($cnt)->{Format}->{Style}= "Heading 4"; &center; last};
      /^%h5/   and do {&prep; $doc->Paragraphs($cnt)->{Format}->{Style}= "Heading 5"; };
      /^%h6/   and do {&prep; $doc->Paragraphs($cnt)->{Format}->{Style}= "Heading 6"; last};
      my ($name, $x) = split /\t/, $xtext;
      $name =~ s/\%h5%//g;
      $doc->Indexes->MarkEntry({Range => $doc->Paragraphs($cnt)->{Range},
                             Entry => "$name",EntryAutoText=>"$name",
                              Bold => 0, Italic => 0});
      }
   sub prep
      {
      $text = substr($text,4);
      $para->{Range}->{text} = $text;
    

     
      }

 sub hprep
      {
      $text = substr($text,4);
      #$para->{Range}->{text} = $text;
    

     
      }

   sub center
      {
      $doc->Paragraphs($cnt)->{Alignment} = wdAlignParagraphCenter;
      }

   }


# Show new Index
#$doc->Indexes(1)->Update;
$Content = $doc->{Content};		# Select entire document
$Content->{ParagraphFormat}->{RightIndent} = $word->InchesToPoints(.75);
$Content->{ParagraphFormat}->{SpaceBeforeAuto}=0;
$Content->{ParagraphFormat}->{SpaceAfterAuto}=0;
$tabs = $Content->{ParagraphFormat}->{Tabstops};
$tabs->ClearAll;
$doc->{DefaultTabStop}= $word->InchesToPoints(0.5);
$tabs->Add({Position => $word->InchesToPoints(1.5),
            Alignment => wdAlignTabLeft,
            Leader => wdTabLeaderDots});
#$tabs->Add({Position => $word->InchesToPoints(1.75),
#            Alignment => wdAlignTabLeft,
#            Leader=>wdTabLeaderDots});
$tabs->Add({Position => $word->InchesToPoints(4.5),
            Alignment => wdAlignTabRight,
           Leader =>wdTabLeaderDots});
$doc->{Paragraphs}->Add($doc->Paragraphs(1)->{Range});
#$doc->TablesOfContents->Add({ Range =>$doc->Paragraphs(1)->{Range},
#                              RightAlignPageNumbers =>1,
#                             UseHeadingStyles =>1,
#                             UpperHeadingLevel =>1,
#                            LowerHeadingLevel =>3,
#                           IncludePageNumbers =>1,
#                          AddedStyles =>"",
#                         UseHyperlinks =>1,
#                        HidePageNumbersInWeb =>1});
#$doc->TablesOfContents(1)->{TabLeader} = wdTabLeaderDots;
#$doc->{TablesOfContents}->{Format} = wdIndexIndent;

$doc->SaveAs("$working_dir\\$outputfile");
$doc->Close;


