<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w
use strict;

my $VERSION;
$VERSION = sprintf "%d.%02d", q$Revision: 1.31 $ =~ /(\d+)/g;

# install.pl - Perl replacement for install(1)

# See the POD documentation a the end of this file
# or run `perl install.pl --man'
# for more information.

use Getopt::Long qw(:config bundling_override);
use Pod::Usage;

use File::Copy;
use File::Basename;
use File::Compare;
use File::Spec::Functions ':ALL';

sub makedirs;
sub install;
sub message;
sub logaction;
sub preaction;
sub postaction;
sub setmode;
sub setgroup;
sub setowner;

my %opt = (
           help =&gt;0,
           man =&gt; 0,
           version =&gt; 0,
           directory =&gt; 0,
           backup =&gt; 0,
           B =&gt; '.old',
           force =&gt; 0,
           compare =&gt; 0,
           preserve =&gt; 0,
           mode =&gt; undef,
           owner =&gt; undef,
           group =&gt; undef,
           verbose =&gt; 0,
           log =&gt; undef,
           pre =&gt; [],
           post =&gt; [],
);

GetOptions (\%opt,
            'help|?',
	    'man',
	    'version',
            'directory|d',
	    'backup|b',
	    'B=s',
            'force|f',
            'compare|C',
	    'preserve|p',
	    'mode|m=s',
	    'owner|o=s',
	    'group|g=s',
	    'verbose|v',
            'log=s',
	    'pre=s',
	    'post=s',
	   ) or pod2usage(1);
pod2usage(1) if $opt{help};
pod2usage(-exitstatus =&gt; 0, -verbose =&gt; 2) if $opt{man};
print "$VERSION\n" and exit(0) if $opt{version};


my $log;
if (defined $opt{log}) {
    no strict 'refs';
    open($log, "&gt;&gt; $opt{log}") or die "can't open $opt{log}: $!";
};

if (defined $opt{mode}){
  my %likely = (
		777 =&gt; 1, 776 =&gt; 1, 775 =&gt; 1, 774 =&gt; 1, 766 =&gt; 1,
		755 =&gt; 1, 744 =&gt; 1, 700 =&gt; 1,
		666 =&gt; 1, 665 =&gt; 1, 664 =&gt; 1, 655 =&gt; 1, 644 =&gt; 1,
		600 =&gt; 1,
		555 =&gt; 1, 554 =&gt; 1, 544 =&gt; 1, 500 =&gt; 1,
		444 =&gt; 1, 400 =&gt; 1,
	       );

  warn "$opt{mode} seems an unlikely mode"
    unless $likely{sprintf "%d", $opt{mode}};
};

if ($opt{directory}) {
    pod2usage("$0: not enough arguments") unless @ARGV &gt; 0;

    makedirs(@ARGV);
} elsif (@ARGV == 2 &amp;&amp; ! -d $ARGV[1]){
    install $ARGV[0], $ARGV[1];
} else {
    pod2usage("$0: not enough arguments") unless @ARGV &gt; 1;

    my $dest = pop @ARGV;

    pod2usage "$0: `$dest' is not a directory" unless -d $dest;

    for (@ARGV){
      my $base = basename($_);
      my $target = catfile($dest, $base);

      install $_, $target;
    };
};

sub makedirs{
    # print "makedirs called with: @_\n";
    for (@_) {
        if(-d $_){
	  postaction($_);
	  next;
	};

        #print "trying $_\n";
        if (-d dirname($_)) {
            mkdir $_ or die "can't mkdir $_: $!";
            message $_;
	    logaction "mkdir $_";
	    postaction $_;
        }else {
            makedirs(dirname($_), $_);
        };
    };
};

