install-sh revision 8fff3f40
1e6232409Smrg#!/bin/sh
2e6232409Smrg# install - install a program, script, or datafile
3e6232409Smrg
48fff3f40Smrgscriptversion=2009-04-28.21; # UTC
5e6232409Smrg
6e6232409Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7e6232409Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8e6232409Smrg# following copyright and license.
9e6232409Smrg#
10e6232409Smrg# Copyright (C) 1994 X Consortium
11e6232409Smrg#
12e6232409Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13e6232409Smrg# of this software and associated documentation files (the "Software"), to
14e6232409Smrg# deal in the Software without restriction, including without limitation the
15e6232409Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16e6232409Smrg# sell copies of the Software, and to permit persons to whom the Software is
17e6232409Smrg# furnished to do so, subject to the following conditions:
18e6232409Smrg#
19e6232409Smrg# The above copyright notice and this permission notice shall be included in
20e6232409Smrg# all copies or substantial portions of the Software.
21e6232409Smrg#
22e6232409Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23e6232409Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24e6232409Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25e6232409Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26e6232409Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27e6232409Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28e6232409Smrg#
29e6232409Smrg# Except as contained in this notice, the name of the X Consortium shall not
30e6232409Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31e6232409Smrg# ings in this Software without prior written authorization from the X Consor-
32e6232409Smrg# tium.
33e6232409Smrg#
34e6232409Smrg#
35e6232409Smrg# FSF changes to this file are in the public domain.
36e6232409Smrg#
37e6232409Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38e6232409Smrg# `make' implicit rules from creating a file called install from it
39e6232409Smrg# when there is no Makefile.
40e6232409Smrg#
41e6232409Smrg# This script is compatible with the BSD install script, but was written
428fff3f40Smrg# from scratch.
438fff3f40Smrg
448fff3f40Smrgnl='
458fff3f40Smrg'
468fff3f40SmrgIFS=" ""	$nl"
47e6232409Smrg
48e6232409Smrg# set DOITPROG to echo to test this script
49e6232409Smrg
50e6232409Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
518fff3f40Smrgdoit=${DOITPROG-}
528fff3f40Smrgif test -z "$doit"; then
538fff3f40Smrg  doit_exec=exec
548fff3f40Smrgelse
558fff3f40Smrg  doit_exec=$doit
568fff3f40Smrgfi
57e6232409Smrg
588fff3f40Smrg# Put in absolute file names if you don't have them in your path;
598fff3f40Smrg# or use environment vars.
608fff3f40Smrg
618fff3f40Smrgchgrpprog=${CHGRPPROG-chgrp}
628fff3f40Smrgchmodprog=${CHMODPROG-chmod}
638fff3f40Smrgchownprog=${CHOWNPROG-chown}
648fff3f40Smrgcmpprog=${CMPPROG-cmp}
658fff3f40Smrgcpprog=${CPPROG-cp}
668fff3f40Smrgmkdirprog=${MKDIRPROG-mkdir}
678fff3f40Smrgmvprog=${MVPROG-mv}
688fff3f40Smrgrmprog=${RMPROG-rm}
698fff3f40Smrgstripprog=${STRIPPROG-strip}
708fff3f40Smrg
718fff3f40Smrgposix_glob='?'
728fff3f40Smrginitialize_posix_glob='
738fff3f40Smrg  test "$posix_glob" != "?" || {
748fff3f40Smrg    if (set -f) 2>/dev/null; then
758fff3f40Smrg      posix_glob=
768fff3f40Smrg    else
778fff3f40Smrg      posix_glob=:
788fff3f40Smrg    fi
798fff3f40Smrg  }
808fff3f40Smrg'
81e6232409Smrg
828fff3f40Smrgposix_mkdir=
838fff3f40Smrg
848fff3f40Smrg# Desired mode of installed file.
858fff3f40Smrgmode=0755
86e6232409Smrg
87e6232409Smrgchgrpcmd=
888fff3f40Smrgchmodcmd=$chmodprog
898fff3f40Smrgchowncmd=
908fff3f40Smrgmvcmd=$mvprog
91e6232409Smrgrmcmd="$rmprog -f"
928fff3f40Smrgstripcmd=
938fff3f40Smrg
94e6232409Smrgsrc=
95e6232409Smrgdst=
96e6232409Smrgdir_arg=
978fff3f40Smrgdst_arg=
988fff3f40Smrg
998fff3f40Smrgcopy_on_change=false
100e6232409Smrgno_target_directory=
101e6232409Smrg
1028fff3f40Smrgusage="\
1038fff3f40SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104e6232409Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105e6232409Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106e6232409Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107e6232409Smrg
108e6232409SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109e6232409SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110e6232409SmrgIn the 4th, create DIRECTORIES.
111e6232409Smrg
112e6232409SmrgOptions:
1138fff3f40Smrg     --help     display this help and exit.
1148fff3f40Smrg     --version  display version info and exit.
1158fff3f40Smrg
1168fff3f40Smrg  -c            (ignored)
1178fff3f40Smrg  -C            install only if different (preserve the last data modification time)
1188fff3f40Smrg  -d            create directories instead of installing files.
1198fff3f40Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1208fff3f40Smrg  -m MODE       $chmodprog installed files to MODE.
1218fff3f40Smrg  -o USER       $chownprog installed files to USER.
1228fff3f40Smrg  -s            $stripprog installed files.
1238fff3f40Smrg  -t DIRECTORY  install into DIRECTORY.
1248fff3f40Smrg  -T            report an error if DSTFILE is a directory.
125e6232409Smrg
126e6232409SmrgEnvironment variables override the default commands:
1278fff3f40Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1288fff3f40Smrg  RMPROG STRIPPROG
129e6232409Smrg"
130e6232409Smrg
1318fff3f40Smrgwhile test $# -ne 0; do
132e6232409Smrg  case $1 in
1338fff3f40Smrg    -c) ;;
1348fff3f40Smrg
1358fff3f40Smrg    -C) copy_on_change=true;;
136e6232409Smrg
1378fff3f40Smrg    -d) dir_arg=true;;
138e6232409Smrg
139e6232409Smrg    -g) chgrpcmd="$chgrpprog $2"
1408fff3f40Smrg	shift;;
141e6232409Smrg
142e6232409Smrg    --help) echo "$usage"; exit $?;;
143e6232409Smrg
1448fff3f40Smrg    -m) mode=$2
1458fff3f40Smrg	case $mode in
1468fff3f40Smrg	  *' '* | *'	'* | *'
1478fff3f40Smrg'*	  | *'*'* | *'?'* | *'['*)
1488fff3f40Smrg	    echo "$0: invalid mode: $mode" >&2
1498fff3f40Smrg	    exit 1;;
1508fff3f40Smrg	esac
1518fff3f40Smrg	shift;;
152e6232409Smrg
153e6232409Smrg    -o) chowncmd="$chownprog $2"
1548fff3f40Smrg	shift;;
155e6232409Smrg
1568fff3f40Smrg    -s) stripcmd=$stripprog;;
157e6232409Smrg
1588fff3f40Smrg    -t) dst_arg=$2
1598fff3f40Smrg	shift;;
160e6232409Smrg
1618fff3f40Smrg    -T) no_target_directory=true;;
162e6232409Smrg
163e6232409Smrg    --version) echo "$0 $scriptversion"; exit $?;;
164e6232409Smrg
1658fff3f40Smrg    --)	shift
166e6232409Smrg	break;;
1678fff3f40Smrg
1688fff3f40Smrg    -*)	echo "$0: invalid option: $1" >&2
1698fff3f40Smrg	exit 1;;
1708fff3f40Smrg
1718fff3f40Smrg    *)  break;;
172e6232409Smrg  esac
1738fff3f40Smrg  shift
174e6232409Smrgdone
175e6232409Smrg
1768fff3f40Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1778fff3f40Smrg  # When -d is used, all remaining arguments are directories to create.
1788fff3f40Smrg  # When -t is used, the destination is already specified.
1798fff3f40Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1808fff3f40Smrg  for arg
1818fff3f40Smrg  do
1828fff3f40Smrg    if test -n "$dst_arg"; then
1838fff3f40Smrg      # $@ is not empty: it contains at least $arg.
1848fff3f40Smrg      set fnord "$@" "$dst_arg"
1858fff3f40Smrg      shift # fnord
1868fff3f40Smrg    fi
1878fff3f40Smrg    shift # arg
1888fff3f40Smrg    dst_arg=$arg
1898fff3f40Smrg  done
1908fff3f40Smrgfi
1918fff3f40Smrg
1928fff3f40Smrgif test $# -eq 0; then
193e6232409Smrg  if test -z "$dir_arg"; then
194e6232409Smrg    echo "$0: no input file specified." >&2
195e6232409Smrg    exit 1
196e6232409Smrg  fi
197e6232409Smrg  # It's OK to call `install-sh -d' without argument.
198e6232409Smrg  # This can happen when creating conditional directories.
199e6232409Smrg  exit 0
200e6232409Smrgfi
201e6232409Smrg
2028fff3f40Smrgif test -z "$dir_arg"; then
2038fff3f40Smrg  trap '(exit $?); exit' 1 2 13 15
2048fff3f40Smrg
2058fff3f40Smrg  # Set umask so as not to create temps with too-generous modes.
2068fff3f40Smrg  # However, 'strip' requires both read and write access to temps.
2078fff3f40Smrg  case $mode in
2088fff3f40Smrg    # Optimize common cases.
2098fff3f40Smrg    *644) cp_umask=133;;
2108fff3f40Smrg    *755) cp_umask=22;;
2118fff3f40Smrg
2128fff3f40Smrg    *[0-7])
2138fff3f40Smrg      if test -z "$stripcmd"; then
2148fff3f40Smrg	u_plus_rw=
2158fff3f40Smrg      else
2168fff3f40Smrg	u_plus_rw='% 200'
2178fff3f40Smrg      fi
2188fff3f40Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2198fff3f40Smrg    *)
2208fff3f40Smrg      if test -z "$stripcmd"; then
2218fff3f40Smrg	u_plus_rw=
2228fff3f40Smrg      else
2238fff3f40Smrg	u_plus_rw=,u+rw
2248fff3f40Smrg      fi
2258fff3f40Smrg      cp_umask=$mode$u_plus_rw;;
2268fff3f40Smrg  esac
2278fff3f40Smrgfi
2288fff3f40Smrg
229e6232409Smrgfor src
230e6232409Smrgdo
231e6232409Smrg  # Protect names starting with `-'.
232e6232409Smrg  case $src in
2338fff3f40Smrg    -*) src=./$src;;
234e6232409Smrg  esac
235e6232409Smrg
236e6232409Smrg  if test -n "$dir_arg"; then
237e6232409Smrg    dst=$src
2388fff3f40Smrg    dstdir=$dst
2398fff3f40Smrg    test -d "$dstdir"
2408fff3f40Smrg    dstdir_status=$?
241e6232409Smrg  else
2428fff3f40Smrg
243e6232409Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244e6232409Smrg    # might cause directories to be created, which would be especially bad
245e6232409Smrg    # if $src (and thus $dsttmp) contains '*'.
246e6232409Smrg    if test ! -f "$src" && test ! -d "$src"; then
247e6232409Smrg      echo "$0: $src does not exist." >&2
248e6232409Smrg      exit 1
249e6232409Smrg    fi
250e6232409Smrg
2518fff3f40Smrg    if test -z "$dst_arg"; then
252e6232409Smrg      echo "$0: no destination specified." >&2
253e6232409Smrg      exit 1
254e6232409Smrg    fi
255e6232409Smrg
2568fff3f40Smrg    dst=$dst_arg
257e6232409Smrg    # Protect names starting with `-'.
258e6232409Smrg    case $dst in
2598fff3f40Smrg      -*) dst=./$dst;;
260e6232409Smrg    esac
261e6232409Smrg
262e6232409Smrg    # If destination is a directory, append the input filename; won't work
263e6232409Smrg    # if double slashes aren't ignored.
264e6232409Smrg    if test -d "$dst"; then
265e6232409Smrg      if test -n "$no_target_directory"; then
2668fff3f40Smrg	echo "$0: $dst_arg: Is a directory" >&2
267e6232409Smrg	exit 1
268e6232409Smrg      fi
2698fff3f40Smrg      dstdir=$dst
2708fff3f40Smrg      dst=$dstdir/`basename "$src"`
2718fff3f40Smrg      dstdir_status=0
2728fff3f40Smrg    else
2738fff3f40Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
2748fff3f40Smrg      dstdir=`
2758fff3f40Smrg	(dirname "$dst") 2>/dev/null ||
2768fff3f40Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
2778fff3f40Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
2788fff3f40Smrg	     X"$dst" : 'X\(//\)$' \| \
2798fff3f40Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
2808fff3f40Smrg	echo X"$dst" |
2818fff3f40Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
2828fff3f40Smrg		   s//\1/
2838fff3f40Smrg		   q
2848fff3f40Smrg		 }
2858fff3f40Smrg		 /^X\(\/\/\)[^/].*/{
2868fff3f40Smrg		   s//\1/
2878fff3f40Smrg		   q
2888fff3f40Smrg		 }
2898fff3f40Smrg		 /^X\(\/\/\)$/{
2908fff3f40Smrg		   s//\1/
2918fff3f40Smrg		   q
2928fff3f40Smrg		 }
2938fff3f40Smrg		 /^X\(\/\).*/{
2948fff3f40Smrg		   s//\1/
2958fff3f40Smrg		   q
2968fff3f40Smrg		 }
2978fff3f40Smrg		 s/.*/./; q'
2988fff3f40Smrg      `
2998fff3f40Smrg
3008fff3f40Smrg      test -d "$dstdir"
3018fff3f40Smrg      dstdir_status=$?
302e6232409Smrg    fi
303e6232409Smrg  fi
304e6232409Smrg
3058fff3f40Smrg  obsolete_mkdir_used=false
3068fff3f40Smrg
3078fff3f40Smrg  if test $dstdir_status != 0; then
3088fff3f40Smrg    case $posix_mkdir in
3098fff3f40Smrg      '')
3108fff3f40Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
3118fff3f40Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
3128fff3f40Smrg	umask=`umask`
3138fff3f40Smrg	case $stripcmd.$umask in
3148fff3f40Smrg	  # Optimize common cases.
3158fff3f40Smrg	  *[2367][2367]) mkdir_umask=$umask;;
3168fff3f40Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3178fff3f40Smrg
3188fff3f40Smrg	  *[0-7])
3198fff3f40Smrg	    mkdir_umask=`expr $umask + 22 \
3208fff3f40Smrg	      - $umask % 100 % 40 + $umask % 20 \
3218fff3f40Smrg	      - $umask % 10 % 4 + $umask % 2
3228fff3f40Smrg	    `;;
3238fff3f40Smrg	  *) mkdir_umask=$umask,go-w;;
3248fff3f40Smrg	esac
3258fff3f40Smrg
3268fff3f40Smrg	# With -d, create the new directory with the user-specified mode.
3278fff3f40Smrg	# Otherwise, rely on $mkdir_umask.
3288fff3f40Smrg	if test -n "$dir_arg"; then
3298fff3f40Smrg	  mkdir_mode=-m$mode
3308fff3f40Smrg	else
3318fff3f40Smrg	  mkdir_mode=
3328fff3f40Smrg	fi
3338fff3f40Smrg
3348fff3f40Smrg	posix_mkdir=false
3358fff3f40Smrg	case $umask in
3368fff3f40Smrg	  *[123567][0-7][0-7])
3378fff3f40Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
3388fff3f40Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3398fff3f40Smrg	    ;;
3408fff3f40Smrg	  *)
3418fff3f40Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3428fff3f40Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3438fff3f40Smrg
3448fff3f40Smrg	    if (umask $mkdir_umask &&
3458fff3f40Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3468fff3f40Smrg	    then
3478fff3f40Smrg	      if test -z "$dir_arg" || {
3488fff3f40Smrg		   # Check for POSIX incompatibilities with -m.
3498fff3f40Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3508fff3f40Smrg		   # other-writeable bit of parent directory when it shouldn't.
3518fff3f40Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3528fff3f40Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3538fff3f40Smrg		   case $ls_ld_tmpdir in
3548fff3f40Smrg		     d????-?r-*) different_mode=700;;
3558fff3f40Smrg		     d????-?--*) different_mode=755;;
3568fff3f40Smrg		     *) false;;
3578fff3f40Smrg		   esac &&
3588fff3f40Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3598fff3f40Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3608fff3f40Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3618fff3f40Smrg		   }
3628fff3f40Smrg		 }
3638fff3f40Smrg	      then posix_mkdir=:
3648fff3f40Smrg	      fi
3658fff3f40Smrg	      rmdir "$tmpdir/d" "$tmpdir"
3668fff3f40Smrg	    else
3678fff3f40Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
3688fff3f40Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3698fff3f40Smrg	    fi
3708fff3f40Smrg	    trap '' 0;;
3718fff3f40Smrg	esac;;
3728fff3f40Smrg    esac
373e6232409Smrg
3748fff3f40Smrg    if
3758fff3f40Smrg      $posix_mkdir && (
3768fff3f40Smrg	umask $mkdir_umask &&
3778fff3f40Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3788fff3f40Smrg      )
3798fff3f40Smrg    then :
3808fff3f40Smrg    else
381e6232409Smrg
3828fff3f40Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3838fff3f40Smrg      # or it failed possibly due to a race condition.  Create the
3848fff3f40Smrg      # directory the slow way, step by step, checking for races as we go.
385e6232409Smrg
3868fff3f40Smrg      case $dstdir in
3878fff3f40Smrg	/*) prefix='/';;
3888fff3f40Smrg	-*) prefix='./';;
3898fff3f40Smrg	*)  prefix='';;
3908fff3f40Smrg      esac
391e6232409Smrg
3928fff3f40Smrg      eval "$initialize_posix_glob"
393e6232409Smrg
3948fff3f40Smrg      oIFS=$IFS
3958fff3f40Smrg      IFS=/
3968fff3f40Smrg      $posix_glob set -f
3978fff3f40Smrg      set fnord $dstdir
398e6232409Smrg      shift
3998fff3f40Smrg      $posix_glob set +f
4008fff3f40Smrg      IFS=$oIFS
4018fff3f40Smrg
4028fff3f40Smrg      prefixes=
4038fff3f40Smrg
4048fff3f40Smrg      for d
4058fff3f40Smrg      do
4068fff3f40Smrg	test -z "$d" && continue
4078fff3f40Smrg
4088fff3f40Smrg	prefix=$prefix$d
4098fff3f40Smrg	if test -d "$prefix"; then
4108fff3f40Smrg	  prefixes=
4118fff3f40Smrg	else
4128fff3f40Smrg	  if $posix_mkdir; then
4138fff3f40Smrg	    (umask=$mkdir_umask &&
4148fff3f40Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4158fff3f40Smrg	    # Don't fail if two instances are running concurrently.
4168fff3f40Smrg	    test -d "$prefix" || exit 1
4178fff3f40Smrg	  else
4188fff3f40Smrg	    case $prefix in
4198fff3f40Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4208fff3f40Smrg	      *) qprefix=$prefix;;
4218fff3f40Smrg	    esac
4228fff3f40Smrg	    prefixes="$prefixes '$qprefix'"
4238fff3f40Smrg	  fi
4248fff3f40Smrg	fi
4258fff3f40Smrg	prefix=$prefix/
4268fff3f40Smrg      done
4278fff3f40Smrg
4288fff3f40Smrg      if test -n "$prefixes"; then
4298fff3f40Smrg	# Don't fail if two instances are running concurrently.
4308fff3f40Smrg	(umask $mkdir_umask &&
4318fff3f40Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
4328fff3f40Smrg	  test -d "$dstdir" || exit 1
4338fff3f40Smrg	obsolete_mkdir_used=true
434e6232409Smrg      fi
4358fff3f40Smrg    fi
436e6232409Smrg  fi
437e6232409Smrg
438e6232409Smrg  if test -n "$dir_arg"; then
4398fff3f40Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4408fff3f40Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4418fff3f40Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4428fff3f40Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443e6232409Smrg  else
444e6232409Smrg
445e6232409Smrg    # Make a couple of temp file names in the proper directory.
446e6232409Smrg    dsttmp=$dstdir/_inst.$$_
447e6232409Smrg    rmtmp=$dstdir/_rm.$$_
448e6232409Smrg
449e6232409Smrg    # Trap to clean up those temp files at exit.
450e6232409Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451e6232409Smrg
452e6232409Smrg    # Copy the file name to the temp name.
4538fff3f40Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454e6232409Smrg
455e6232409Smrg    # and set any options; do chmod last to preserve setuid bits.
456e6232409Smrg    #
457e6232409Smrg    # If any of these fail, we abort the whole thing.  If we want to
458e6232409Smrg    # ignore errors from any of these, just make sure not to ignore
459e6232409Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460e6232409Smrg    #
4618fff3f40Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4628fff3f40Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4638fff3f40Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4648fff3f40Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4658fff3f40Smrg
4668fff3f40Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4678fff3f40Smrg    if $copy_on_change &&
4688fff3f40Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4698fff3f40Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4708fff3f40Smrg
4718fff3f40Smrg       eval "$initialize_posix_glob" &&
4728fff3f40Smrg       $posix_glob set -f &&
4738fff3f40Smrg       set X $old && old=:$2:$4:$5:$6 &&
4748fff3f40Smrg       set X $new && new=:$2:$4:$5:$6 &&
4758fff3f40Smrg       $posix_glob set +f &&
4768fff3f40Smrg
4778fff3f40Smrg       test "$old" = "$new" &&
4788fff3f40Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4798fff3f40Smrg    then
4808fff3f40Smrg      rm -f "$dsttmp"
4818fff3f40Smrg    else
4828fff3f40Smrg      # Rename the file to the real destination.
4838fff3f40Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4848fff3f40Smrg
4858fff3f40Smrg      # The rename failed, perhaps because mv can't rename something else
4868fff3f40Smrg      # to itself, or perhaps because mv is so ancient that it does not
4878fff3f40Smrg      # support -f.
4888fff3f40Smrg      {
4898fff3f40Smrg	# Now remove or move aside any old file at destination location.
4908fff3f40Smrg	# We try this two ways since rm can't unlink itself on some
4918fff3f40Smrg	# systems and the destination file might be busy for other
4928fff3f40Smrg	# reasons.  In this case, the final cleanup might fail but the new
4938fff3f40Smrg	# file should still install successfully.
4948fff3f40Smrg	{
4958fff3f40Smrg	  test ! -f "$dst" ||
4968fff3f40Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
4978fff3f40Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4988fff3f40Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4998fff3f40Smrg	  } ||
5008fff3f40Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5018fff3f40Smrg	    (exit 1); exit 1
5028fff3f40Smrg	  }
5038fff3f40Smrg	} &&
5048fff3f40Smrg
5058fff3f40Smrg	# Now rename the file to the real destination.
5068fff3f40Smrg	$doit $mvcmd "$dsttmp" "$dst"
5078fff3f40Smrg      }
5088fff3f40Smrg    fi || exit 1
5098fff3f40Smrg
5108fff3f40Smrg    trap '' 0
5118fff3f40Smrg  fi
512e6232409Smrgdone
513e6232409Smrg
514e6232409Smrg# Local variables:
515e6232409Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516e6232409Smrg# time-stamp-start: "scriptversion="
517e6232409Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5188fff3f40Smrg# time-stamp-time-zone: "UTC"
5198fff3f40Smrg# time-stamp-end: "; # UTC"
520e6232409Smrg# End:
521