home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1040 / install.pl < prev    next >
Perl Script  |  1990-12-28  |  5KB  |  240 lines

  1. #!/usr/local/bin/perl
  2. #    enhanced version of BSD "install" command
  3.  
  4. $ENV{'PATH'} = '/usr/local/bin:/usr/ucb:/bin:/usr/bin:/usr/bsd:/usr/local/etc:/usr/etc:/etc';
  5. $ENV{'SHELL'} = '/bin/sh';
  6. $ENV{'IFS'} = '';
  7. $TRUE  = 1;
  8. $FALSE = 0;
  9. $thispgm = $0;
  10. $thispgm =~ s/.*\///g;
  11. $my_hostname = `hostname`;
  12. chop ($my_hostname);
  13. $do_remove = $do_copy = $do_strip = $do_chown = $do_chgrp = $do_chmod = $do_ls = $be_verbose = $FALSE;
  14.  
  15. #    parse command line
  16. while ($#ARGV >= 0) {
  17.     if ($ARGV[0] !~ /^-/) {
  18.         last;
  19.         }
  20.     elsif ($ARGV[0] eq "-c") {
  21.         $do_copy = $TRUE;
  22.         }
  23.     elsif ($ARGV[0] eq "-g") {
  24.         if ($#ARGV == 0) {
  25.             $errors++;
  26.             printf stderr "%s:  group id missing\n", $thispgm;
  27.             }
  28.         else    {
  29.             shift;
  30.             $new_group = $ARGV[0];
  31.             $do_chgrp = $TRUE;
  32.             }
  33.         }
  34.     elsif ($ARGV[0] eq "-l") {
  35.         $do_ls = $TRUE;
  36.         }
  37.     elsif ($ARGV[0] eq "-m") {
  38.         if ($#ARGV == 0) {
  39.             $errors++;
  40.             printf stderr "%s:  file mode missing\n", $thispgm;
  41.             }
  42.         else    {
  43.             shift;
  44.             $new_mode = $ARGV[0];
  45.             $do_chmod = $TRUE;
  46.             }
  47.         }
  48.     elsif ($ARGV[0] eq "-o") {
  49.         if ($#ARGV == 0) {
  50.             $errors++;
  51.             printf stderr "%s:  owner id missing\n", $thispgm;
  52.             }
  53.         else    {
  54.             shift;
  55.             $new_owner = $ARGV[0];
  56.             $do_chown = $TRUE;
  57.             }
  58.         }
  59.     elsif ($ARGV[0] eq "-r") {
  60.         $do_remove = $TRUE;
  61.         }
  62.     elsif ($ARGV[0] eq "-s") {
  63.         $do_strip = $TRUE;
  64.         }
  65.     elsif ($ARGV[0] eq "-v") {
  66.         $be_verbose = $TRUE;
  67.         }
  68.     else    {
  69.         $errors++;
  70.         printf stderr "%s:  unrecognized command line option \"%s\"\n", $thispgm, $ARGV[0];
  71.         }
  72.     shift;
  73.     }
  74.  
  75. #    parse source file
  76. if ($#ARGV >= 0) {
  77.     $source = $ARGV[0];
  78.     if (! -e $source) {
  79.         $errors++;
  80.         printf stderr "%s:  source file %s doesn't exist\n", $thispgm, $source;
  81.         }
  82.     elsif (! -f $source) {
  83.         $errors++;
  84.         printf stderr "%s:  source file %s is not a regular file\n", $thispgm, $source;
  85.         }
  86.     shift;
  87.     }
  88. else    {
  89.     $errors++;
  90.     printf stderr "%s:  missing source file name\n", $thispgm;
  91.     }
  92.  
  93. @target = ();
  94. while ($#ARGV >= 0) {
  95.     $target = $ARGV[0];
  96.     @t = split (/:/, $ARGV[0]);
  97.     if ($#t == 0) {
  98.         $target = $t[0];
  99.         if (-d $target) {
  100.             $target =~ s/\/$//g;
  101.             $target .= "/" . $source;
  102.             }
  103.         push (@target, $target);
  104.         }
  105.     else    {
  106.         @h = split (/\+/, $t[0]);
  107.         for ($ctr = 0; $ctr <= $#h; $ctr++) {
  108.             if ($h[$ctr] eq $my_hostname) {
  109.                 $target = $t[1];
  110.                 }
  111.             else    {
  112.                 $target = $h[$ctr] . ":" . $t[1];
  113.                 }
  114.             if (-d $target) {
  115.                 $target =~ s/\/$//g;
  116.                 $target .= "/" . $source;
  117.                 }
  118.             push (@target, $target);
  119.             }
  120.         }
  121.     shift;
  122.     }
  123. if ($#target == -1) {
  124.     $errors++;
  125.     printf stderr "%s:  missing target file/directory name(s)\n", $thispgm;
  126.     }
  127.  
  128. if ($errors > 0) {
  129.     printf stderr "\nusage:  %s [-c] [-g group] [-l[ls-opts]] [-m mode] [-o owner] [-r] [-s]\n", $thispgm;
  130.     printf stderr "                [-v] source [host[+host]:]target [target ...]\n\n";
  131.     printf stderr "\t-c  = specifies source is to be copied to target, not moved (don't delete source)\n";
  132.     printf stderr "\t-g  = change target to specified group\n";
  133.     printf stderr "\t-l  = do an \"ll\" on the target after installation,\n";
  134.     printf stderr "\t-m  = change target to specified mode\n";
  135.     printf stderr "\t-o  = change target to specified owner\n";
  136.     printf stderr "\t-r  = remove old target, if any\n";
  137.     printf stderr "\t-s  = strip symbols from target (executables only!)\n";
  138.     printf stderr "\t-v  = be verbose and print all commands before you do them\n";
  139.     exit (1);
  140.     }
  141.  
  142. #    process each target
  143. for ($ctr = 0; $ctr <= $#target; $ctr++ ) {
  144.     @t = split (/:/, $target[$ctr]);
  145.     if ($#t == 1) {
  146.         do remote_install ($t[0], $t[1]);
  147.         }
  148.     else    {
  149.         do local_install ($t[0]);
  150.         }
  151.     }
  152.  
  153. if (! $do_copy) {
  154.     do local_command ("rm", "-f", $source);
  155.     }
  156.  
  157. exit (0);
  158.  
  159. sub local_install {
  160.     $target = $_[0];
  161.  
  162.     if ($do_remove) {
  163.         do local_command ("rm", "-f", $target);
  164.         }
  165.  
  166.     do local_command ("cp", $source, $target);
  167.  
  168.     if ($do_strip) {
  169.         do local_command ("strip", $target);
  170.         }
  171.  
  172.     if ($do_chown) {
  173.         do local_command ("chown", $new_owner, $target);
  174.         }
  175.  
  176.     if ($do_chgrp) {
  177.         do local_command ("chgrp", $new_group, $target);
  178.         }
  179.  
  180.     if ($do_chmod) {
  181.         do local_command ("chmod", $new_mode, $target);
  182.         }
  183.  
  184.     if ($do_ls) {
  185.         do local_command ("ll", $target);
  186.         }
  187.     }
  188.  
  189. sub remote_install {
  190.     $host = $_[0];
  191.     $target = $_[1];
  192.  
  193.     if ($do_remove) {
  194.         do remote_command ($host, "rm", "-f", $target);
  195.         }
  196.  
  197.     do local_command ("rcp", $source, $host . ":" . $target);
  198.  
  199.     if ($do_strip) {
  200.         do remote_command ($host, "strip", $target);
  201.         }
  202.  
  203.     if ($do_chown) {
  204.         do remote_command ($host, "chown", $new_owner, $target);
  205.         }
  206.  
  207.     if ($do_chgrp) {
  208.         do remote_command ($host, "chgrp", $new_group, $target);
  209.         }
  210.  
  211.     if ($do_chmod) {
  212.         do remote_command ($host, "chmod", $new_mode, $target);
  213.         }
  214.  
  215.     if ($do_ls) {
  216.         do remote_command ($host, "ll", $target);
  217.         }
  218.     }
  219.  
  220. sub local_command {
  221.     $cmd = join (" ", @_);
  222.     if ($be_verbose) {
  223.         printf stderr "\t%s\n", $cmd;
  224.         }
  225.     $tstat = (system $cmd) >> 8;
  226.     if ($tstat != 0) {
  227.         printf stderr "%s:  command \"%s\" failed (returned status %d)\n", $thispgm, $cmd, $tstat;
  228.         exit (1);
  229.         }
  230.     }
  231.  
  232. sub remote_command {
  233.     $rmthost = shift (@_);
  234.     $rmtcmd = join (" ", @_);
  235.     if ($be_verbose) {
  236.         printf stderr "\trsh %s -n %s\n", $rmthost, $rmtcmd;
  237.         }
  238.     system "rsh", $rmthost, "-n", $rmtcmd;
  239.     }
  240.