#!/usr/bin/perl

# This script provides revert functionality for Bastille
# Copyright 2000, Jay Beale
# Copyright 2001,2002 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");

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

#use Bastille::API; # To allow us to get rid of hardcoding -RwF

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

if (( @ARGV ) || ( $error)) { # GetOptions didn't parse all of the args
    &ErrorLog("Error encountered while parsing command line options.");
}

&showDisclaimer($nodisclaim);

# Override revert directory coming in from API
# This never worked
#if ($options{r:}) {
#    $GLOBAL_BDIR{"revert"} = $options{r};
#}

# 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 );
	}
	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 $revert_script=&getGlobal('BFILE', "revert-actions");
my $mv=&getGlobal('BIN','mv');

# clear out "torevert" file so it doesn't keep on growing

if (defined(&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 $file = &getGlobal('BFILE',"TOREVERT"); 
       if ( -e $file ) { 
           # This file won't exist if no actions ran that use it. 
           unlink $file; 
       } 
   } 

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

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

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");
}

