#!/usr/bin/perl

# This script provides revert functionality for Bastille
# Copyright 2000, Jay Beale
# Copyright 2001-2003 Hewlett Packard Corporation

# This script simply runs the revert shell command which was build during the course of the
# Backend run, then removes it.

use Getopt::Long;
use English;
use Cwd;
use File::Path;
use File::Basename;


use lib "/usr/lib";
push (@INC,"/usr/lib/perl5/site_perl/");
push (@INC,"/usr/lib/Bastille");
push (@INC,"/usr/lib/perl5/site_perl/5.8.0/i586-linux-thread-multi");

require Bastille::API;
import Bastille::API;


# Process command-line arguments...
my $nodisclaim = 0;

if( Getopt::Long::GetOptions( "n"     => \$nodisclaim)) {
    $error = 0; # no parse error

} else {
    $error = 1; # parse error
}


&ConfigureForDistro;

if ( $error ) { # GetOptions didn't parse all of the args
    print &getGlobal('ERROR',"usage");
    exit (1);
}

&showDisclaimer($nodisclaim);

my $revert_script=&getGlobal('BFILE', "revert-actions");
if ( -e $revert_script ){


  # protect against simultaneous runs of Bastille
  if (-e &getGlobal('BFILE',"lockfile")){ 
       &ErrorLog ("ERROR:  Another copy of bastille is currently making changes.\n".
                  "        If you are sure yours is the only Bastille process running, \n".
                  "        delete the file\n" . 
                  "           " . &getGlobal('BFILE','lockfile')."\n".
                  "        to reset lock.\n");
       exit 1;
  }else{
       system (&getGlobal('BIN',"touch")." ". &getGlobal('BFILE',"lockfile"));
  }

  print "NOTE:    Reverting system state...\n\n";

  # run B_check_sum for each file in sum.csv
  my @changedFiles;

  &B_read_sums;

  for my $file (sort keys %GLOBAL_SUM) {
    &B_check_sum($file);
    if($GLOBAL_SUM{$file}{flag}) {
	
	if( ! -d &getGlobal('BDIR',"oldconfig")) {
	    mkpath( &getGlobal('BDIR',"oldconfig"),0,0700 );
	}
        if ($file ne &getGlobal('BFILE',"TODO")) {
	    push @changedFiles,$file;
        }

	my $oldconfig = &getGlobal('BDIR',"oldconfig");
	my $dirname = dirname($file);
	my $cp = getGlobal('BIN',"cp");

	if( ! -d $oldconfig . $dirname ){
	    mkpath($oldconfig . $dirname,0,0700);
	}
	if ( system ("$cp $file ${oldconfig}${file}")) {
	    &ErrorLog ("WARNING: Failed to copy $file to ${oldconfig}${directory}.\n");
	}
    }
  }
    
  # removing the list of sums
  my $rm = &getGlobal('BIN','rm');
  my $sumfile=&getGlobal('BFILE',"sum.csv");
  if ( system( "$rm -f $sumfile")) {
    &ErrorLog ("WARNING: Failed to remove $sumfile. \n");  
  }
		  
   my $mv=&getGlobal('BIN','mv');

   # clear out "torevert" file so it doesn't keep on growing
   if ( -e &getGlobal('BFILE',"TOREVERT")) { 
       # This isn't defined on Linux right now, so we'll use this step to 
       # prevent visible errors on this file's nonexistence. 
    
       my $revfile = &getGlobal('BFILE',"TOREVERT"); 
       if ( -e $revfile ) { 
           # This file won't exist if no actions ran that use it. 
           unlink $revfile; 
       } 
   } 

    if (system($revert_script)) {  # run revert script
	&ErrorLog ("ERROR:   Failed to run revert script.\n");
    }
    else {
        # move script to ensure we don't run it again
	if(system("$mv $revert_script ${revert_script}.last")){
	    &ErrorLog ("WARNING: Failed to move revert script out of the way.\n");
	}
	if(system("$rm -f " . &getGlobal('BFILE',"last.config"))) {
	    &ErrorLog("WARNING: Failed to remove " . &getGlobal('BIN',"last.config"));
	}
	    
    }

    # Remove lockfile
    unless (unlink &getGlobal('BFILE',"lockfile")){  
        &ErrorLog ("ERROR:  Unable to delete Bastille lock file: ".
	           &getGlobal('BFILE','lockfile')."\n");
    }


  if ($#changedFiles >= 0) {
    my $changedList = "";

    foreach my $file (@changedFiles) {
	$changedList .="$file\n";
    }
    
    &ErrorLog( "\n###############################################################################\n" .
	       "WARNING: the following files have been changed manually since Bastille was run:\n" .
	       "###############################################################################\n\n" .
	       $changedList . "\n" .	       
	       "###############################################################################\n" .
	       "You will need to merge the resulting changes manually.  The user-modified\n" .
	       "versions of these files can be found in the following directory:\n\n" . 
	       &getGlobal('BDIR',"oldconfig") . "\n\n" .
	       "###############################################################################\n\n");
  }

} else {
    &ErrorLog("NOTE:   System is in original state, no action taken.\n");
    exit 0;
}