sub install{
  my $source = shift;
  my $target = shift;
  my $base = basename($source);
  my $old;

#  die "$source is not a file" unless -f $source;

  preaction($source);

  if ($opt{compare} and compare($source, $target) == 0){
      logaction "$source and $target are the same";
  } else {
      if ($opt{backup} &amp;&amp; -f $target){
          my $ext = $opt{B};
          if ($ext eq 'numbered'){
              $ext = 1;
              while (-f "$target.$ext"){$ext++};
              $ext = ".$ext";
          };
          $old = "$target$ext";

          move $target =&gt; $old or die "can't move $target to $old: $!";
          message $old;
          logaction "$target =&gt; $old";
      };

      my ($dev, $ino) = (stat $source)[0,1];
      if (-f $target &amp;&amp; $dev == (stat $target)[0] &amp;&amp; $ino == (stat _)[1]){
          die "$source and $target are the same file";
      };

      my $copied = copy $source, $target;

      unless ($copied){
          if ($opt{force} &amp;&amp; -f $target){
              unlink $target or die "can't unlink $target:$!";
              logaction "unlink $target";
              $copied = copy $source, $target;
          };
          if ($opt{backup}) {
              move $old =&gt; $target or die "can't move $old to $target: $!";
              message "restoring $target from $old";
              logaction "$old =&gt; $target";
          }
      };
      die "can't copy $source to $target: $!" unless $copied;

      message $target;
      logaction "$source -&gt; $target";
  };

  # Code cribbed from
  # &lt;http://www.perl.com/language/ppt/src/cp/cp.schumacks&gt;
  if ($opt{preserve}){
    my($mode, $uid, $gid, $atime, $mtime) = (stat $source)[2,4,5,8,9];
    utime $atime, $mtime, $target or die "can't utime $target: $!";
    my $oldmode = (07777 &amp; $mode);
    chmod $oldmode, $target or die "can't chmod $target: $!";
    chown $uid, $gid, $target
      or warn "can't chown $target to $mode: $!";
  };

  postaction($target);
};

sub preaction{
  my $source = shift;
  for my $pre (@{$opt{pre}}){
    system "$pre $source" and die "--pre command `$pre' failed: $!";
    logaction "$pre $source";
  };
};

sub postaction{
  my $target = shift;
  setmode($opt{mode}, $target) if (defined $opt{mode});
  setowner($opt{owner}, $target) if (defined $opt{owner});
  setgroup($opt{group}, $target) if (defined $opt{group});

  for my $post (@{$opt{post}}){
    system "$post $target" and die "--post command `$post' failed: $!";
    logaction "$post $target";
  };
};

sub setmode{
  my $mode = shift;
  $mode = oct($mode);


  for (@_){
    chmod $mode, $_ or die "can't chmod $_ to $mode: $!";
    logaction "chmod $mode, $_";
  };

};

sub setowner{
  my $owner = shift;
  my $uid;

  if ($owner =~ /^\d+$/) {
      ($uid, $owner) = ($owner, getpwuid $owner);
      die "can't getpwuid $uid: $!" unless defined $owner;
  } else {
      $uid = getpwnam $owner;
      die "can't getpwnam $owner: $!" unless defined $uid;
  }

  for (@_){
    chown $uid, -1, $_ or die "can't chown $_ to $owner: $!";
    logaction "chown $uid, -1, $_";
  };

};

sub setgroup{
  my $group = shift;
  my $gid;

  if ($group =~ /^\d+$/) {
      ($gid, $group) = ($group, getgrgid $group);
      die "can't getgrnam $gid: $!" unless defined $group;
  }else {
      $gid = getgrnam $group;
      die "can't getgrnam $group: $!" unless defined $gid;
  }

  for (@_){
    chown -1, $gid, $_ or die "can't chown $_ to $group: $!";
    logaction "chown -1, $gid, $_";
  };

};

sub message{
    print @_, "\n" if $opt{verbose};
}

sub logaction{
    no strict 'refs';
    print $log scalar gmtime, ': ', @_, "\n" if defined $opt{log};
};

__END__

=head1 NAME

install.pl - Perl replacement for install(1)

=head1 SYNOPSIS

=over

=item B&lt;install.pl&gt; [I&lt;options&gt;] I&lt;source&gt; I&lt;target&gt;

=item B&lt;install.pl&gt; [I&lt;options&gt;] I&lt;source&gt;... I&lt;directory&gt;

=item B&lt;install.pl&gt; B&lt;-d&gt; [I&lt;options&gt;] I&lt;directory&gt;...

=back

=head1 DESCRIPTION

Installs the I&lt;source&gt; file to the I&lt;target&gt; filename, each I&lt;source&gt;
file to the destination I&lt;directory&gt;, or (with B&lt;-d&gt;) creates each
I&lt;directory&gt;.  Like install(1), install.pl includes several options
primarily intended for use in makefiles.

