[EMAIL PROTECTED] writes:

> I've included the bare script in line at the

OOps maybe not..

===========================
./updetc.pl

#!/usr/bin/perl 

## find /etc -iname '._cfg????_*'
use strict;
use warnings;
use File::Find;

my $myscript;
($myscript = $0) =~ s/^.*\///;
# Variable to create directory where  needed
my $STST = 'START_STOP';


## produces something like: 091207_101630
## Which means: Aug 12 2007 10:16:30
my ($mon,$mday,$year,$hour,$min,$sec) = (localtime(time))[4,3,5,2,1,0];
#    $year += 1900; ## prints 2005 
    $year -= 100;  ## prints 05 (with %02d)
    $mon  += 1;
my  $PaddedDateStr = sprintf "%02d%02d%02d%s%02d%02d%02d", 
$mon,$mday,$year,"_",$hour,$min,$sec;

if([EMAIL PROTECTED]){
  usage();
   print "Usage tripped at " . __LINE__ . "\n";
  exit;
}
my $dir;
my $switch = shift;
$dir  = shift || die "We need two cmdline arguments: $!";

if ( ! -d  $dir){
   usage();
   print "No directory <$dir> can be found\n";
   print "Usage tripped at " . __LINE__ . "\n";
}

find(\&wanted, $dir);
my @ar;
sub wanted {
  if(/^\._cfg0000/){
     push @ar,$File::Find::name;
   }
}
if([EMAIL PROTECTED]){
  print "
  No temporary ._cfg0000_*files were found... exiting\n";
  exit;
}
if($switch =~ /\-s\s*$/){
  ## we are in `show' mode
    for(@ar){
        print "$_\n";
    }
    exit;
}

