# Install new sprites for DISGRUNT

&main();

sub main
{
    local($action);
    local($dmgraph) =".\\dmgraph.exe";
    local($doomdir);
    local($new_sprites_dir);
    local($ok);
    local($old_sprites_dir);
    local($stuff);

    print "Install or Uninstall DISGRUNT Graphics\n";

    unless ( -x $dmgraph )
    {
        &error("DMGRAPH is missing");
    }

    for ( $ok = 0; !$ok; )
    {
        print "I)nstall, U)ninstall or Q)uit (I/U/Q)? ";
        chop($stuff = <STDIN>);
        ($stuff) =~ /^\s*(\S+)\s*$/;
        if (length($stuff) > 0)
        {
            $stuff = substr($stuff, 0, 1);
            $stuff =~ y/a-z/A-Z/;
            if ( $stuff eq "I" || $stuff eq "U" || $stuff eq "Q" )
            {
                $ok = 1;
            }
        }
    }

    $action = $stuff;

    if ( $action eq "Q" )
    {
        print "Bye!";
        exit;
    }

    for ( $ok = 0; !$ok; )
    {
        print "Where is your DOOM directory? ";
        chop($stuff = <STDIN>);
        ($stuff) =~ /^\s*(\S+)\s*$/;
        if (length($stuff) > 0)
        {
            $stuff =~ y/A-Z/a-z/;
            $stuff =~ y/\//\\/;
            unless ( -d $stuff )
            {
                print "$stuff is not a directory\n";
            }
            else
            {
                unless ( -e "$stuff\\doom.wad" && -x "$stuff\\doom.exe" )
                {
                    print "DOOM.WAD and DOOM.EXE not found in $stuff\n";
                }
                else
                {
                    $ok = 1;
                }
            }
        }
    }

    $doomdir = $stuff;
    $new_sprites_dir = "$doomdir\\disgnews";
    $old_sprites_dir = "$doomdir\\disgolds";

    if ( $action eq "I" )
    {
        print "Install DISGRUNT graphics in $doomdir\n";
        unless ( -d $new_sprites_dir )
        {
            &error("New sprites directory $new_sprites_dir is missing");
        }

        &hit_return();
        &install($doomdir, $new_sprites_dir, $dmgraph);
    }
    else
    {
        print "Uninstall DISGRUNT graphics in $doomdir\n";
        &hit_return();
        &uninstall($doomdir, $old_sprites_dir, $dmgraph);
    }
}    

