Home | History | Annotate | Line # | Download | only in dist
install-sh revision 1.1.1.1.2.2
      1  1.1.1.1.2.2  snj #!/bin/sh
      2  1.1.1.1.2.2  snj # install - install a program, script, or datafile
      3  1.1.1.1.2.2  snj 
      4  1.1.1.1.2.2  snj scriptversion=2005-05-14.22
      5  1.1.1.1.2.2  snj 
      6  1.1.1.1.2.2  snj # This originates from X11R5 (mit/util/scripts/install.sh), which was
      7  1.1.1.1.2.2  snj # later released in X11R6 (xc/config/util/install.sh) with the
      8  1.1.1.1.2.2  snj # following copyright and license.
      9  1.1.1.1.2.2  snj #
     10  1.1.1.1.2.2  snj # Copyright (C) 1994 X Consortium
     11  1.1.1.1.2.2  snj #
     12  1.1.1.1.2.2  snj # Permission is hereby granted, free of charge, to any person obtaining a copy
     13  1.1.1.1.2.2  snj # of this software and associated documentation files (the "Software"), to
     14  1.1.1.1.2.2  snj # deal in the Software without restriction, including without limitation the
     15  1.1.1.1.2.2  snj # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     16  1.1.1.1.2.2  snj # sell copies of the Software, and to permit persons to whom the Software is
     17  1.1.1.1.2.2  snj # furnished to do so, subject to the following conditions:
     18  1.1.1.1.2.2  snj #
     19  1.1.1.1.2.2  snj # The above copyright notice and this permission notice shall be included in
     20  1.1.1.1.2.2  snj # all copies or substantial portions of the Software.
     21  1.1.1.1.2.2  snj #
     22  1.1.1.1.2.2  snj # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23  1.1.1.1.2.2  snj # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24  1.1.1.1.2.2  snj # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     25  1.1.1.1.2.2  snj # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     26  1.1.1.1.2.2  snj # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
     27  1.1.1.1.2.2  snj # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     28  1.1.1.1.2.2  snj #
     29  1.1.1.1.2.2  snj # Except as contained in this notice, the name of the X Consortium shall not
     30  1.1.1.1.2.2  snj # be used in advertising or otherwise to promote the sale, use or other deal-
     31  1.1.1.1.2.2  snj # ings in this Software without prior written authorization from the X Consor-
     32  1.1.1.1.2.2  snj # tium.
     33  1.1.1.1.2.2  snj #
     34  1.1.1.1.2.2  snj #
     35  1.1.1.1.2.2  snj # FSF changes to this file are in the public domain.
     36  1.1.1.1.2.2  snj #
     37  1.1.1.1.2.2  snj # Calling this script install-sh is preferred over install.sh, to prevent
     38  1.1.1.1.2.2  snj # `make' implicit rules from creating a file called install from it
     39  1.1.1.1.2.2  snj # when there is no Makefile.
     40  1.1.1.1.2.2  snj #
     41  1.1.1.1.2.2  snj # This script is compatible with the BSD install script, but was written
     42  1.1.1.1.2.2  snj # from scratch.  It can only install one file at a time, a restriction
     43  1.1.1.1.2.2  snj # shared with many OS's install programs.
     44  1.1.1.1.2.2  snj 
     45  1.1.1.1.2.2  snj # set DOITPROG to echo to test this script
     46  1.1.1.1.2.2  snj 
     47  1.1.1.1.2.2  snj # Don't use :- since 4.3BSD and earlier shells don't like it.
     48  1.1.1.1.2.2  snj doit="${DOITPROG-}"
     49  1.1.1.1.2.2  snj 
     50  1.1.1.1.2.2  snj # put in absolute paths if you don't have them in your path; or use env. vars.
     51  1.1.1.1.2.2  snj 
     52  1.1.1.1.2.2  snj mvprog="${MVPROG-mv}"
     53  1.1.1.1.2.2  snj cpprog="${CPPROG-cp}"
     54  1.1.1.1.2.2  snj chmodprog="${CHMODPROG-chmod}"
     55  1.1.1.1.2.2  snj chownprog="${CHOWNPROG-chown}"
     56  1.1.1.1.2.2  snj chgrpprog="${CHGRPPROG-chgrp}"
     57  1.1.1.1.2.2  snj stripprog="${STRIPPROG-strip}"
     58  1.1.1.1.2.2  snj rmprog="${RMPROG-rm}"
     59  1.1.1.1.2.2  snj mkdirprog="${MKDIRPROG-mkdir}"
     60  1.1.1.1.2.2  snj 
     61  1.1.1.1.2.2  snj chmodcmd="$chmodprog 0755"
     62  1.1.1.1.2.2  snj chowncmd=
     63  1.1.1.1.2.2  snj chgrpcmd=
     64  1.1.1.1.2.2  snj stripcmd=
     65  1.1.1.1.2.2  snj rmcmd="$rmprog -f"
     66  1.1.1.1.2.2  snj mvcmd="$mvprog"
     67  1.1.1.1.2.2  snj src=
     68  1.1.1.1.2.2  snj dst=
     69  1.1.1.1.2.2  snj dir_arg=
     70  1.1.1.1.2.2  snj dstarg=
     71  1.1.1.1.2.2  snj no_target_directory=
     72  1.1.1.1.2.2  snj 
     73  1.1.1.1.2.2  snj usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
     74  1.1.1.1.2.2  snj    or: $0 [OPTION]... SRCFILES... DIRECTORY
     75  1.1.1.1.2.2  snj    or: $0 [OPTION]... -t DIRECTORY SRCFILES...
     76  1.1.1.1.2.2  snj    or: $0 [OPTION]... -d DIRECTORIES...
     77  1.1.1.1.2.2  snj 
     78  1.1.1.1.2.2  snj In the 1st form, copy SRCFILE to DSTFILE.
     79  1.1.1.1.2.2  snj In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
     80  1.1.1.1.2.2  snj In the 4th, create DIRECTORIES.
     81  1.1.1.1.2.2  snj 
     82  1.1.1.1.2.2  snj Options:
     83  1.1.1.1.2.2  snj -c         (ignored)
     84  1.1.1.1.2.2  snj -d         create directories instead of installing files.
     85  1.1.1.1.2.2  snj -g GROUP   $chgrpprog installed files to GROUP.
     86  1.1.1.1.2.2  snj -m MODE    $chmodprog installed files to MODE.
     87  1.1.1.1.2.2  snj -o USER    $chownprog installed files to USER.
     88  1.1.1.1.2.2  snj -s         $stripprog installed files.
     89  1.1.1.1.2.2  snj -t DIRECTORY  install into DIRECTORY.
     90  1.1.1.1.2.2  snj -T         report an error if DSTFILE is a directory.
     91  1.1.1.1.2.2  snj --help     display this help and exit.
     92  1.1.1.1.2.2  snj --version  display version info and exit.
     93  1.1.1.1.2.2  snj 
     94  1.1.1.1.2.2  snj Environment variables override the default commands:
     95  1.1.1.1.2.2  snj   CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
     96  1.1.1.1.2.2  snj "
     97  1.1.1.1.2.2  snj 
     98  1.1.1.1.2.2  snj while test -n "$1"; do
     99  1.1.1.1.2.2  snj   case $1 in
    100  1.1.1.1.2.2  snj     -c) shift
    101  1.1.1.1.2.2  snj         continue;;
    102  1.1.1.1.2.2  snj 
    103  1.1.1.1.2.2  snj     -d) dir_arg=true
    104  1.1.1.1.2.2  snj         shift
    105  1.1.1.1.2.2  snj         continue;;
    106  1.1.1.1.2.2  snj 
    107  1.1.1.1.2.2  snj     -g) chgrpcmd="$chgrpprog $2"
    108  1.1.1.1.2.2  snj         shift
    109  1.1.1.1.2.2  snj         shift
    110  1.1.1.1.2.2  snj         continue;;
    111  1.1.1.1.2.2  snj 
    112  1.1.1.1.2.2  snj     --help) echo "$usage"; exit $?;;
    113  1.1.1.1.2.2  snj 
    114  1.1.1.1.2.2  snj     -m) chmodcmd="$chmodprog $2"
    115  1.1.1.1.2.2  snj         shift
    116  1.1.1.1.2.2  snj         shift
    117  1.1.1.1.2.2  snj         continue;;
    118  1.1.1.1.2.2  snj 
    119  1.1.1.1.2.2  snj     -o) chowncmd="$chownprog $2"
    120  1.1.1.1.2.2  snj         shift
    121  1.1.1.1.2.2  snj         shift
    122  1.1.1.1.2.2  snj         continue;;
    123  1.1.1.1.2.2  snj 
    124  1.1.1.1.2.2  snj     -s) stripcmd=$stripprog
    125  1.1.1.1.2.2  snj         shift
    126  1.1.1.1.2.2  snj         continue;;
    127  1.1.1.1.2.2  snj 
    128  1.1.1.1.2.2  snj     -t) dstarg=$2
    129  1.1.1.1.2.2  snj 	shift
    130  1.1.1.1.2.2  snj 	shift
    131  1.1.1.1.2.2  snj 	continue;;
    132  1.1.1.1.2.2  snj 
    133  1.1.1.1.2.2  snj     -T) no_target_directory=true
    134  1.1.1.1.2.2  snj 	shift
    135  1.1.1.1.2.2  snj 	continue;;
    136  1.1.1.1.2.2  snj 
    137  1.1.1.1.2.2  snj     --version) echo "$0 $scriptversion"; exit $?;;
    138  1.1.1.1.2.2  snj 
    139  1.1.1.1.2.2  snj     *)  # When -d is used, all remaining arguments are directories to create.
    140  1.1.1.1.2.2  snj 	# When -t is used, the destination is already specified.
    141  1.1.1.1.2.2  snj 	test -n "$dir_arg$dstarg" && break
    142  1.1.1.1.2.2  snj         # Otherwise, the last argument is the destination.  Remove it from $@.
    143  1.1.1.1.2.2  snj 	for arg
    144  1.1.1.1.2.2  snj 	do
    145  1.1.1.1.2.2  snj           if test -n "$dstarg"; then
    146  1.1.1.1.2.2  snj 	    # $@ is not empty: it contains at least $arg.
    147  1.1.1.1.2.2  snj 	    set fnord "$@" "$dstarg"
    148  1.1.1.1.2.2  snj 	    shift # fnord
    149  1.1.1.1.2.2  snj 	  fi
    150  1.1.1.1.2.2  snj 	  shift # arg
    151  1.1.1.1.2.2  snj 	  dstarg=$arg
    152  1.1.1.1.2.2  snj 	done
    153  1.1.1.1.2.2  snj 	break;;
    154  1.1.1.1.2.2  snj   esac
    155  1.1.1.1.2.2  snj done
    156  1.1.1.1.2.2  snj 
    157  1.1.1.1.2.2  snj if test -z "$1"; then
    158  1.1.1.1.2.2  snj   if test -z "$dir_arg"; then
    159  1.1.1.1.2.2  snj     echo "$0: no input file specified." >&2
    160  1.1.1.1.2.2  snj     exit 1
    161  1.1.1.1.2.2  snj   fi
    162  1.1.1.1.2.2  snj   # It's OK to call `install-sh -d' without argument.
    163  1.1.1.1.2.2  snj   # This can happen when creating conditional directories.
    164  1.1.1.1.2.2  snj   exit 0
    165  1.1.1.1.2.2  snj fi
    166  1.1.1.1.2.2  snj 
    167  1.1.1.1.2.2  snj for src
    168  1.1.1.1.2.2  snj do
    169  1.1.1.1.2.2  snj   # Protect names starting with `-'.
    170  1.1.1.1.2.2  snj   case $src in
    171  1.1.1.1.2.2  snj     -*) src=./$src ;;
    172  1.1.1.1.2.2  snj   esac
    173  1.1.1.1.2.2  snj 
    174  1.1.1.1.2.2  snj   if test -n "$dir_arg"; then
    175  1.1.1.1.2.2  snj     dst=$src
    176  1.1.1.1.2.2  snj     src=
    177  1.1.1.1.2.2  snj 
    178  1.1.1.1.2.2  snj     if test -d "$dst"; then
    179  1.1.1.1.2.2  snj       mkdircmd=:
    180  1.1.1.1.2.2  snj       chmodcmd=
    181  1.1.1.1.2.2  snj     else
    182  1.1.1.1.2.2  snj       mkdircmd=$mkdirprog
    183  1.1.1.1.2.2  snj     fi
    184  1.1.1.1.2.2  snj   else
    185  1.1.1.1.2.2  snj     # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
    186  1.1.1.1.2.2  snj     # might cause directories to be created, which would be especially bad
    187  1.1.1.1.2.2  snj     # if $src (and thus $dsttmp) contains '*'.
    188  1.1.1.1.2.2  snj     if test ! -f "$src" && test ! -d "$src"; then
    189  1.1.1.1.2.2  snj       echo "$0: $src does not exist." >&2
    190  1.1.1.1.2.2  snj       exit 1
    191  1.1.1.1.2.2  snj     fi
    192  1.1.1.1.2.2  snj 
    193  1.1.1.1.2.2  snj     if test -z "$dstarg"; then
    194  1.1.1.1.2.2  snj       echo "$0: no destination specified." >&2
    195  1.1.1.1.2.2  snj       exit 1
    196  1.1.1.1.2.2  snj     fi
    197  1.1.1.1.2.2  snj 
    198  1.1.1.1.2.2  snj     dst=$dstarg
    199  1.1.1.1.2.2  snj     # Protect names starting with `-'.
    200  1.1.1.1.2.2  snj     case $dst in
    201  1.1.1.1.2.2  snj       -*) dst=./$dst ;;
    202  1.1.1.1.2.2  snj     esac
    203  1.1.1.1.2.2  snj 
    204  1.1.1.1.2.2  snj     # If destination is a directory, append the input filename; won't work
    205  1.1.1.1.2.2  snj     # if double slashes aren't ignored.
    206  1.1.1.1.2.2  snj     if test -d "$dst"; then
    207  1.1.1.1.2.2  snj       if test -n "$no_target_directory"; then
    208  1.1.1.1.2.2  snj 	echo "$0: $dstarg: Is a directory" >&2
    209  1.1.1.1.2.2  snj 	exit 1
    210  1.1.1.1.2.2  snj       fi
    211  1.1.1.1.2.2  snj       dst=$dst/`basename "$src"`
    212  1.1.1.1.2.2  snj     fi
    213  1.1.1.1.2.2  snj   fi
    214  1.1.1.1.2.2  snj 
    215  1.1.1.1.2.2  snj   # This sed command emulates the dirname command.
    216  1.1.1.1.2.2  snj   dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'`
    217  1.1.1.1.2.2  snj 
    218  1.1.1.1.2.2  snj   # Make sure that the destination directory exists.
    219  1.1.1.1.2.2  snj 
    220  1.1.1.1.2.2  snj   # Skip lots of stat calls in the usual case.
    221  1.1.1.1.2.2  snj   if test ! -d "$dstdir"; then
    222  1.1.1.1.2.2  snj     defaultIFS='
    223  1.1.1.1.2.2  snj 	 '
    224  1.1.1.1.2.2  snj     IFS="${IFS-$defaultIFS}"
    225  1.1.1.1.2.2  snj 
    226  1.1.1.1.2.2  snj     oIFS=$IFS
    227  1.1.1.1.2.2  snj     # Some sh's can't handle IFS=/ for some reason.
    228  1.1.1.1.2.2  snj     IFS='%'
    229  1.1.1.1.2.2  snj     set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
    230  1.1.1.1.2.2  snj     shift
    231  1.1.1.1.2.2  snj     IFS=$oIFS
    232  1.1.1.1.2.2  snj 
    233  1.1.1.1.2.2  snj     pathcomp=
    234  1.1.1.1.2.2  snj 
    235  1.1.1.1.2.2  snj     while test $# -ne 0 ; do
    236  1.1.1.1.2.2  snj       pathcomp=$pathcomp$1
    237  1.1.1.1.2.2  snj       shift
    238  1.1.1.1.2.2  snj       if test ! -d "$pathcomp"; then
    239  1.1.1.1.2.2  snj         $mkdirprog "$pathcomp"
    240  1.1.1.1.2.2  snj 	# mkdir can fail with a `File exist' error in case several
    241  1.1.1.1.2.2  snj 	# install-sh are creating the directory concurrently.  This
    242  1.1.1.1.2.2  snj 	# is OK.
    243  1.1.1.1.2.2  snj 	test -d "$pathcomp" || exit
    244  1.1.1.1.2.2  snj       fi
    245  1.1.1.1.2.2  snj       pathcomp=$pathcomp/
    246  1.1.1.1.2.2  snj     done
    247  1.1.1.1.2.2  snj   fi
    248  1.1.1.1.2.2  snj 
    249  1.1.1.1.2.2  snj   if test -n "$dir_arg"; then
    250  1.1.1.1.2.2  snj     $doit $mkdircmd "$dst" \
    251  1.1.1.1.2.2  snj       && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
    252  1.1.1.1.2.2  snj       && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
    253  1.1.1.1.2.2  snj       && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
    254  1.1.1.1.2.2  snj       && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
    255  1.1.1.1.2.2  snj 
    256  1.1.1.1.2.2  snj   else
    257  1.1.1.1.2.2  snj     dstfile=`basename "$dst"`
    258  1.1.1.1.2.2  snj 
    259  1.1.1.1.2.2  snj     # Make a couple of temp file names in the proper directory.
    260  1.1.1.1.2.2  snj     dsttmp=$dstdir/_inst.$$_
    261  1.1.1.1.2.2  snj     rmtmp=$dstdir/_rm.$$_
    262  1.1.1.1.2.2  snj 
    263  1.1.1.1.2.2  snj     # Trap to clean up those temp files at exit.
    264  1.1.1.1.2.2  snj     trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
    265  1.1.1.1.2.2  snj     trap '(exit $?); exit' 1 2 13 15
    266  1.1.1.1.2.2  snj 
    267  1.1.1.1.2.2  snj     # Copy the file name to the temp name.
    268  1.1.1.1.2.2  snj     $doit $cpprog "$src" "$dsttmp" &&
    269  1.1.1.1.2.2  snj 
    270  1.1.1.1.2.2  snj     # and set any options; do chmod last to preserve setuid bits.
    271  1.1.1.1.2.2  snj     #
    272  1.1.1.1.2.2  snj     # If any of these fail, we abort the whole thing.  If we want to
    273  1.1.1.1.2.2  snj     # ignore errors from any of these, just make sure not to ignore
    274  1.1.1.1.2.2  snj     # errors from the above "$doit $cpprog $src $dsttmp" command.
    275  1.1.1.1.2.2  snj     #
    276  1.1.1.1.2.2  snj     { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
    277  1.1.1.1.2.2  snj       && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
    278  1.1.1.1.2.2  snj       && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
    279  1.1.1.1.2.2  snj       && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
    280  1.1.1.1.2.2  snj 
    281  1.1.1.1.2.2  snj     # Now rename the file to the real destination.
    282  1.1.1.1.2.2  snj     { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \
    283  1.1.1.1.2.2  snj       || {
    284  1.1.1.1.2.2  snj 	   # The rename failed, perhaps because mv can't rename something else
    285  1.1.1.1.2.2  snj 	   # to itself, or perhaps because mv is so ancient that it does not
    286  1.1.1.1.2.2  snj 	   # support -f.
    287  1.1.1.1.2.2  snj 
    288  1.1.1.1.2.2  snj 	   # Now remove or move aside any old file at destination location.
    289  1.1.1.1.2.2  snj 	   # We try this two ways since rm can't unlink itself on some
    290  1.1.1.1.2.2  snj 	   # systems and the destination file might be busy for other
    291  1.1.1.1.2.2  snj 	   # reasons.  In this case, the final cleanup might fail but the new
    292  1.1.1.1.2.2  snj 	   # file should still install successfully.
    293  1.1.1.1.2.2  snj 	   {
    294  1.1.1.1.2.2  snj 	     if test -f "$dstdir/$dstfile"; then
    295  1.1.1.1.2.2  snj 	       $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
    296  1.1.1.1.2.2  snj 	       || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
    297  1.1.1.1.2.2  snj 	       || {
    298  1.1.1.1.2.2  snj 		 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
    299  1.1.1.1.2.2  snj 		 (exit 1); exit 1
    300  1.1.1.1.2.2  snj 	       }
    301  1.1.1.1.2.2  snj 	     else
    302  1.1.1.1.2.2  snj 	       :
    303  1.1.1.1.2.2  snj 	     fi
    304  1.1.1.1.2.2  snj 	   } &&
    305  1.1.1.1.2.2  snj 
    306  1.1.1.1.2.2  snj 	   # Now rename the file to the real destination.
    307  1.1.1.1.2.2  snj 	   $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
    308  1.1.1.1.2.2  snj 	 }
    309  1.1.1.1.2.2  snj     }
    310  1.1.1.1.2.2  snj   fi || { (exit 1); exit 1; }
    311  1.1.1.1.2.2  snj done
    312  1.1.1.1.2.2  snj 
    313  1.1.1.1.2.2  snj # The final little trick to "correctly" pass the exit status to the exit trap.
    314  1.1.1.1.2.2  snj {
    315  1.1.1.1.2.2  snj   (exit 0); exit 0
    316  1.1.1.1.2.2  snj }
    317  1.1.1.1.2.2  snj 
    318  1.1.1.1.2.2  snj # Local variables:
    319  1.1.1.1.2.2  snj # eval: (add-hook 'write-file-hooks 'time-stamp)
    320  1.1.1.1.2.2  snj # time-stamp-start: "scriptversion="
    321  1.1.1.1.2.2  snj # time-stamp-format: "%:y-%02m-%02d.%02H"
    322  1.1.1.1.2.2  snj # time-stamp-end: "$"
    323  1.1.1.1.2.2  snj # End:
    324