if($switch =~ /\-c\s*$/){
   ## cp  new versions over old
       ## get our filename for echoing and its root
    my $newconf1      =  $ar[0];
   (my $fs_conf       =  $newconf1) =~ s/\._cfg0+_//;

    print 
"  ====* Replacing old conf versions  with new,, backup both *====
   We find the new ._cfg000 files and offer a chance to view diffs
   followed by choosing to use or not.  But either way they are 
   stored for future reference or use.

   If you decide to use the conf files we OVERWRITE the old ones but 
   keep a dated copy in a special directory <START_STOP> made for that
   purpose, wherever needed in the filesystem. 
   Like:       
     copy <$newconf1>  
   OVERWRITING
     <$fs_conf>
   (NOTE: We will save renamed copies of both the one on the fs and the 
   new version. New versions are also backed up to that special directory 
   <START_STOP/> created throughout filesystem as needed.)

   If you want more information before proceeding..
   Press Ctrl-c now and type:
     \`$myscript  help' to learn the names to search for and 
   the detailed proceedure
"; 
   print "Press enter to continue or select the Ctrl-c option\n";
   <STDIN>;
    print 
"=====     *     =====     *     =====     *     =====     *     =====\n";
 my $ans;
    for(@ar){
   ## Setup some more file name renditions we are likely to need
    my $newconf       = $_;
   ## Reassing fs_conf so old value doesn't pop up unwanted from above
   ($fs_conf          =  $newconf) =~ s/\._cfg0+_//;
   (my $prepended     =  $newconf) =~ s/(^.*)(\._cfg0+_.*$)/$2/;
   (my $conf_path     =  $newconf) =~ s/\/\._cfg0+.*$//;
   (my $fs_conf_short =  $fs_conf)  =~ s/^.*\///;
    my $stt_conf      = "START-USE" . $prepended;
    my $stp_conf      = "STOP-USE_" . $fs_conf_short;
    my $nouse_conf    = "NOUSE".$newconf;

     ## Test to see if me have a matching conf already on
     ## the fs in use.
     if ( ! -e $fs_conf){
        ## None present.. so we offer to install AND save it in STST            
 
          print "   
        <$newconf> 
   has no counterpart on the fs..  Shall we install it like this:
 cp <$newconf>  (over writing!) 
      <$fs_conf>
   and 
 mv <$newconf
      <$conf_path/$STST/$stt_conf-$PaddedDateStr>
 ";
    print "
   Any thing but <y> will rename the newconf to:
     $conf_path/$STST/NOUSE$prepended-$PaddedDateStr
   
 ";
    print "     [y/n] > ";
    $ans = <STDIN>;
    ## Make sure we have the $STST directory
    if (! -d "$conf_path/$STST") {
        print "Creating directory $conf_path/$STST\n";
        mkdir "$conf_path/$STST" ||
               die "Cannot mkdir $conf_path/$STST: $!"; 
     }
      if ($ans !~ /\by\b/) {
         print "
   =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
   Moving 
     $newconf 
   to 
     $conf_path/$STST/NOUSE$prepended
";
        rename $newconf, "$conf_path/$STST/NOUSE".$prepended ."-". 
$PaddedDateStr || die 
              "Can't rename $newconf to $conf_path: $!";           
        print "=-=-=-=-=-=-=-=-=-=-= Rename succeded 
-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
        next;
       }else {

         print "   
   =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
   Ok, you've decided to start using this conf file.
   cp $newconf  (installing it)
        $fs_conf 
  and moving
    $newconf to
       $conf_path/$STST/$stt_conf-$PaddedDateStr
";
        ## open the START-USE file
        open(START,">$conf_path/$STST/$stt_conf-$PaddedDateStr")  
            or die "Can't open <$conf_path/$STST/$stp_conf-$PaddedDateStr>: 
$!";      
        ## Write our size tested slurped data to that file Now theoretically
        ## we have solid copy of the working conf file written to the STST 
directory
        print START slurp($fs_conf)  . "\n"; 
        close(START);
        
        ## now we rename $newconf over $fs_conf
        rename $newconf, $fs_conf  or die 
                "Can't rename $newconf to $fs_conf: $!";
       print "   =-=-=-=-=-=-=-=-=-=-= Rename Suceeded 
-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
          next; 
      }   
     }else {
       ## we handle the files that do match their counterparts
       print "   Incoming <$newconf>
   Shall we install it as described; overwriting the existing one?
 ";
    print "
   Anything but <y> will rename the new conf listed above to 
 $conf_path/$STST/NOUSE$prepended-$PaddedDateStr 
   for reference
 ";
    print "
  **Take a look at the diffs before deciding if you need to**
diff $newconf $fs_conf

";
    print "     [y/n] > ";
    $ans = '';
    $ans = <STDIN>;
    ## Make sure we have the $STST directory
    if (! -d "$conf_path/$STST") {
        print "Creating directory $conf_path/$STST\n";
        mkdir "$conf_path/$STST" ||
               die "Cannot mkdir $conf_path/$STST: $!"; 
     }
       if ($ans !~ /\by\b/) {     
        print "
   =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
   Moving 
     <$newconf> 
   to 
     <$conf_path/$STST/NOUSE$prepended-$PaddedDateStr>
";
        rename $newconf, "$conf_path/$STST/NOUSE".$prepended."-".$PaddedDateStr 
|| die 
                       "Can't rename <$newconf> to <$conf_path>h: $!";  
        print"
   =-=-=-=-=-=-=-=-=-=-= Rename Succeeded -=-=-=-=-=-=-=-=-=-=-=-=-=-\n";       
  
        next;
       }else {
          print "
   =-=-=-=-=-=-=-=-=-=-= Operations Underway -=-=-=-=-=-=-=-=-=-=-=-=-=-
   Copy the working file to
     <$conf_path/$STST/$stp_conf-$PaddedDateStr>
   copy the newconf
     <$newconf> 
   to 
     <$conf_path/$STST/$stt_conf-$PaddedDateStr>
   And finally rename 
   <$newconf> 
  to 
   <$fs_conf> 
";
 
        ## open the STOP-USE file
        open(STOP,">$conf_path/$STST/$stp_conf-$PaddedDateStr")  
            or die "Can't open <$conf_path/$STST/$stp_conf-$PaddedDateStr>: 
$!";      
        ## Write our size tested slurped data to that file Now theoretically
        ## we have solid copy of the working conf file written to the STST 
directory
        print STOP slurp($fs_conf)  . "\n"; 
        close(STOP);
 
        ## open the START-USE file
        open(START,">$conf_path/$STST/$stt_conf-$PaddedDateStr")  
            or die "Can't open <$$conf_path/$STST/$stt_conf-$PaddedDateStr>: 
$!";      
        ## Write our size tested slurped data to that file Now theoretically
        ## we have solid copy of the working conf file written to the STST 
directory
        print START slurp($newconf) || die
        "reprint of $newconf to <$conf_path/$STST/$stt_conf-$PaddedDateStr> 
Failed: $!"; 
        close(START);
        
        ## now we rename $newconf over $fs_conf
        rename $newconf, $fs_conf  or die "Can't rename $newconf to $fs_conf: 
$!";
   print "   =-=-=-=-=-=-=-=-=-=-= Rename  Succeeded 
-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
     }
    }
  }   
}

## This routine avoids having to use another module like
## File::Copy to get single file copies preformed.
sub slurp {
  ## Compare reported size to read size 
  ## (will find funky file endings like from dos files)
  ## amongst other things.
  my $file = $_;
  open(my $fh,'<:raw', $file)
    or die "Cannot open '$file' $!";
  my $size = -s $fh;
  $size == read $fh, my $data, $size
    or die "Cannot \`read' <$file> $!";
     return  $data ;
}

sub usage {
     print "
  Purpose: Find files that need updating after emerge update operations  
           on gentoo linux
    Usage: \`$myscript -[cs] DIRECTORY'
    flags: -c carry out the copy move operations in DIRECTORY <recursive>
           -s show the existing ._cfg0000* files in DIRECTORY <recursive>

  Notes: \`$myscript' will give you a chance to bail in several places
         before any changes are made.  It will keep you posted as to what
         it will do prior to doing it.

         You will also get a ready made diff command to check out what
         has changed if you want to use it. (requires another terminal
         running to past the ready made `diff' command in) ";
}

-- 
[EMAIL PROTECTED] mailing list

Reply via email to