#! /usr/bin/perl
#
# TextToManPages.pl <name> <name> ....
# Constructs Praat ManPages from formated text
# Without a name, processes ALL files
#
###############################################################################
#
# Copyright R.J.J.H. van Son © 2000, 2001
#
# Author Rob van Son
# Institute of Phonetic Sciences & ACLC
# University of Amsterdam
# Herengracht 338
# NL-1016CG Amsterdam, The Netherlands
# Email: Rob.van.Son@hum.uva.nl
#        rob.van.son@workmail.com
# WWW  : http://www.fon.hum.uva.nl/rob/
# mail:  Institute of Phonetic Sciences
#        University of Amsterdam
#        Herengracht 338
#        NL-1016CG Amsterdam
#        The Netherlands
#        tel +31 205252183
#        fax +31 205252197
#
# License for use and disclaimers
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
#
#######################################################
#
$HomeDir = '../';
my $SourceTextDirectory = "$HomeDir/LabelProtocol/text";
my $DestinationManPageDirectory = "$HomeDir/LabelProtocol/ManPages";

# Information
my $Author = "Rob van Son";

# Withoud an argument, process ALL files
@ARGV = glob("$SourceTextDirectory/*.txt") unless @ARGV;

while(@ARGV)
{
	# ManPage to open
	my $Title = shift(@ARGV) || return 0;
	if($Title =~ /\/([^\/]+)\.txt\s*$/)
	{
		$Title = $1;
	};

	# Construct Header
	$TextTitle = $Title;
	$TextTitle =~ s/(\b|[^_])_(\b|[^_])/$1 $2/g;
  $TextTitle =~ s/__/_/g;

  # print STDERR "$SourceTextDirectory/$Title.txt -> $DestinationManPageDirectory/$Title.man\n";
  # Open source file
  open(SOURCE, "<$SourceTextDirectory/$Title.txt")
  || die "$SourceTextDirectory/$Title.txt: $!\n";
  my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                             localtime(time);
  $year += 1900;  # Yes, the Milleniumbug is still with us
  ++$mon;         # January is month number 0
  my $creationdate = sprintf("%4d%02d%02d", $year, $mon, $mday);

  # Read text file
  my @AllEntries = ();
  my $CurrentString = "";
  my $CurrentType = "";
  while(<SOURCE>)
  {
  	while(/\<([^\>]+)\>/)
  	{
  		$CurrentString .= $`;
  		my $NewType = $1;
  		$_ = $';
  		# Construct Entry
  		if($CurrentType)
  		{
  			my $ListPtr = [$CurrentType, $CurrentString];
  			push(@AllEntries, $ListPtr);
  		};

  		$CurrentType = $NewType;
  		$CurrentString = "";
  		# Clean up the Current Type
  		$CurrentType =~ s/^\s+//g;
  		$CurrentType =~ s/\s+$//g;
  	};
  	$CurrentString .= $_;
  };
  my $ListPtr = [$CurrentType, $CurrentString];
  push(@AllEntries, $ListPtr);

  # Ready, close input file
  close(SOURCE);

  # Open destination file
  open(DESTINATION, ">$DestinationManPageDirectory/$Title.man")
  || die "$DestinationManPageDirectory/$Title.man: $!\n";

  # Print Header
  print DESTINATION << "EndOfHeader";
ManPagesTextFile
"$TextTitle" "$Author" $creationdate 0

EndOfHeader

  foreach (@AllEntries)
  {
  	my @Entry = @$_;
  	next unless $Entry[0];
  	# Clean up text (remove empty lines etc.)
    $Entry[1] =~ s/(\@\@[^\@]+)\n/$1 /g;  # Make sure links are valid
  	$Entry[1] =~ s/(^\s+)|(\s+$)//g;  # Remove empty lines and space
  	$Entry[1] =~ s/\s\s+/ /g unless $Entry[0] =~ /code/; # Remove double spaces
  	$Entry[1] =~ s/\"/\"\"/g;

  	# Print the actual entry
    print DESTINATION << "EndOfEntry";
<$Entry[0]> "$Entry[1]"

EndOfEntry
  };
};
		
