###########################################################################
#
# baseconvert.pm -- provides some fundamental methods for converting files
#                    using external programming 
#
# Copyright (C) 1999 DigiLib Systems Limited, NZ
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################


package baseconvert;

use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa


use convertutil;

sub new {
    my ($class) = shift @_;

    my ($base_dir,$filename,$verbosity,$outhandle) = @_;

    my $self = {};

    $self->{'verbosity'}    = $verbosity;
    $self->{'outhandle'}    = $outhandle;

    # Work out relative filename within 'base_dir'
    my ($file) = ($filename =~ m/^$base_dir(.*?)$/);
    
    $file =~ s/^\/|\\//; # get rid of leading slash from relative filename

    # Setup cached_dir and file_root

    my ($file_root, $dirname, $suffix)
	= &File::Basename::fileparse($file, "\\.[^\\.]+\$");

    my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
    my $base_output_dir = &util::filename_cat($collect_dir,"cached",$dirname);

    if (!-e $base_output_dir ) {
	print $outhandle "Creating directory $base_output_dir\n"
	    if ($verbosity>2);

	&util::mk_all_dir($base_output_dir);
    }

    my $output_dir = &util::filename_cat($base_output_dir,$file_root);

    if (!-e $output_dir) {
	print $outhandle "Creating directory $output_dir\n"
	    if ($verbosity>2);

	&util::mk_dir($output_dir);
    }

    $self->{'cached_dir'} = $output_dir;
    $self->{'file_root'} = $file_root;
    
    return bless $self, $class;
}



sub run_general_cmd
{
    my $self = shift @_;
    my ($command,$options) = @_;

    if (!defined $options->{'verbosity'}) {
	$options->{'verbosity'} = $self->{'verbosity'};
    }

    if (!defined $options->{'outhandle'}) {
	$options->{'outhandle'} = $self->{'outhandle'};
    }

    
    return &convertutil::run_general_cmd(@_);
}


sub regenerate_general_cmd
{
    my $self = shift @_;
    my ($command,$ofilename,$options) = @_;

    if (!defined $options->{'verbosity'}) {
	$options->{'verbosity'} = $self->{'verbosity'};
    }

    if (!defined $options->{'outhandle'}) {
	$options->{'outhandle'} = $self->{'outhandle'};
    }

    return &convertutil::regenerate_general_cmd(@_);
}



sub run_cached_general_cmd
{
    my $self = shift @_;

    my ($command,$ofilename,$options) = @_;

    if (!defined $options->{'verbosity'}) {
	$options->{'verbosity'} = $self->{'verbosity'};
    }

    if (!defined $options->{'outhandle'}) {
	$options->{'outhandle'} = $self->{'outhandle'};
    }

    return &convertutil::run_cached_general_cmd(@_);
}





1;