=head2 Why use yet another install program?

Features, portability and flexibility.

An install program is really just a glorified cp(1) command with a few
convenience options.  If your install program is missing a feature
that you need, you're better off writing a program or script that
does what you want.  An install program should be ``All things to all
people''.

Once you have an install program that does exactly what you want,
you'll probably want to take it with you to new machines and operating
systems.  Many implementations of install are portable in the sense of
working on a wide variety of machines.  A perl implementation of
install has a subtle advantage of being quickly portable.  No need to
build and install your install program--just copy install.pl to a
machine with a working installation of perl.

Even the most complete install program is missing a feature important
to someone.  Obviously a perl script has a flexibility advantage.  But
install.pl also has pre- and post-installation hooks to provide
flexibility without the need to change any code.  In fact several of
the options provided by traditional install programs can by
implemented through these hooks.  (For efficiency reasons, most are
not, however.)

=head2 The three forms

Traditionally, install programs have three forms: the single file
form, the multiple file form, and the directory form.  If there are
only two arguments and the second is not a directory name, install.pl
assumes the single file form.  The I&lt;source&gt; file is copied to the
I&lt;target&gt; filename.  Use this argument if you want to specify a
different filename then the original.  For instance, the original
might have its version be part of the filename, but the installed copy
should exclude the version portion of the filename.

If the final argument is a directory name, install.pl copies each
listed I&lt;source&gt; file to that I&lt;directory&gt;.  This form is usual for
installing many or if I&lt;target&gt; file will have the same name as the
I&lt;source&gt;.  All actions (such as changing the permissions mode) are
taken on each target file.

The B&lt;-d&gt; option causes install.pl to assume the final, directory
form.  Each argument is created (including all components) if it does
not already exist.  Also all actions are taken on each listed
I&lt;directory&gt;.  Some install programs, such as this one, also perform
each action on the missing components that are created.

=head1 OPTIONS

=over

=item B&lt;-d&gt;, B&lt;--directory&gt;

Create each I&lt;directory&gt;.  It is functionally the same as C&lt;mkdir -p&gt;.

=item B&lt;-b&gt;, B&lt;--backup&gt;

Make a backup of each existing target file.

=item B&lt;-B&gt; I&lt;suffix&gt;

Specify the backup I&lt;suffix&gt;.  The default is C&lt;.old&gt;.  Usually
I&lt;suffix&gt; is a literal value, but the special value C&lt;numbered&gt; causes
the suffix to be a number.  For instance, the first time a file is
backed up, its suffix is C&lt;.1&gt; and the second time it's C&lt;.2&gt; and so
on.

=item B&lt;-f&gt;, B&lt;--force&gt;

Attempt to force each file to be copied even if the target permissions
mode wouldn't normally allow it to be changed (i.e., 0555 and
stricter).  In general, it is better to use the backup option instead
since it allows the original file to be restored.

=item B&lt;-C&gt;, B&lt;--compare&gt;

If a I&lt;target&gt; exists and is the same as the I&lt;source&gt; file, the
actual copy is skipped.  If the B&lt;--backup&gt; option is specified, the
backup is skipped as well.  Other actions, such as changing the mode,
are performed as usual.

=item B&lt;-p&gt;, B&lt;--preserve&gt;

Preserve the file attributes of the I&lt;source&gt; files.  Other options
may ultimately change some target file attributes.

=item B&lt;-m&gt; I&lt;mode&gt;, B&lt;--mode&gt; I&lt;mode&gt;

Set the absolute permission I&lt;mode&gt; of the target files and
directories.  By default the mode is not explicitly set.

=item B&lt;-o&gt; I&lt;owner&gt;, B&lt;--owner&gt; I&lt;owner&gt;

Set the I&lt;owner&gt; of target files and directories.

=item B&lt;-g&gt; I&lt;group&gt;, B&lt;--group&gt; I&lt;group&gt;

Set the I&lt;group&gt; of target files and directories.

=item B&lt;--pre&gt; I&lt;command&gt;

Specify a I&lt;command&gt; to be executed on each I&lt;source&gt; file before it
is installed.  The name of the I&lt;source&gt; file will be provided as an
argument to each I&lt;command&gt;.  This option may be repeated to perform
multiple pre-installation I&lt;command&gt;s.

