home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / perl / reconfig.pl < prev    next >
Perl Script  |  1992-03-10  |  3KB  |  126 lines

  1. #!/bin/sh  # need to mention perl here to avoid recursion
  2. # NOTE:
  3. #   If you know where perl is and your system groks #!, put its
  4. # pathname at the top to make this a tad faster.
  5. #
  6. # the following magic is from the perl man page
  7. # and should work to get us to run with perl 
  8. # even if invoked as an sh or csh or foosh script.
  9. # notice we don't use full path cause we don't
  10. # know where the user has perl on their system.
  11. #
  12. eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' 
  13. & eval 'exec perl -S $0 $argv:q'
  14.     if $running_under_some_stupid_shell_instead_of_perl;
  15.  
  16. #  Target shell scripts in question:
  17. $COPS_CONFIG="pathconf.pl";
  18.  
  19. #  Potential directories to find commands:
  20. @all_dirs=("/bin",
  21.        "/usr/bin",
  22.        "/usr/ucb",
  23.        "/usr/local/bin",  # scary
  24.        "/usr/bsd");
  25.  
  26. # uncomment next line if you want your own current path used instead
  27. #
  28. # @all_dirs = split(/:/, $ENV{'PATH'});
  29.  
  30. #  Target commands in question, sans those checked above:
  31. @all_commands= ("cc", "awk", "cat",
  32.         "chmod", "cmp", "comm", "cp",
  33.         "date", "diff", "echo", "egrep", "expr",
  34.         "find", "grep", "ls", "mail",
  35.         "mkdir", "mv", "rm", "sed",
  36.         "sh", "sort", "test", "tftp", "touch",
  37.         "uudecode", "uniq", "ypcat");
  38.  
  39. @want{@all_commands} = ();
  40.  
  41. %exceptions=   ('strings', 'chk_strings',
  42.                 'tftp', 'misc.chk',
  43.         'cmp', 'ftp.chk',
  44.                 'uudecode', 'misc.chk');
  45.  
  46. # grab the current values:
  47. open COPS_CONFIG || die "Can't open $COPS_CONFIG: $!\n";
  48.  
  49. $new = "$COPS_CONFIG.$$";
  50. open(NEW_CONFIG, ">$new") || die "Can't open $new: $!\n";
  51.  
  52. while (<COPS_CONFIG>) {
  53.     unless (/\$(\w+)\s*=\s*(['"])(\S*)\2/) {
  54.     print NEW_CONFIG;
  55.     next;
  56.     } 
  57.     ($cap_command, $path) = ($1, $3);
  58.     ($command = $cap_command) =~ tr/A-Z/a-z/;
  59.     unless (($newpath = &getpath($command)) || $command =~ /^yp/) {
  60.     warn "Warning!  no path for $command!\n";
  61.     warn "          $exceptions{$command} will not work as planned!\n"
  62.              if $exceptions{$command};
  63.     $errors++;
  64.     } else {
  65.     delete $want{$command};
  66.     } 
  67.     print "old $path now in $newpath\n" if $newpath ne $path;
  68.     print NEW_CONFIG "\$$cap_command = '$newpath';\n";
  69.  
  70. }
  71.  
  72. for (sort keys %want) {
  73.     delete $want{$_} if $path = &getpath($_);
  74.     tr/a-z/A-Z/;
  75.     print NEW_CONFIG '$', $_, " = '", $path, "';\n";
  76.  
  77. close(COPS_CONFIG) || die "can't close $COPS_CONFIG: $!\n";
  78. close(NEW_CONFIG) || die "can't close $new: $!\n";
  79.  
  80. if (@missing = keys %want) {
  81.      warn "Warning!   missing paths for @missing!\n";
  82.      warn "The shell version may not work right!\n";
  83.  
  84.  
  85. if ($errors) {
  86.     print STDERR "Not all paths were found: write anyway? ";
  87.     # what about removing NEW_CONFIG, $new ??
  88.     exit 1 if <STDIN> !~ /^\s*y/i;
  89.     print STDERR "Ok, but this might not be right...\n";
  90.  
  91. $old = "$COPS_CONFIG.old";
  92.  
  93. rename($COPS_CONFIG, $old)
  94.     || die "can't rename $COPS_CONFIG to $old: $!\n";
  95.  
  96. rename($new, $COPS_CONFIG)
  97.     || die "can't rename $new to $COPS_CONFIG: $!\n";
  98.  
  99.  
  100. open COPS_CONFIG || die "can't re-open $COPS_CONFIG: $!\n";
  101. ($SH_CONF = $COPS_CONFIG) =~ s/\.pl$/.sh/;
  102. open (SH_CONF, ">$SH_CONF") || die "can't create $SH_CONF: $!\n";
  103.  
  104. while (<COPS_CONFIG>) {
  105.     s/^\$//;
  106.     print SH_CONF;
  107. close SH_CONF || die "can't close $SH_CONF: $!\n";
  108.  
  109. exit 0;
  110.  
  111. #############
  112.  
  113. sub getpath {
  114.     local($cmd) = @_;
  115.     local($path);
  116.  
  117.     for (@all_dirs) {
  118.     return $path if -x ($path = "$_/$cmd");
  119.     } 
  120.     '';
  121.