home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / OpenLinux 2.3 CD.iso / live / usr / lib / rpm-2.5.5 / fixsymlinks.pl < prev    next >
Encoding:
Perl Script  |  1999-01-15  |  2.1 KB  |  94 lines

  1. #!/usr/bin/perl -w
  2. # $Id: fixsymlinks.pl,v 1.2 1998/07/29 12:37:33 ray Exp ray $
  3. #use strict;
  4.  
  5. my $C = $0; $C =~ s%.*/%%;
  6.  
  7.  
  8. my $Help = 0;
  9. my $verbose = 0;
  10. my $manGz = "";
  11. my $debug = 0;
  12.  
  13. sub Usage ($$) {
  14.   my( $rv, $msg) = @_;
  15.   print( STDERR $msg . "\n") if ( $msg );
  16.   printf( STDERR "Usage: $C [options] dir ...\n");
  17.   $rv = 0 if ( $Opt{'help'} );
  18.   exit( $rv);
  19. }
  20.  
  21. {
  22.   use Getopt::Long;
  23.   $Getopt::Long::debug = 0;
  24.   $Getopt::Long::ignorecase = 0;
  25.   #$Getopt::Long::pass_through = 1;
  26.   $Getopt::Long::bundling = 1;
  27.   %Opt = ();
  28.  
  29.   Usage(1, "") unless ( GetOptions( \%Opt,
  30.         'help|h', 'verbose|v', 'debug|d',
  31.     'man-gz|m=s') && ! $Opt{'help'} );
  32.  
  33.   $verbose ++ if ( $Opt{'verbose'} );
  34.   ( $debug ++, $verbose ++ ) if ( $Opt{'debug'} );
  35.  
  36.   Usage( 1, "$C: no target(s) specified.") unless ( $#ARGV >= $[ );
  37.  
  38.   if ( $Opt{'man-gz'} ) {
  39.     $manGz = $Opt{'man-gz'};
  40.     Usage( 2, "$C: $manGz: no such directory.\n") if ( ! -d $manGz );
  41.     $manGz =~ s,([*?+.]),\\$1,g;
  42.   }
  43. }
  44.  
  45. my $d;
  46. while ( defined( $d = shift) ) {
  47.  
  48.   ( -d $d ) || die( "$C: $d: no such directory.\n");
  49.   open( SYMLINKS, "find $d -type l -print |" ) ||
  50.      die( "$C: open(SYMLINKS): $!\n");
  51.  
  52.   my $p;
  53.   my $f;
  54.   my $of;
  55.  
  56.   if ( $manGz ) {
  57.     $d = $manGz;
  58.   } else {
  59.     $d =~ s,([*?+.]),\\$1,g;
  60.   }
  61.  
  62.   while ( <SYMLINKS> ) {
  63.     chop( $_);
  64.     $p = $_;
  65.     $f = $of = readlink($p)    || die( "$C: readlink($p): $!\n");
  66.     # remove trailing component (aka filename)
  67.     $p =~ s,/[^/]*$,,;
  68.     $p =~ s,([*?+.]),\\$1,g;
  69.     # strip all directories from $f, if $_ will be in the same dir as $f
  70.     $f =~ s,^$p/,,;
  71.     # strip "DESTDIR" part from $f, making it absolute...
  72.     $f =~ s,^$d,,;
  73.     if ( $manGz ) {
  74.       if ( -l $_ ) {
  75.     printf( "rm %s\n", $_) if ( $verbose );
  76.     (unlink( $_) == 1)    || die( "$C: unlink($_): $!\n");
  77.       }
  78.       $_ .= ".gz";
  79.       $f .= ".gz";
  80.     }
  81.     next if ( $f eq $of );
  82.     if ( -l $_ ) {
  83.       printf( "rm %s\n", $_) if ( $verbose );
  84.       (unlink( $_) == 1)    || die( "$C: unlink($_): $!\n");
  85.     }
  86.     printf( "ln -s %s %s\n", $f, $_) if ( $verbose );
  87.     symlink( $f, $_)    || die( "$C: symlink( $f, $_): $!\n");
  88.   }
  89.  
  90.   close( SYMLINKS);
  91. }
  92.  
  93. exit( 0);
  94.