=item B&lt;--post&gt; I&lt;command&gt;

Specify a I&lt;command&gt; to be executed on each I&lt;target&gt; file or
I&lt;directory&gt; after it is installed.  The name of the I&lt;target&gt; file or
I&lt;directory&gt; will be provided as an argument to each I&lt;command&gt;.  This
option may be repeated to perform multiple post-installation
I&lt;command&gt;s.

=item B&lt;--log&gt; I&lt;logfile&gt;

Append a record of actions to a I&lt;logfile&gt;.  The special value C&lt;-&gt;
sends the output to STDOUT.

=item B&lt;-?&gt;, B&lt;--help&gt;

Prints the B&lt;SYNOPSIS&gt; and B&lt;OPTIONS&gt; sections.

=item B&lt;--man&gt;

Prints the install.pl(1) manual.

=item B&lt;--version&gt;

Prints the current version number of install.pl and exits.

=back

=head1 EXAMPLES

The simplest case copies one or more files into the target
I&lt;directory&gt;.  This example copies src/program1 and src/program2 into
the bin directory:

  install.pl src/program1 src/program2 bin

Creating a backup of the original targets makes it easier to recover
from mistakes (such as installing a buggy program).  On some systems
it can also protect running processes that are using the original
files from crashing or losing data.  (The reason is that the file
descriptor continues to point to the original even when it is
renamed.) This example moves the original targets to bin/program1~ and
bin/program2~:

  install.pl -b -B \~ src/program1 src/program2 bin

Be sure to protect the tilde from its special meaning in shell
commands!

Suppose you wanted to display the source and target file names as
programs are installed.  You could use the B&lt;-v&gt; option or you
can use the B&lt;--pre&gt; and B&lt;--post&gt; options:

  $ install.pl --pre printf src/program1 src/program2 bin \
    --post 'printf " -&gt; %s\n"'
  src/program1 -&gt; bin/program1
  src/program2 -&gt; bin/program2

Note that options may be placed anywhere in the command line and that
multiple pre- and post-installation commands are allowed.  For
instance, this command strips the target files and prints a file
listing:

  install.pl --post strip --post 'ls -l' src/program1 src/program2 bin

A makefile install target generally looks something like:

  INSTALL := ./install.pl -m 775 -b -B .bak

  prefix := /usr/local

  bindir := $(prefix)/bin

  install: $(SCRIPTS) $(PROGRAMS)
	  $(INSTALL) $^ $(bindir)

When `make install' is executed, each script and program will be
copied to /usr/local/bin.  If the target already exists, it will be
backed up to a file ending in `.bak'.  The mode will be set to be user
and group writable, readable and executable, and world readable and
executable.

=head1 TODO

=over

=item *

Add an option to install into a tar file.

=item *

Add an option or create a new script to un-install based on an install
log file.

=item *

Add an option to create a link instead of copying.

=item *

Make the B&lt;-m&gt; option accept several forms of input.
(Such as allowing symbolic modes.)

=item *

Test on a wider variety of operating systems.  Please write to the
author if you'd like to help.

=item *

Make the B&lt;--pre&gt; and B&lt;--post&gt; options accept the file name anywhere
within the command, rather then only at the end.  The xargs(1) B&lt;-i&gt;
option is the model.

=item *

Implement some sort of locking mechanism, most likely using flock, to
allow two or mor install programs to work on the same files.

=back

=head1 BUGS

=over

=item *

install.pl is B&lt;not&gt; yet all things to all people.

=item *

install.pl has too many options which may interact in unexpected ways,
open security holes and confuse people who don't know the syntax.

=item *

The B&lt;-f&gt; option is probably unneeded.

=back

=head1 NOTES

See R. Pike and B.W. Kernighan, Program Design in the UNIX Environment
(Derived from the talk by Rob Pike, ``Cat -v considered harmful'')
I&lt;http://www.cs.bell-labs.com/cm/cs/doc/84/kp.ps.gz&gt; for reasons why
programs like install are at odds with the UNIX Style.  For instance,
the install target in the above example could be written like this:

  install: $(SCRIPTS) $(PROGRAMS)
          -ls $^ | xargs -i mv $(bindir)/`basename {}` \
                               $(bindir)/`basename {}`.bak
          cp $^ $(bindir)
          ls $^ | xargs -i chmod 755 $(bindir)/`basename {}`

