xref: /netbsd-src/external/gpl3/autoconf/dist/build-aux/install-sh (revision d874e91932377fc40d53f102e48fc3ee6f4fe9de)
1*d874e919Schristos#!/bin/sh
2*d874e919Schristos# install - install a program, script, or datafile
3*d874e919Schristos
4*d874e919Schristosscriptversion=2011-11-20.07; # UTC
5*d874e919Schristos
6*d874e919Schristos# This originates from X11R5 (mit/util/scripts/install.sh), which was
7*d874e919Schristos# later released in X11R6 (xc/config/util/install.sh) with the
8*d874e919Schristos# following copyright and license.
9*d874e919Schristos#
10*d874e919Schristos# Copyright (C) 1994 X Consortium
11*d874e919Schristos#
12*d874e919Schristos# Permission is hereby granted, free of charge, to any person obtaining a copy
13*d874e919Schristos# of this software and associated documentation files (the "Software"), to
14*d874e919Schristos# deal in the Software without restriction, including without limitation the
15*d874e919Schristos# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16*d874e919Schristos# sell copies of the Software, and to permit persons to whom the Software is
17*d874e919Schristos# furnished to do so, subject to the following conditions:
18*d874e919Schristos#
19*d874e919Schristos# The above copyright notice and this permission notice shall be included in
20*d874e919Schristos# all copies or substantial portions of the Software.
21*d874e919Schristos#
22*d874e919Schristos# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23*d874e919Schristos# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24*d874e919Schristos# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25*d874e919Schristos# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26*d874e919Schristos# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27*d874e919Schristos# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28*d874e919Schristos#
29*d874e919Schristos# Except as contained in this notice, the name of the X Consortium shall not
30*d874e919Schristos# be used in advertising or otherwise to promote the sale, use or other deal-
31*d874e919Schristos# ings in this Software without prior written authorization from the X Consor-
32*d874e919Schristos# tium.
33*d874e919Schristos#
34*d874e919Schristos#
35*d874e919Schristos# FSF changes to this file are in the public domain.
36*d874e919Schristos#
37*d874e919Schristos# Calling this script install-sh is preferred over install.sh, to prevent
38*d874e919Schristos# 'make' implicit rules from creating a file called install from it
39*d874e919Schristos# when there is no Makefile.
40*d874e919Schristos#
41*d874e919Schristos# This script is compatible with the BSD install script, but was written
42*d874e919Schristos# from scratch.
43*d874e919Schristos
44*d874e919Schristosnl='
45*d874e919Schristos'
46*d874e919SchristosIFS=" ""	$nl"
47*d874e919Schristos
48*d874e919Schristos# set DOITPROG to echo to test this script
49*d874e919Schristos
50*d874e919Schristos# Don't use :- since 4.3BSD and earlier shells don't like it.
51*d874e919Schristosdoit=${DOITPROG-}
52*d874e919Schristosif test -z "$doit"; then
53*d874e919Schristos  doit_exec=exec
54*d874e919Schristoselse
55*d874e919Schristos  doit_exec=$doit
56*d874e919Schristosfi
57*d874e919Schristos
58*d874e919Schristos# Put in absolute file names if you don't have them in your path;
59*d874e919Schristos# or use environment vars.
60*d874e919Schristos
61*d874e919Schristoschgrpprog=${CHGRPPROG-chgrp}
62*d874e919Schristoschmodprog=${CHMODPROG-chmod}
63*d874e919Schristoschownprog=${CHOWNPROG-chown}
64*d874e919Schristoscmpprog=${CMPPROG-cmp}
65*d874e919Schristoscpprog=${CPPROG-cp}
66*d874e919Schristosmkdirprog=${MKDIRPROG-mkdir}
67*d874e919Schristosmvprog=${MVPROG-mv}
68*d874e919Schristosrmprog=${RMPROG-rm}
69*d874e919Schristosstripprog=${STRIPPROG-strip}
70*d874e919Schristos
71*d874e919Schristosposix_glob='?'
72*d874e919Schristosinitialize_posix_glob='
73*d874e919Schristos  test "$posix_glob" != "?" || {
74*d874e919Schristos    if (set -f) 2>/dev/null; then
75*d874e919Schristos      posix_glob=
76*d874e919Schristos    else
77*d874e919Schristos      posix_glob=:
78*d874e919Schristos    fi
79*d874e919Schristos  }
80*d874e919Schristos'
81*d874e919Schristos
82*d874e919Schristosposix_mkdir=
83*d874e919Schristos
84*d874e919Schristos# Desired mode of installed file.
85*d874e919Schristosmode=0755
86*d874e919Schristos
87*d874e919Schristoschgrpcmd=
88*d874e919Schristoschmodcmd=$chmodprog
89*d874e919Schristoschowncmd=
90*d874e919Schristosmvcmd=$mvprog
91*d874e919Schristosrmcmd="$rmprog -f"
92*d874e919Schristosstripcmd=
93*d874e919Schristos
94*d874e919Schristossrc=
95*d874e919Schristosdst=
96*d874e919Schristosdir_arg=
97*d874e919Schristosdst_arg=
98*d874e919Schristos
99*d874e919Schristoscopy_on_change=false
100*d874e919Schristosno_target_directory=
101*d874e919Schristos
102*d874e919Schristosusage="\
103*d874e919SchristosUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104*d874e919Schristos   or: $0 [OPTION]... SRCFILES... DIRECTORY
105*d874e919Schristos   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106*d874e919Schristos   or: $0 [OPTION]... -d DIRECTORIES...
107*d874e919Schristos
108*d874e919SchristosIn the 1st form, copy SRCFILE to DSTFILE.
109*d874e919SchristosIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110*d874e919SchristosIn the 4th, create DIRECTORIES.
111*d874e919Schristos
112*d874e919SchristosOptions:
113*d874e919Schristos     --help     display this help and exit.
114*d874e919Schristos     --version  display version info and exit.
115*d874e919Schristos
116*d874e919Schristos  -c            (ignored)
117*d874e919Schristos  -C            install only if different (preserve the last data modification time)
118*d874e919Schristos  -d            create directories instead of installing files.
119*d874e919Schristos  -g GROUP      $chgrpprog installed files to GROUP.
120*d874e919Schristos  -m MODE       $chmodprog installed files to MODE.
121*d874e919Schristos  -o USER       $chownprog installed files to USER.
122*d874e919Schristos  -s            $stripprog installed files.
123*d874e919Schristos  -t DIRECTORY  install into DIRECTORY.
124*d874e919Schristos  -T            report an error if DSTFILE is a directory.
125*d874e919Schristos
126*d874e919SchristosEnvironment variables override the default commands:
127*d874e919Schristos  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128*d874e919Schristos  RMPROG STRIPPROG
129*d874e919Schristos"
130*d874e919Schristos
131*d874e919Schristoswhile test $# -ne 0; do
132*d874e919Schristos  case $1 in
133*d874e919Schristos    -c) ;;
134*d874e919Schristos
135*d874e919Schristos    -C) copy_on_change=true;;
136*d874e919Schristos
137*d874e919Schristos    -d) dir_arg=true;;
138*d874e919Schristos
139*d874e919Schristos    -g) chgrpcmd="$chgrpprog $2"
140*d874e919Schristos	shift;;
141*d874e919Schristos
142*d874e919Schristos    --help) echo "$usage"; exit $?;;
143*d874e919Schristos
144*d874e919Schristos    -m) mode=$2
145*d874e919Schristos	case $mode in
146*d874e919Schristos	  *' '* | *'	'* | *'
147*d874e919Schristos'*	  | *'*'* | *'?'* | *'['*)
148*d874e919Schristos	    echo "$0: invalid mode: $mode" >&2
149*d874e919Schristos	    exit 1;;
150*d874e919Schristos	esac
151*d874e919Schristos	shift;;
152*d874e919Schristos
153*d874e919Schristos    -o) chowncmd="$chownprog $2"
154*d874e919Schristos	shift;;
155*d874e919Schristos
156*d874e919Schristos    -s) stripcmd=$stripprog;;
157*d874e919Schristos
158*d874e919Schristos    -t) dst_arg=$2
159*d874e919Schristos	# Protect names problematic for 'test' and other utilities.
160*d874e919Schristos	case $dst_arg in
161*d874e919Schristos	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162*d874e919Schristos	esac
163*d874e919Schristos	shift;;
164*d874e919Schristos
165*d874e919Schristos    -T) no_target_directory=true;;
166*d874e919Schristos
167*d874e919Schristos    --version) echo "$0 $scriptversion"; exit $?;;
168*d874e919Schristos
169*d874e919Schristos    --)	shift
170*d874e919Schristos	break;;
171*d874e919Schristos
172*d874e919Schristos    -*)	echo "$0: invalid option: $1" >&2
173*d874e919Schristos	exit 1;;
174*d874e919Schristos
175*d874e919Schristos    *)  break;;
176*d874e919Schristos  esac
177*d874e919Schristos  shift
178*d874e919Schristosdone
179*d874e919Schristos
180*d874e919Schristosif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181*d874e919Schristos  # When -d is used, all remaining arguments are directories to create.
182*d874e919Schristos  # When -t is used, the destination is already specified.
183*d874e919Schristos  # Otherwise, the last argument is the destination.  Remove it from $@.
184*d874e919Schristos  for arg
185*d874e919Schristos  do
186*d874e919Schristos    if test -n "$dst_arg"; then
187*d874e919Schristos      # $@ is not empty: it contains at least $arg.
188*d874e919Schristos      set fnord "$@" "$dst_arg"
189*d874e919Schristos      shift # fnord
190*d874e919Schristos    fi
191*d874e919Schristos    shift # arg
192*d874e919Schristos    dst_arg=$arg
193*d874e919Schristos    # Protect names problematic for 'test' and other utilities.
194*d874e919Schristos    case $dst_arg in
195*d874e919Schristos      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196*d874e919Schristos    esac
197*d874e919Schristos  done
198*d874e919Schristosfi
199*d874e919Schristos
200*d874e919Schristosif test $# -eq 0; then
201*d874e919Schristos  if test -z "$dir_arg"; then
202*d874e919Schristos    echo "$0: no input file specified." >&2
203*d874e919Schristos    exit 1
204*d874e919Schristos  fi
205*d874e919Schristos  # It's OK to call 'install-sh -d' without argument.
206*d874e919Schristos  # This can happen when creating conditional directories.
207*d874e919Schristos  exit 0
208*d874e919Schristosfi
209*d874e919Schristos
210*d874e919Schristosif test -z "$dir_arg"; then
211*d874e919Schristos  do_exit='(exit $ret); exit $ret'
212*d874e919Schristos  trap "ret=129; $do_exit" 1
213*d874e919Schristos  trap "ret=130; $do_exit" 2
214*d874e919Schristos  trap "ret=141; $do_exit" 13
215*d874e919Schristos  trap "ret=143; $do_exit" 15
216*d874e919Schristos
217*d874e919Schristos  # Set umask so as not to create temps with too-generous modes.
218*d874e919Schristos  # However, 'strip' requires both read and write access to temps.
219*d874e919Schristos  case $mode in
220*d874e919Schristos    # Optimize common cases.
221*d874e919Schristos    *644) cp_umask=133;;
222*d874e919Schristos    *755) cp_umask=22;;
223*d874e919Schristos
224*d874e919Schristos    *[0-7])
225*d874e919Schristos      if test -z "$stripcmd"; then
226*d874e919Schristos	u_plus_rw=
227*d874e919Schristos      else
228*d874e919Schristos	u_plus_rw='% 200'
229*d874e919Schristos      fi
230*d874e919Schristos      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231*d874e919Schristos    *)
232*d874e919Schristos      if test -z "$stripcmd"; then
233*d874e919Schristos	u_plus_rw=
234*d874e919Schristos      else
235*d874e919Schristos	u_plus_rw=,u+rw
236*d874e919Schristos      fi
237*d874e919Schristos      cp_umask=$mode$u_plus_rw;;
238*d874e919Schristos  esac
239*d874e919Schristosfi
240*d874e919Schristos
241*d874e919Schristosfor src
242*d874e919Schristosdo
243*d874e919Schristos  # Protect names problematic for 'test' and other utilities.
244*d874e919Schristos  case $src in
245*d874e919Schristos    -* | [=\(\)!]) src=./$src;;
246*d874e919Schristos  esac
247*d874e919Schristos
248*d874e919Schristos  if test -n "$dir_arg"; then
249*d874e919Schristos    dst=$src
250*d874e919Schristos    dstdir=$dst
251*d874e919Schristos    test -d "$dstdir"
252*d874e919Schristos    dstdir_status=$?
253*d874e919Schristos  else
254*d874e919Schristos
255*d874e919Schristos    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256*d874e919Schristos    # might cause directories to be created, which would be especially bad
257*d874e919Schristos    # if $src (and thus $dsttmp) contains '*'.
258*d874e919Schristos    if test ! -f "$src" && test ! -d "$src"; then
259*d874e919Schristos      echo "$0: $src does not exist." >&2
260*d874e919Schristos      exit 1
261*d874e919Schristos    fi
262*d874e919Schristos
263*d874e919Schristos    if test -z "$dst_arg"; then
264*d874e919Schristos      echo "$0: no destination specified." >&2
265*d874e919Schristos      exit 1
266*d874e919Schristos    fi
267*d874e919Schristos    dst=$dst_arg
268*d874e919Schristos
269*d874e919Schristos    # If destination is a directory, append the input filename; won't work
270*d874e919Schristos    # if double slashes aren't ignored.
271*d874e919Schristos    if test -d "$dst"; then
272*d874e919Schristos      if test -n "$no_target_directory"; then
273*d874e919Schristos	echo "$0: $dst_arg: Is a directory" >&2
274*d874e919Schristos	exit 1
275*d874e919Schristos      fi
276*d874e919Schristos      dstdir=$dst
277*d874e919Schristos      dst=$dstdir/`basename "$src"`
278*d874e919Schristos      dstdir_status=0
279*d874e919Schristos    else
280*d874e919Schristos      # Prefer dirname, but fall back on a substitute if dirname fails.
281*d874e919Schristos      dstdir=`
282*d874e919Schristos	(dirname "$dst") 2>/dev/null ||
283*d874e919Schristos	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284*d874e919Schristos	     X"$dst" : 'X\(//\)[^/]' \| \
285*d874e919Schristos	     X"$dst" : 'X\(//\)$' \| \
286*d874e919Schristos	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287*d874e919Schristos	echo X"$dst" |
288*d874e919Schristos	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289*d874e919Schristos		   s//\1/
290*d874e919Schristos		   q
291*d874e919Schristos		 }
292*d874e919Schristos		 /^X\(\/\/\)[^/].*/{
293*d874e919Schristos		   s//\1/
294*d874e919Schristos		   q
295*d874e919Schristos		 }
296*d874e919Schristos		 /^X\(\/\/\)$/{
297*d874e919Schristos		   s//\1/
298*d874e919Schristos		   q
299*d874e919Schristos		 }
300*d874e919Schristos		 /^X\(\/\).*/{
301*d874e919Schristos		   s//\1/
302*d874e919Schristos		   q
303*d874e919Schristos		 }
304*d874e919Schristos		 s/.*/./; q'
305*d874e919Schristos      `
306*d874e919Schristos
307*d874e919Schristos      test -d "$dstdir"
308*d874e919Schristos      dstdir_status=$?
309*d874e919Schristos    fi
310*d874e919Schristos  fi
311*d874e919Schristos
312*d874e919Schristos  obsolete_mkdir_used=false
313*d874e919Schristos
314*d874e919Schristos  if test $dstdir_status != 0; then
315*d874e919Schristos    case $posix_mkdir in
316*d874e919Schristos      '')
317*d874e919Schristos	# Create intermediate dirs using mode 755 as modified by the umask.
318*d874e919Schristos	# This is like FreeBSD 'install' as of 1997-10-28.
319*d874e919Schristos	umask=`umask`
320*d874e919Schristos	case $stripcmd.$umask in
321*d874e919Schristos	  # Optimize common cases.
322*d874e919Schristos	  *[2367][2367]) mkdir_umask=$umask;;
323*d874e919Schristos	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324*d874e919Schristos
325*d874e919Schristos	  *[0-7])
326*d874e919Schristos	    mkdir_umask=`expr $umask + 22 \
327*d874e919Schristos	      - $umask % 100 % 40 + $umask % 20 \
328*d874e919Schristos	      - $umask % 10 % 4 + $umask % 2
329*d874e919Schristos	    `;;
330*d874e919Schristos	  *) mkdir_umask=$umask,go-w;;
331*d874e919Schristos	esac
332*d874e919Schristos
333*d874e919Schristos	# With -d, create the new directory with the user-specified mode.
334*d874e919Schristos	# Otherwise, rely on $mkdir_umask.
335*d874e919Schristos	if test -n "$dir_arg"; then
336*d874e919Schristos	  mkdir_mode=-m$mode
337*d874e919Schristos	else
338*d874e919Schristos	  mkdir_mode=
339*d874e919Schristos	fi
340*d874e919Schristos
341*d874e919Schristos	posix_mkdir=false
342*d874e919Schristos	case $umask in
343*d874e919Schristos	  *[123567][0-7][0-7])
344*d874e919Schristos	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345*d874e919Schristos	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346*d874e919Schristos	    ;;
347*d874e919Schristos	  *)
348*d874e919Schristos	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349*d874e919Schristos	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350*d874e919Schristos
351*d874e919Schristos	    if (umask $mkdir_umask &&
352*d874e919Schristos		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353*d874e919Schristos	    then
354*d874e919Schristos	      if test -z "$dir_arg" || {
355*d874e919Schristos		   # Check for POSIX incompatibilities with -m.
356*d874e919Schristos		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357*d874e919Schristos		   # other-writable bit of parent directory when it shouldn't.
358*d874e919Schristos		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359*d874e919Schristos		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360*d874e919Schristos		   case $ls_ld_tmpdir in
361*d874e919Schristos		     d????-?r-*) different_mode=700;;
362*d874e919Schristos		     d????-?--*) different_mode=755;;
363*d874e919Schristos		     *) false;;
364*d874e919Schristos		   esac &&
365*d874e919Schristos		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366*d874e919Schristos		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367*d874e919Schristos		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368*d874e919Schristos		   }
369*d874e919Schristos		 }
370*d874e919Schristos	      then posix_mkdir=:
371*d874e919Schristos	      fi
372*d874e919Schristos	      rmdir "$tmpdir/d" "$tmpdir"
373*d874e919Schristos	    else
374*d874e919Schristos	      # Remove any dirs left behind by ancient mkdir implementations.
375*d874e919Schristos	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376*d874e919Schristos	    fi
377*d874e919Schristos	    trap '' 0;;
378*d874e919Schristos	esac;;
379*d874e919Schristos    esac
380*d874e919Schristos
381*d874e919Schristos    if
382*d874e919Schristos      $posix_mkdir && (
383*d874e919Schristos	umask $mkdir_umask &&
384*d874e919Schristos	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385*d874e919Schristos      )
386*d874e919Schristos    then :
387*d874e919Schristos    else
388*d874e919Schristos
389*d874e919Schristos      # The umask is ridiculous, or mkdir does not conform to POSIX,
390*d874e919Schristos      # or it failed possibly due to a race condition.  Create the
391*d874e919Schristos      # directory the slow way, step by step, checking for races as we go.
392*d874e919Schristos
393*d874e919Schristos      case $dstdir in
394*d874e919Schristos	/*) prefix='/';;
395*d874e919Schristos	[-=\(\)!]*) prefix='./';;
396*d874e919Schristos	*)  prefix='';;
397*d874e919Schristos      esac
398*d874e919Schristos
399*d874e919Schristos      eval "$initialize_posix_glob"
400*d874e919Schristos
401*d874e919Schristos      oIFS=$IFS
402*d874e919Schristos      IFS=/
403*d874e919Schristos      $posix_glob set -f
404*d874e919Schristos      set fnord $dstdir
405*d874e919Schristos      shift
406*d874e919Schristos      $posix_glob set +f
407*d874e919Schristos      IFS=$oIFS
408*d874e919Schristos
409*d874e919Schristos      prefixes=
410*d874e919Schristos
411*d874e919Schristos      for d
412*d874e919Schristos      do
413*d874e919Schristos	test X"$d" = X && continue
414*d874e919Schristos
415*d874e919Schristos	prefix=$prefix$d
416*d874e919Schristos	if test -d "$prefix"; then
417*d874e919Schristos	  prefixes=
418*d874e919Schristos	else
419*d874e919Schristos	  if $posix_mkdir; then
420*d874e919Schristos	    (umask=$mkdir_umask &&
421*d874e919Schristos	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422*d874e919Schristos	    # Don't fail if two instances are running concurrently.
423*d874e919Schristos	    test -d "$prefix" || exit 1
424*d874e919Schristos	  else
425*d874e919Schristos	    case $prefix in
426*d874e919Schristos	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427*d874e919Schristos	      *) qprefix=$prefix;;
428*d874e919Schristos	    esac
429*d874e919Schristos	    prefixes="$prefixes '$qprefix'"
430*d874e919Schristos	  fi
431*d874e919Schristos	fi
432*d874e919Schristos	prefix=$prefix/
433*d874e919Schristos      done
434*d874e919Schristos
435*d874e919Schristos      if test -n "$prefixes"; then
436*d874e919Schristos	# Don't fail if two instances are running concurrently.
437*d874e919Schristos	(umask $mkdir_umask &&
438*d874e919Schristos	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439*d874e919Schristos	  test -d "$dstdir" || exit 1
440*d874e919Schristos	obsolete_mkdir_used=true
441*d874e919Schristos      fi
442*d874e919Schristos    fi
443*d874e919Schristos  fi
444*d874e919Schristos
445*d874e919Schristos  if test -n "$dir_arg"; then
446*d874e919Schristos    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447*d874e919Schristos    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448*d874e919Schristos    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449*d874e919Schristos      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450*d874e919Schristos  else
451*d874e919Schristos
452*d874e919Schristos    # Make a couple of temp file names in the proper directory.
453*d874e919Schristos    dsttmp=$dstdir/_inst.$$_
454*d874e919Schristos    rmtmp=$dstdir/_rm.$$_
455*d874e919Schristos
456*d874e919Schristos    # Trap to clean up those temp files at exit.
457*d874e919Schristos    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458*d874e919Schristos
459*d874e919Schristos    # Copy the file name to the temp name.
460*d874e919Schristos    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461*d874e919Schristos
462*d874e919Schristos    # and set any options; do chmod last to preserve setuid bits.
463*d874e919Schristos    #
464*d874e919Schristos    # If any of these fail, we abort the whole thing.  If we want to
465*d874e919Schristos    # ignore errors from any of these, just make sure not to ignore
466*d874e919Schristos    # errors from the above "$doit $cpprog $src $dsttmp" command.
467*d874e919Schristos    #
468*d874e919Schristos    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469*d874e919Schristos    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470*d874e919Schristos    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471*d874e919Schristos    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472*d874e919Schristos
473*d874e919Schristos    # If -C, don't bother to copy if it wouldn't change the file.
474*d874e919Schristos    if $copy_on_change &&
475*d874e919Schristos       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476*d874e919Schristos       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477*d874e919Schristos
478*d874e919Schristos       eval "$initialize_posix_glob" &&
479*d874e919Schristos       $posix_glob set -f &&
480*d874e919Schristos       set X $old && old=:$2:$4:$5:$6 &&
481*d874e919Schristos       set X $new && new=:$2:$4:$5:$6 &&
482*d874e919Schristos       $posix_glob set +f &&
483*d874e919Schristos
484*d874e919Schristos       test "$old" = "$new" &&
485*d874e919Schristos       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486*d874e919Schristos    then
487*d874e919Schristos      rm -f "$dsttmp"
488*d874e919Schristos    else
489*d874e919Schristos      # Rename the file to the real destination.
490*d874e919Schristos      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491*d874e919Schristos
492*d874e919Schristos      # The rename failed, perhaps because mv can't rename something else
493*d874e919Schristos      # to itself, or perhaps because mv is so ancient that it does not
494*d874e919Schristos      # support -f.
495*d874e919Schristos      {
496*d874e919Schristos	# Now remove or move aside any old file at destination location.
497*d874e919Schristos	# We try this two ways since rm can't unlink itself on some
498*d874e919Schristos	# systems and the destination file might be busy for other
499*d874e919Schristos	# reasons.  In this case, the final cleanup might fail but the new
500*d874e919Schristos	# file should still install successfully.
501*d874e919Schristos	{
502*d874e919Schristos	  test ! -f "$dst" ||
503*d874e919Schristos	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504*d874e919Schristos	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505*d874e919Schristos	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506*d874e919Schristos	  } ||
507*d874e919Schristos	  { echo "$0: cannot unlink or rename $dst" >&2
508*d874e919Schristos	    (exit 1); exit 1
509*d874e919Schristos	  }
510*d874e919Schristos	} &&
511*d874e919Schristos
512*d874e919Schristos	# Now rename the file to the real destination.
513*d874e919Schristos	$doit $mvcmd "$dsttmp" "$dst"
514*d874e919Schristos      }
515*d874e919Schristos    fi || exit 1
516*d874e919Schristos
517*d874e919Schristos    trap '' 0
518*d874e919Schristos  fi
519*d874e919Schristosdone
520*d874e919Schristos
521*d874e919Schristos# Local variables:
522*d874e919Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
523*d874e919Schristos# time-stamp-start: "scriptversion="
524*d874e919Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
525*d874e919Schristos# time-stamp-time-zone: "UTC"
526*d874e919Schristos# time-stamp-end: "; # UTC"
527*d874e919Schristos# End:
528