#!/usr/bin/perl -w

# (c) Copyright by Tels 1999-2000. All Rights Reserved.
# 2000-05-30 v0.0.1 - retexture a mutlibrush based one mis for usage in another 

use Cowgen::Bunch;  # for a bunch of brushes
use Cowgen::Dromed; # for some Thief constants and specifics
use Cowgen::Texture;
use Cowgen::Gamfile;
use Cowgen::Vbr;
use strict 'vars';

my $VERSION = 0.01;
my $time = time;

print "Retexture v$VERSION Retexture mutlibrush\n";
print "(c) Copyright by Tels 1999-2000. All Rights Reserved.\n\n";
print " Usage:     perl retexture sourcemission destmission multibrush outputfile\n";
print " Examples:  mis11.mis mymis.mis myvbr.vbr new.vbr\n\n";

my $errors = [ "File is no mission (MIS) file.", "File is no multibrush (VBR) file.", ];

# fix commandline parameter
my ($source,$dest,$vbrfile,$output) = @_;

print "Source mission: $source";
my $vbr = Cowgen::Vbr->create($source); # read container
print " loaded. ";
die $errors->[0] if ($vbr->fileType() != MISFILE); # no mission, bail out
my $txt = Cowgen::Texture->create( $vbr->texturePalette() );
my $txthash1 = $txt->asHash();
print "Contains ",$txt->numTexture()," textures.\n";

my $gf = Cowgen::Gamfile->create( $vbr->gamesysName() );

print "Destination mission: $dest";
$vbr = Cowgen::Vbr->create($dest); # read container, overwrite and free first 
print " loaded. ";
die $errors->[0] if ($vbr->fileType() != MISFILE); # no mission, bail out
$txt = Cowgen::Texture->create( $vbr->texturePalette() );
my $txthash2 = $txt->asHash();
print "Contains ",$txt->numTexture()," textures.\n";

print "Multibrush: $vbrfile";
$vbr = Cowgen::Vbr->create($vbrfile); # read container, overwrite and free second
print " loaded.\n";
die $errors->[1] if ($vbr->fileType() != VBRFILE); # no vbr, bail out

# bunch from binary
my $bunch = Cowgen::Bunch->create( "$vbrfile", $vbr, $gf);

# write out 
print "Writing fixed file to $output\n";
$vbr->saveMission($gf,$bunch,$output);

# EOF
1;

END
  {
  my $seconds = time - $time;
  print "\nAll done. Took $seconds seconds.\n";
  }

##########################################################################