sub install
{
    local($doomdir, $new_sprites_dir, $dmgraph) = @_;

    local($old_sprites_dir) = "$doomdir\\disgolds";
    local($tmp_sprites_dir) = "$doomdir\\disgtmps";
    local($sprite);
    local($spritename);
    local(@sprites_files);
    local(@sprites);
    local(%imp_sprites) = 
        (
         "trooa1", "sposa1",
         "trooa2a8", "sposa2a8",
         "trooa3a7", "sposa3a7",
         "trooa4a6", "sposa4a6",
         "trooa5", "sposa5",

         "troob1", "sposb1",
         "troob2b8", "sposb2b8",
         "troob3b7", "sposb3b7",
         "troob4b6", "sposb4b6",
         "troob5", "sposb5",

         "trooc1", "sposc1",
         "trooc2c8", "sposc2c8",
         "trooc3c7", "sposc3c7",
         "trooc4c6", "sposc4c6",
         "trooc5","sposc5",

         "trood1", "sposd1",
         "trood2d8", "sposd2d8",
         "trood3d7", "sposd3d7",
         "trood4d6", "sposd4d6",
         "trood5","sposd5",

         "trooe1", "spose1",
         "trooe2e8", "spose2e8",
         "trooe3e7", "spose3e7",
         "trooe4e6", "spose4e6",
         "trooe5","spose5",

         "troof1", "spose1",
         "troof2f8", "spose2e8",
         "troof3f7", "spose3e7",
         "troof4f6", "spose4e6",
         "troof5","spose5",

         "troog1", "sposf1",
         "troog2g8", "sposf2f8",
         "troog3g7", "sposf3f7",
         "troog4g6", "sposf4f6",
         "troog5","sposf5",

         "trooh1", "sposg1",
         "trooh2h8", "sposg2g8",
         "trooh3h7", "sposg3g7",
         "trooh4h6", "sposg4g6",
         "trooh5","sposg5",

         "trooi0", "sposm0",
         "trooj0", "sposn0",
         "trook0", "sposo0",
         "trool0", "sposq0",
         "troom0","sposu0",
         "troon0", "sposm0",
         "trooo0", "sposn0",
         "troop0", "sposo0",
         "trooq0", "sposp0",
         "troor0", "sposq0",
         "troos0", "sposr0",
         "troot0", "sposs0",
         "troou0", "sposu0"
         );

    unless ( -d $old_sprites_dir )
    {
        system("mkdir $old_sprites_dir");
        unless ( -d $old_sprites_dir )
        {
            &error("$old_sprites_dir does not exist, failed to create it");
        }
    }

    unless ( -d $tmp_sprites_dir )
    {
        system("mkdir $tmp_sprites_dir");
        unless ( -d $tmp_sprites_dir )
        {
            &error("$tmp_sprites_dir does not exist, failed to create it");
        }
    }

    unless ( opendir(SPRITES, $new_sprites_dir) )
    {
        &error("Failed to open new sprites directory $new_sprites_dir " .
               "for input");
    }
    @sprites_files = readdir(SPRITES);
    closedir(SPRITES);

    @sprites_files = grep(/\w+\.GIF/i, @sprites_files);
    @sprites = grep(s/\.GIF//i, @sprites_files);

    print "Backing up sprites that will be replaced...\n";

    foreach $sprite (@sprites)
    {
        &backup_sprite($sprite, $old_sprites_dir, $dmgraph);
    }

    foreach $sprite (sort keys %imp_sprites)
    {
        &backup_sprite($sprite, $old_sprites_dir, $dmgraph);
    }

    foreach $sprite (sort keys %imp_sprites)
    {
        &backup_sprite($imp_sprites{$sprite}, $tmp_sprites_dir, $dmgraph);
    }

    print "Replacing sprites in DOOM.WAD...\n";
    foreach $sprite (@sprites)
    {
        &install_sprite($sprite, $new_sprites_dir, $dmgraph);
    }

    foreach $sprite (sort keys %imp_sprites)
    {
        $new_sprite_file = "$new_sprites_dir\\$imp_sprites{$sprite}.gif";
        unless ( -e $new_sprite_file )
        {
            $new_sprite_file = "$tmp_sprites_dir\\$imp_sprites{$sprite}.gif";
        }
        print "Installing $sprite from $new_sprite_file...\n";
        system("$dmgraph $sprite -s $new_sprite_file");
    }

    print "Deleting temporary directory...";
    system("echo y | del $tmp_sprites_dir\\*.* >nul");
    system("rmdir $tmp_sprites_dir");
}

sub uninstall
{
    local($doomdir, $old_sprites_dir, $dmgraph) = @_;

    local(@sprites_files);
    local(@sprites);

    unless ( opendir(SPRITES, $old_sprites_dir) )
    {
        &error("Failed to open old sprites directory $old_sprites_dir " .
               "for input");
    }
    @sprites_files = readdir(SPRITES);
    closedir(SPRITES);

    @sprites_files = grep(/\w+\.GIF/i, @sprites_files);
    @sprites = grep(s/\.GIF//i, @sprites_files);

    print "Restoring sprites backed up by the install procedure...\n";
    foreach $sprite (@sprites)
    {
        &install_sprite($sprite, $old_sprites_dir, $dmgraph);
    }
    print "Your DOOM is good as new (what could go wrong?)\n";
}

sub error
{
    local($msg) = @_;

    print "Error: $msg\n";
    exit;
}

sub backup_sprite
{
    local($sprite, $old_sprites_dir, $dmgraph) = @_;
    local($old_sprite_file);

    $old_sprite_file = "$old_sprites_dir\\$sprite.gif";
    if ( -e $old_sprite_file )
    {
        print "$sprite has already been extracted " . 
            "to $old_sprite_file\n";
    }
    else
    {
        print "Extracting $sprite to $old_sprite_file...\n";
        system("$dmgraph $sprite -e $old_sprite_file");
    }
}

sub install_sprite
{
    local($sprite, $new_sprites_dir, $dmgraph) = @_;
    local($new_sprite_file);

    $new_sprite_file = "$new_sprites_dir\\$sprite.gif";
    print "Installing $sprite from $new_sprite_file...\n";
    system("$dmgraph $sprite -s $new_sprite_file");
}

sub hit_return
{
    local($stuff);

    print "Hit RETURN to continue\n";
    $stuff = <STDIN>;
}