The C&lt;-&gt; before the first command is needed since there isn't always a
target file.  But there are other reasons the backup might fail.  So
really there should be something like C&lt;if [ -f $(bindir)/`basename
{}`]&gt; added.  If the copy fails, the backup really should be restored.
Pretty soon you'd have written a shell version of install.pl.

The underlying problem in this case is that UNIX doesn't provide a
reliable way to make backed-up copies.  GNU cp(1) provides a backup
feature, but there are many other programs that probably should make
backed-up copies.  Ideally, the file system should automatically
create backups whenever a file is modified.  I believe this is a
feature of VMS, for instance.

An install program is often deployed in hostile environments.  The
B&lt;-d&gt; option is important on systems where C&lt;mkdir -p&gt; isn't
available.  Some systems don't even have a cp(1) command.  When you're
touring Europe, it's ok to rely on restaurants and markets for food,
but you better bring rations and water if you're trekking through the
desert.

On the other hand, install.pl doesn't go out of its way to make life
harder for people using it in the comforts of a UNIX environment.
Consider this rather tortured output from GNU install(1):

  $ install -vd man
  install: creating directory `man'

  $ install -v install.pl.1 man
  `install.pl.1' -&gt; `man/install.pl.1'

  $ install -vb install.pl.1 man
  `install.pl.1' -&gt; `man/install.pl.1' (backup: `man/install.pl.1~')

Imagine trying to use this in a pipeline!  The far more civilized
output:

  $ install.pl -vd man
  man

  $ install.pl -v install.pl.1 man
  man/install.pl.1

  $ install.pl -vb install.pl.1 man
  man/install.pl.1.old
  man/install.pl.1

=head1 HISTORY

The install utility appeared in 4.2BSD.  Originally it moved files to
their destination.  By at least 4.3BSD Reno install had a B&lt;-c&gt; option
to copy files instead.  (See I&lt;http://www.freebsd.org/cgi/man.cgi&gt; for
a wide collection of Unix man pages.)  Modern install programs no
longer move by default, but the B&lt;-c&gt; option has been retained for
legacy scripts and makefiles.  This version does not support the
vestigial B&lt;-c&gt; at all.

Every install program that I am aware of has a B&lt;-s&gt; option to strip
the binary of debugging information after it has been installed.
install.pl does not offer that option because I believe there is
little reason to strip binaries.  Stripping should be a strictly
private activity.  (If you I&lt;must&gt; strip you binaries, use the
C&lt;--post strip&gt; option.)

  $Log: install.pl,v $
  Revision 1.31  2004/04/07 22:09:16  jericson
  Add a compare option
  Update the documentation

  Revision 1.30  2004/02/27 20:46:06  jericson
  Make -o and -g take numeric user and group IDs.
  Use the %opt variable to hold options.
  Change version option to print 2 decimal places.
  Change version number to 1.30.


=head1 SEE ALSO

install(1), perl(1), cp(1), mv(1), chmod(1), chown(1), mkdir(1),
stat(2), File::Copy, File::Compare, File::Basename, File::Spec,
File::Spec::Functions

=head1 AUTHOR

Jon Ericson I&lt;jericson@cpan.org&gt;

=head1 COPYRIGHT

  Copyright 2004 by Jon Ericson.

  This program is free software; you can redistribute it and/or modify
  it under the same terms as Perl.

=begin CPAN

=head1 README

A Perl replacement for install(1).

=head1 SCRIPT CATEGORIES

UNIX/System_administration
VersionControl/CVS
Win32/Utilities

=end CPAN

=cut

#  LocalWords:  LocalWords Getopt makedirs GetOptions ARGV Kernighan bindir
#  LocalWords:  basename xargs printf chmod mkdir chown exitstatus dest sprintf
#  LocalWords:  atime mtime utime oldmode dirname logfile logaction preaction
#  LocalWords:  postaction setmode setgroup setowner elsif getpwnam getgrnam
#  LocalWords:  VersionControl TODO UIDs GIDs undef getpwuid getgrgid gmtime
#  LocalWords:  catfile STDOUT
</pre></body></html>