# !/usr/bin/perl -w

# 2000-03-18 0.0.1 first version
# 2000-03-26 0.0.2 writing out uses diff gamesys

$VERSION = '0.02';
$| = 1; # disable out buffer

use Cowgen::Dromed;    # for some Thief constants and specifics
use Cowgen::Vbr;
use Cowgen::Bunch;
$gamsysname = { THIEF() => 'Dark.gam', THIEF2() => 'Thief2.gam', SSHOCK2() => 'Shock2.gam', };

print "Convert v$VERSION (c) Copyright by Tels 1999-2000. All Rights Reserved.\n\n";
print "Convert between different MIS/VBR/GAM/SAV file versions.\n\n";
print "Usage: perl convert.pl infile outfile [version]\n";
print " where version is one of 0 (Thief/Thief Gold), 1 (SShock 2), or 2 (Thief 2).\n\n";

# get commandline parameter
$infile = shift || "in.mis"; 
$outfile = shift || "out.mis"; 
$type = shift || 2; 
if (($type < 0) or ($type > 2))
  {
  print "Error: Type must be in 0...2!. Exiting.\n"; exit;
  }

print "Reading '$infile' ...";
$vbr = Cowgen::Vbr->create($infile); # make object from template mission
print "\n Input is a ",filetype2Name($vbr->fileType())," file from ",
      version2Name($vbr->version());
if ($vbr->gamesysName() ne "") 
  {
  print " and uses '",$vbr->gamesysName(),"'.\n";
  $gf = Cowgen::Gamfile->create($vbr->gamesysName());
  }
else
  {
  print ".\n";
  }

print "Converting to ",version2Name($type)," by using $gamsysname->{$type}...\n";
$vbr->version($type);

my $b = Cowgen::Bunch->create ( "$outfile", $vbr, $gf ); undef $gf; # free memory
$gf2 = Cowgen::Gamfile->create($gamsysname->{$type});
$b->stuff2vbr ($vbr,$gf2); undef $gf2; 

# add module gen for Thief II, doubles will be automatically removed
$vbr->scriptModules ( $vbr->scriptModules (), 'gen' ) if ($type == 2);

print "Writing to '$outfile' ...";
$vbr->writeFile($outfile);

print " done, enjoy '$outfile'!\n";

# EOF
1;

