# #######################################################################
# Umenuwriter.pm version 0.5.0: Part of the emdl2umenu.pl program.
#   Writes a system of UMENU .mnu files from a Menutree object

#   Copyright (C) 2002 by Steve Litt (slitt@troubleshooters.com)
#
#   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; version 2 of the License.
#
#   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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Steve Litt, slitt@troubleshooters.com, http://www.troubleshooters.com
# #######################################################################
# #######################################################################
# HISTORY
# 	Version 0.5.0, 3/5/2002
# 		Initial distribution
# #######################################################################
use strict;
use Menutree;

package Writer;
#############################################################################
#############################################################################
#############################################################################
# The Writer object writes output. This particular writer writes a series
# of UMENU .mnu menu definition files.
#############################################################################
sub new($$)
	{
	my($type) = $_[0];
	my($self) = {};
	$self->{'outputfilename'} = $_[1];
	$self->{'menutree'} = $_[2];
#	open(OUF, ">$_[1]") or die "FATAL ERROR: Could not open $_[1] for writing.\n";
	my(@outfile) = ();
	my($temp);
	$self->{'outfile'} = \@outfile;
	bless($self, $type);
	return($self);
	}

#############################################################################
# pushLine() pushes string arg1 onto the single menu output array.
#############################################################################
sub pushLine($) { push(@{$_[0]->{'outfile'}}, $_[1]); }

#############################################################################
# writeOneMenuFile() writes the lines in the array referenced by arg2
# to a file with name arg1. Both the array and the file involve a single
# menu.
#############################################################################
sub writeOneMenuFile($$)
	{
	my($filename) =$_[1];
	my(@lines) = @{$_[2]};
	my($line);
	open(OUF, ">" . $filename) or die "FATAL WRITE ERROR: Could not open $filename for write.\n";
	foreach $line (@lines)
		{
		print OUF "$line\n";
		}
	close(OUF);
	}

#############################################################################
# write() does the Writer object's work by writing all UMENU .mnu files
# with the data in its contained Menutree object. Note that if
# the Menuarray object is not valid, the write doesn't occur.
#############################################################################
sub write()
	{
	my($self) = $_[0];
	unless($self->{'menutree'}->isValid())
		{
		print "WRITER DECLINES TO WRITE because parse or menu errors were encountered!\n";
		return;
		}
	my(@menuKeys) = $self->{'menutree'}->getKeyList();
	@menuKeys = sort(@menuKeys);
	my($menuKey);
	print "WRITING UMENU FILES....................\n";
	foreach $menuKey (@menuKeys)
		{
		$self->{'menutree'}->setCurrentMenuID($menuKey);
		$self->{'menutree'}->resetChoiceSubscript();
		my(@output);
		push(@output,"T_" . $self->{'menutree'}->getTitle());
		push(@output,"#___Menu created by EMDL to UMENU converter");
		push(@output,"#___Filename=" . $self->{'menutree'}->getLetterPath()
			. ".mnu, EMDL key=$menuKey.");
		while($self->{'menutree'}->nextChoice() != 0)
			{
			push(@output,"");
			push(@output,"L_" . $self->{'menutree'}->getChoiceLetter());
			push(@output,"T_". $self->{'menutree'}->getChoiceText());
			if($self->{'menutree'}->isChoiceMenu())
				{
				push(@output,"C_~");
				}
			else
				{
				push(@output,"C_". $self->{'menutree'}->getChoiceCommand());
				}
			my($keyValueRef) = $self->{'menutree'}->nextParamKeyValueRef();
			if(defined($keyValueRef))
				{
				my($key, $value) = @{$keyValueRef};
				}
			else
				{
				}
				
			while($keyValueRef)
				{
				my($key, $value) = @{$keyValueRef};
				push(@output,$key . "_" . $value);
				$keyValueRef = $self->{'menutree'}->nextParamKeyValueRef();
				}
			}

		$self->writeOneMenuFile(lc($self->{'menutree'}->getLetterPath()) . ".mnu", \@output);
		}
	}

return 1;
