# $Header: r:/prj/thief2/art/src/obj/RCS/insart.pl 1.2 1998/09/15 14:08:27 DAVET Exp $

# insart.pl (INStall ART to final)


use Cwd;

# build the name of the .e file.  exit if it does not exist

$dot_e = $ARGV[0] . ".e";

if (!(-e $dot_e)) {
    printf "$dot_e does not exist.  This is an error\n";
    exit(-1);
}

# get the current directory and build the directory name to 
# copy the gifs to.

$srcdir=cwd();
# lets lower case it...
$srcdir =~ tr/A-Z/a-z/;
$srcdir =~ s~\/~\\~g; # change foo/bar to foo\bar (Darn Unix people.)

# we are in r:\prj\thief2\art\src\obj, want to copy gifs from 
#           r:\prj\thief2\art\src\obj\txt16
# to
#           r:\prj\thief2\art\finals\obj\txt16

$targetdir=$srcdir;
$targetdir =~ s/src/finals/;


# copy the .bin to finals

$binfile = $ARGV[0] . ".bin";
if (-e $binfile) {
    printf "copying $binfile to $targetdir\n";
    system("copy $binfile $targetdir");
}
else {
    printf "Error: $binfile does not exist! \n";
    exit(-1);
}


# oh yeah, add txt to the path for copies of the gifs to the finals dir
$targetdir = $targetdir . "\\txt16";
if (!(-d $targetdir)) {system("mkdir $targetdir");}

&getmaterials($dot_e);
    


#**********************************************************************
#   subroutines start here! 
#**********************************************************************

sub getmaterials 
{
    local ($filename) = @_;
    local ($mflag)=0;
    open(TEMP,$filename) || die "cannot open $filename\n";
    while (<TEMP>) {
        if ($_ =~ /MATERIALS{/) {
            $mflag=1;  # we are processing materials
            while (<TEMP>) {
                if ($_ =~ /.*\"(.*\....)\".*/) {
                    $t = "TXT16" . "\\" . $1;
                    if (-e $t) {
                       printf "copying $t to $targetdir\n";
                       system("copy $t $targetdir");
                    }
                    else {
                        printf "Error $t does not exist\n";
                    }
                }
                if($_ =~ /}/) {
                   $mflag=0;
                   close(TEMP);
                   return;
                }  # 
        }
        }
    }
    close(TEMP);
}
