# FileRunner.pm
#
# Copyright (c) 2002 Cunningham & Cunningham, Inc.
# Released under the terms of the GNU General Public License version 2 or later.
#
# Perl translation by Dave W. Smith <dws@postcognitive.com>

package FileRunner;

use strict;
use IO::File;
use Parse;
use Fixture;

sub new {
    my $pkg = shift;

    return bless { input => undef,,
                   tables => undef,
                   output => undef, 
                   fixture => new Fixture(),
                   @_ }, $pkg;
}

sub run {
    my $self = shift;
    my(@argv) = @_;

    $self->argv(@argv);
    $self->process();
    $self->_exit();
}

sub argv {
    my $self = shift;
    my(@argv) = @_;

    die "usage: FileRunner.pl input-file output-file\n"
	unless 2 == @argv;
    my $in = new IO::File($argv[0], "r")
	or die "$argv[0]: $!\n";
    $self->{'input'} = join("", <$in>);
    my $out = new IO::File($argv[1], "w")
	or die "$argv[1]: $!\n";
    $self->{'output'} = $out;
}

sub process {
    my $self = shift;

    eval {
	$self->{'tables'} = new Parse($self->{'input'});
	$self->{'fixture'}->doTables($self->{'tables'});
    };
    if ( $@ ) {
	$self->exception($@);
    }

    $self->{'output'}->print( $self->{'tables'}->asString());
}

sub exception {
    my $self = shift;
    my($exception) = @_;

    # $self->{'tables'} = new Parse("Unable to parse input. Input ignored.", undef);
    # $self->{'fixture'}->exception($self->{'tables'}, $exception);

    print $exception;
    exit(1);
}

sub _exit {
    my($self) = @_;

    $self->{'output'}->close();
    exit(0);
}

1;
