install-sh revision 578741aa
1145b7b3cSmrg#!/bin/sh
2145b7b3cSmrg# install - install a program, script, or datafile
3145b7b3cSmrg
4578741aaSmrgscriptversion=2009-04-28.21; # UTC
5145b7b3cSmrg
6145b7b3cSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7145b7b3cSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8145b7b3cSmrg# following copyright and license.
9145b7b3cSmrg#
10145b7b3cSmrg# Copyright (C) 1994 X Consortium
11145b7b3cSmrg#
12145b7b3cSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13145b7b3cSmrg# of this software and associated documentation files (the "Software"), to
14145b7b3cSmrg# deal in the Software without restriction, including without limitation the
15145b7b3cSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16145b7b3cSmrg# sell copies of the Software, and to permit persons to whom the Software is
17145b7b3cSmrg# furnished to do so, subject to the following conditions:
18145b7b3cSmrg#
19145b7b3cSmrg# The above copyright notice and this permission notice shall be included in
20145b7b3cSmrg# all copies or substantial portions of the Software.
21145b7b3cSmrg#
22145b7b3cSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23145b7b3cSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24145b7b3cSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25145b7b3cSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26145b7b3cSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27145b7b3cSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28145b7b3cSmrg#
29145b7b3cSmrg# Except as contained in this notice, the name of the X Consortium shall not
30145b7b3cSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31145b7b3cSmrg# ings in this Software without prior written authorization from the X Consor-
32145b7b3cSmrg# tium.
33145b7b3cSmrg#
34145b7b3cSmrg#
35145b7b3cSmrg# FSF changes to this file are in the public domain.
36145b7b3cSmrg#
37145b7b3cSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38145b7b3cSmrg# `make' implicit rules from creating a file called install from it
39145b7b3cSmrg# when there is no Makefile.
40145b7b3cSmrg#
41145b7b3cSmrg# This script is compatible with the BSD install script, but was written
42145b7b3cSmrg# from scratch.
43145b7b3cSmrg
44145b7b3cSmrgnl='
45145b7b3cSmrg'
46145b7b3cSmrgIFS=" ""	$nl"
47145b7b3cSmrg
48145b7b3cSmrg# set DOITPROG to echo to test this script
49145b7b3cSmrg
50145b7b3cSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51578741aaSmrgdoit=${DOITPROG-}
52145b7b3cSmrgif test -z "$doit"; then
53145b7b3cSmrg  doit_exec=exec
54145b7b3cSmrgelse
55145b7b3cSmrg  doit_exec=$doit
56145b7b3cSmrgfi
57145b7b3cSmrg
58145b7b3cSmrg# Put in absolute file names if you don't have them in your path;
59145b7b3cSmrg# or use environment vars.
60145b7b3cSmrg
61578741aaSmrgchgrpprog=${CHGRPPROG-chgrp}
62578741aaSmrgchmodprog=${CHMODPROG-chmod}
63578741aaSmrgchownprog=${CHOWNPROG-chown}
64578741aaSmrgcmpprog=${CMPPROG-cmp}
65578741aaSmrgcpprog=${CPPROG-cp}
66578741aaSmrgmkdirprog=${MKDIRPROG-mkdir}
67578741aaSmrgmvprog=${MVPROG-mv}
68578741aaSmrgrmprog=${RMPROG-rm}
69578741aaSmrgstripprog=${STRIPPROG-strip}
70578741aaSmrg
71578741aaSmrgposix_glob='?'
72578741aaSmrginitialize_posix_glob='
73578741aaSmrg  test "$posix_glob" != "?" || {
74578741aaSmrg    if (set -f) 2>/dev/null; then
75578741aaSmrg      posix_glob=
76578741aaSmrg    else
77578741aaSmrg      posix_glob=:
78578741aaSmrg    fi
79578741aaSmrg  }
80578741aaSmrg'
81145b7b3cSmrg
82145b7b3cSmrgposix_mkdir=
83145b7b3cSmrg
84145b7b3cSmrg# Desired mode of installed file.
85145b7b3cSmrgmode=0755
86145b7b3cSmrg
87578741aaSmrgchgrpcmd=
88145b7b3cSmrgchmodcmd=$chmodprog
89145b7b3cSmrgchowncmd=
90578741aaSmrgmvcmd=$mvprog
91145b7b3cSmrgrmcmd="$rmprog -f"
92578741aaSmrgstripcmd=
93578741aaSmrg
94145b7b3cSmrgsrc=
95145b7b3cSmrgdst=
96145b7b3cSmrgdir_arg=
97578741aaSmrgdst_arg=
98578741aaSmrg
99578741aaSmrgcopy_on_change=false
100145b7b3cSmrgno_target_directory=
101145b7b3cSmrg
102578741aaSmrgusage="\
103578741aaSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104145b7b3cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105145b7b3cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106145b7b3cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107145b7b3cSmrg
108145b7b3cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109145b7b3cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110145b7b3cSmrgIn the 4th, create DIRECTORIES.
111145b7b3cSmrg
112145b7b3cSmrgOptions:
113578741aaSmrg     --help     display this help and exit.
114578741aaSmrg     --version  display version info and exit.
115578741aaSmrg
116578741aaSmrg  -c            (ignored)
117578741aaSmrg  -C            install only if different (preserve the last data modification time)
118578741aaSmrg  -d            create directories instead of installing files.
119578741aaSmrg  -g GROUP      $chgrpprog installed files to GROUP.
120578741aaSmrg  -m MODE       $chmodprog installed files to MODE.
121578741aaSmrg  -o USER       $chownprog installed files to USER.
122578741aaSmrg  -s            $stripprog installed files.
123578741aaSmrg  -t DIRECTORY  install into DIRECTORY.
124578741aaSmrg  -T            report an error if DSTFILE is a directory.
125145b7b3cSmrg
126145b7b3cSmrgEnvironment variables override the default commands:
127578741aaSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128578741aaSmrg  RMPROG STRIPPROG
129145b7b3cSmrg"
130145b7b3cSmrg
131145b7b3cSmrgwhile test $# -ne 0; do
132145b7b3cSmrg  case $1 in
133578741aaSmrg    -c) ;;
134578741aaSmrg
135578741aaSmrg    -C) copy_on_change=true;;
136145b7b3cSmrg
137578741aaSmrg    -d) dir_arg=true;;
138145b7b3cSmrg
139145b7b3cSmrg    -g) chgrpcmd="$chgrpprog $2"
140578741aaSmrg	shift;;
141145b7b3cSmrg
142145b7b3cSmrg    --help) echo "$usage"; exit $?;;
143145b7b3cSmrg
144145b7b3cSmrg    -m) mode=$2
145145b7b3cSmrg	case $mode in
146145b7b3cSmrg	  *' '* | *'	'* | *'
147145b7b3cSmrg'*	  | *'*'* | *'?'* | *'['*)
148145b7b3cSmrg	    echo "$0: invalid mode: $mode" >&2
149145b7b3cSmrg	    exit 1;;
150145b7b3cSmrg	esac
151578741aaSmrg	shift;;
152145b7b3cSmrg
153145b7b3cSmrg    -o) chowncmd="$chownprog $2"
154578741aaSmrg	shift;;
155145b7b3cSmrg
156578741aaSmrg    -s) stripcmd=$stripprog;;
157145b7b3cSmrg
158578741aaSmrg    -t) dst_arg=$2
159578741aaSmrg	shift;;
160145b7b3cSmrg
161578741aaSmrg    -T) no_target_directory=true;;
162145b7b3cSmrg
163145b7b3cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
164145b7b3cSmrg
165145b7b3cSmrg    --)	shift
166145b7b3cSmrg	break;;
167145b7b3cSmrg
168145b7b3cSmrg    -*)	echo "$0: invalid option: $1" >&2
169145b7b3cSmrg	exit 1;;
170145b7b3cSmrg
171145b7b3cSmrg    *)  break;;
172145b7b3cSmrg  esac
173578741aaSmrg  shift
174145b7b3cSmrgdone
175145b7b3cSmrg
176578741aaSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177145b7b3cSmrg  # When -d is used, all remaining arguments are directories to create.
178145b7b3cSmrg  # When -t is used, the destination is already specified.
179145b7b3cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180145b7b3cSmrg  for arg
181145b7b3cSmrg  do
182578741aaSmrg    if test -n "$dst_arg"; then
183145b7b3cSmrg      # $@ is not empty: it contains at least $arg.
184578741aaSmrg      set fnord "$@" "$dst_arg"
185145b7b3cSmrg      shift # fnord
186145b7b3cSmrg    fi
187145b7b3cSmrg    shift # arg
188578741aaSmrg    dst_arg=$arg
189145b7b3cSmrg  done
190145b7b3cSmrgfi
191145b7b3cSmrg
192145b7b3cSmrgif test $# -eq 0; then
193145b7b3cSmrg  if test -z "$dir_arg"; then
194145b7b3cSmrg    echo "$0: no input file specified." >&2
195145b7b3cSmrg    exit 1
196145b7b3cSmrg  fi
197145b7b3cSmrg  # It's OK to call `install-sh -d' without argument.
198145b7b3cSmrg  # This can happen when creating conditional directories.
199145b7b3cSmrg  exit 0
200145b7b3cSmrgfi
201145b7b3cSmrg
202145b7b3cSmrgif test -z "$dir_arg"; then
203145b7b3cSmrg  trap '(exit $?); exit' 1 2 13 15
204145b7b3cSmrg
205145b7b3cSmrg  # Set umask so as not to create temps with too-generous modes.
206145b7b3cSmrg  # However, 'strip' requires both read and write access to temps.
207145b7b3cSmrg  case $mode in
208145b7b3cSmrg    # Optimize common cases.
209145b7b3cSmrg    *644) cp_umask=133;;
210145b7b3cSmrg    *755) cp_umask=22;;
211145b7b3cSmrg
212145b7b3cSmrg    *[0-7])
213145b7b3cSmrg      if test -z "$stripcmd"; then
214145b7b3cSmrg	u_plus_rw=
215145b7b3cSmrg      else
216145b7b3cSmrg	u_plus_rw='% 200'
217145b7b3cSmrg      fi
218145b7b3cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
219145b7b3cSmrg    *)
220145b7b3cSmrg      if test -z "$stripcmd"; then
221145b7b3cSmrg	u_plus_rw=
222145b7b3cSmrg      else
223145b7b3cSmrg	u_plus_rw=,u+rw
224145b7b3cSmrg      fi
225145b7b3cSmrg      cp_umask=$mode$u_plus_rw;;
226145b7b3cSmrg  esac
227145b7b3cSmrgfi
228145b7b3cSmrg
229145b7b3cSmrgfor src
230145b7b3cSmrgdo
231145b7b3cSmrg  # Protect names starting with `-'.
232145b7b3cSmrg  case $src in
233578741aaSmrg    -*) src=./$src;;
234145b7b3cSmrg  esac
235145b7b3cSmrg
236145b7b3cSmrg  if test -n "$dir_arg"; then
237145b7b3cSmrg    dst=$src
238145b7b3cSmrg    dstdir=$dst
239145b7b3cSmrg    test -d "$dstdir"
240145b7b3cSmrg    dstdir_status=$?
241145b7b3cSmrg  else
242145b7b3cSmrg
243145b7b3cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
244145b7b3cSmrg    # might cause directories to be created, which would be especially bad
245145b7b3cSmrg    # if $src (and thus $dsttmp) contains '*'.
246145b7b3cSmrg    if test ! -f "$src" && test ! -d "$src"; then
247145b7b3cSmrg      echo "$0: $src does not exist." >&2
248145b7b3cSmrg      exit 1
249145b7b3cSmrg    fi
250145b7b3cSmrg
251578741aaSmrg    if test -z "$dst_arg"; then
252145b7b3cSmrg      echo "$0: no destination specified." >&2
253145b7b3cSmrg      exit 1
254145b7b3cSmrg    fi
255145b7b3cSmrg
256578741aaSmrg    dst=$dst_arg
257145b7b3cSmrg    # Protect names starting with `-'.
258145b7b3cSmrg    case $dst in
259578741aaSmrg      -*) dst=./$dst;;
260145b7b3cSmrg    esac
261145b7b3cSmrg
262145b7b3cSmrg    # If destination is a directory, append the input filename; won't work
263145b7b3cSmrg    # if double slashes aren't ignored.
264145b7b3cSmrg    if test -d "$dst"; then
265145b7b3cSmrg      if test -n "$no_target_directory"; then
266578741aaSmrg	echo "$0: $dst_arg: Is a directory" >&2
267145b7b3cSmrg	exit 1
268145b7b3cSmrg      fi
269145b7b3cSmrg      dstdir=$dst
270145b7b3cSmrg      dst=$dstdir/`basename "$src"`
271145b7b3cSmrg      dstdir_status=0
272145b7b3cSmrg    else
273145b7b3cSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
274145b7b3cSmrg      dstdir=`
275145b7b3cSmrg	(dirname "$dst") 2>/dev/null ||
276145b7b3cSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
277145b7b3cSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
278145b7b3cSmrg	     X"$dst" : 'X\(//\)$' \| \
279145b7b3cSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
280145b7b3cSmrg	echo X"$dst" |
281145b7b3cSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
282145b7b3cSmrg		   s//\1/
283145b7b3cSmrg		   q
284145b7b3cSmrg		 }
285145b7b3cSmrg		 /^X\(\/\/\)[^/].*/{
286145b7b3cSmrg		   s//\1/
287145b7b3cSmrg		   q
288145b7b3cSmrg		 }
289145b7b3cSmrg		 /^X\(\/\/\)$/{
290145b7b3cSmrg		   s//\1/
291145b7b3cSmrg		   q
292145b7b3cSmrg		 }
293145b7b3cSmrg		 /^X\(\/\).*/{
294145b7b3cSmrg		   s//\1/
295145b7b3cSmrg		   q
296145b7b3cSmrg		 }
297145b7b3cSmrg		 s/.*/./; q'
298145b7b3cSmrg      `
299145b7b3cSmrg
300145b7b3cSmrg      test -d "$dstdir"
301145b7b3cSmrg      dstdir_status=$?
302145b7b3cSmrg    fi
303145b7b3cSmrg  fi
304145b7b3cSmrg
305145b7b3cSmrg  obsolete_mkdir_used=false
306145b7b3cSmrg
307145b7b3cSmrg  if test $dstdir_status != 0; then
308145b7b3cSmrg    case $posix_mkdir in
309145b7b3cSmrg      '')
310145b7b3cSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
311145b7b3cSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
312145b7b3cSmrg	umask=`umask`
313145b7b3cSmrg	case $stripcmd.$umask in
314145b7b3cSmrg	  # Optimize common cases.
315145b7b3cSmrg	  *[2367][2367]) mkdir_umask=$umask;;
316145b7b3cSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
317145b7b3cSmrg
318145b7b3cSmrg	  *[0-7])
319145b7b3cSmrg	    mkdir_umask=`expr $umask + 22 \
320145b7b3cSmrg	      - $umask % 100 % 40 + $umask % 20 \
321145b7b3cSmrg	      - $umask % 10 % 4 + $umask % 2
322145b7b3cSmrg	    `;;
323145b7b3cSmrg	  *) mkdir_umask=$umask,go-w;;
324145b7b3cSmrg	esac
325145b7b3cSmrg
326145b7b3cSmrg	# With -d, create the new directory with the user-specified mode.
327145b7b3cSmrg	# Otherwise, rely on $mkdir_umask.
328145b7b3cSmrg	if test -n "$dir_arg"; then
329145b7b3cSmrg	  mkdir_mode=-m$mode
330145b7b3cSmrg	else
331145b7b3cSmrg	  mkdir_mode=
332145b7b3cSmrg	fi
333145b7b3cSmrg
334145b7b3cSmrg	posix_mkdir=false
335145b7b3cSmrg	case $umask in
336145b7b3cSmrg	  *[123567][0-7][0-7])
337145b7b3cSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
338145b7b3cSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
339145b7b3cSmrg	    ;;
340145b7b3cSmrg	  *)
341145b7b3cSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
342145b7b3cSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
343145b7b3cSmrg
344145b7b3cSmrg	    if (umask $mkdir_umask &&
345145b7b3cSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
346145b7b3cSmrg	    then
347145b7b3cSmrg	      if test -z "$dir_arg" || {
348145b7b3cSmrg		   # Check for POSIX incompatibilities with -m.
349145b7b3cSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
350145b7b3cSmrg		   # other-writeable bit of parent directory when it shouldn't.
351145b7b3cSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
352145b7b3cSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
353145b7b3cSmrg		   case $ls_ld_tmpdir in
354145b7b3cSmrg		     d????-?r-*) different_mode=700;;
355145b7b3cSmrg		     d????-?--*) different_mode=755;;
356145b7b3cSmrg		     *) false;;
357145b7b3cSmrg		   esac &&
358145b7b3cSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
359145b7b3cSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
360145b7b3cSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
361145b7b3cSmrg		   }
362145b7b3cSmrg		 }
363145b7b3cSmrg	      then posix_mkdir=:
364145b7b3cSmrg	      fi
365145b7b3cSmrg	      rmdir "$tmpdir/d" "$tmpdir"
366145b7b3cSmrg	    else
367145b7b3cSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
368145b7b3cSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
369145b7b3cSmrg	    fi
370145b7b3cSmrg	    trap '' 0;;
371145b7b3cSmrg	esac;;
372145b7b3cSmrg    esac
373145b7b3cSmrg
374145b7b3cSmrg    if
375145b7b3cSmrg      $posix_mkdir && (
376145b7b3cSmrg	umask $mkdir_umask &&
377145b7b3cSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
378145b7b3cSmrg      )
379145b7b3cSmrg    then :
380145b7b3cSmrg    else
381145b7b3cSmrg
382145b7b3cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
383145b7b3cSmrg      # or it failed possibly due to a race condition.  Create the
384145b7b3cSmrg      # directory the slow way, step by step, checking for races as we go.
385145b7b3cSmrg
386145b7b3cSmrg      case $dstdir in
387578741aaSmrg	/*) prefix='/';;
388578741aaSmrg	-*) prefix='./';;
389578741aaSmrg	*)  prefix='';;
390145b7b3cSmrg      esac
391145b7b3cSmrg
392578741aaSmrg      eval "$initialize_posix_glob"
393145b7b3cSmrg
394145b7b3cSmrg      oIFS=$IFS
395145b7b3cSmrg      IFS=/
396578741aaSmrg      $posix_glob set -f
397145b7b3cSmrg      set fnord $dstdir
398145b7b3cSmrg      shift
399578741aaSmrg      $posix_glob set +f
400145b7b3cSmrg      IFS=$oIFS
401145b7b3cSmrg
402145b7b3cSmrg      prefixes=
403145b7b3cSmrg
404145b7b3cSmrg      for d
405145b7b3cSmrg      do
406145b7b3cSmrg	test -z "$d" && continue
407145b7b3cSmrg
408145b7b3cSmrg	prefix=$prefix$d
409145b7b3cSmrg	if test -d "$prefix"; then
410145b7b3cSmrg	  prefixes=
411145b7b3cSmrg	else
412145b7b3cSmrg	  if $posix_mkdir; then
413145b7b3cSmrg	    (umask=$mkdir_umask &&
414145b7b3cSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
415145b7b3cSmrg	    # Don't fail if two instances are running concurrently.
416145b7b3cSmrg	    test -d "$prefix" || exit 1
417145b7b3cSmrg	  else
418145b7b3cSmrg	    case $prefix in
419145b7b3cSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
420145b7b3cSmrg	      *) qprefix=$prefix;;
421145b7b3cSmrg	    esac
422145b7b3cSmrg	    prefixes="$prefixes '$qprefix'"
423145b7b3cSmrg	  fi
424145b7b3cSmrg	fi
425145b7b3cSmrg	prefix=$prefix/
426145b7b3cSmrg      done
427145b7b3cSmrg
428145b7b3cSmrg      if test -n "$prefixes"; then
429145b7b3cSmrg	# Don't fail if two instances are running concurrently.
430145b7b3cSmrg	(umask $mkdir_umask &&
431145b7b3cSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
432145b7b3cSmrg	  test -d "$dstdir" || exit 1
433145b7b3cSmrg	obsolete_mkdir_used=true
434145b7b3cSmrg      fi
435145b7b3cSmrg    fi
436145b7b3cSmrg  fi
437145b7b3cSmrg
438145b7b3cSmrg  if test -n "$dir_arg"; then
439145b7b3cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
440145b7b3cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
441145b7b3cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
442145b7b3cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
443145b7b3cSmrg  else
444145b7b3cSmrg
445145b7b3cSmrg    # Make a couple of temp file names in the proper directory.
446145b7b3cSmrg    dsttmp=$dstdir/_inst.$$_
447145b7b3cSmrg    rmtmp=$dstdir/_rm.$$_
448145b7b3cSmrg
449145b7b3cSmrg    # Trap to clean up those temp files at exit.
450145b7b3cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
451145b7b3cSmrg
452145b7b3cSmrg    # Copy the file name to the temp name.
453145b7b3cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
454145b7b3cSmrg
455145b7b3cSmrg    # and set any options; do chmod last to preserve setuid bits.
456145b7b3cSmrg    #
457145b7b3cSmrg    # If any of these fail, we abort the whole thing.  If we want to
458145b7b3cSmrg    # ignore errors from any of these, just make sure not to ignore
459145b7b3cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
460145b7b3cSmrg    #
461578741aaSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
462578741aaSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
463578741aaSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
464578741aaSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
465578741aaSmrg
466578741aaSmrg    # If -C, don't bother to copy if it wouldn't change the file.
467578741aaSmrg    if $copy_on_change &&
468578741aaSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
469578741aaSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
470578741aaSmrg
471578741aaSmrg       eval "$initialize_posix_glob" &&
472578741aaSmrg       $posix_glob set -f &&
473578741aaSmrg       set X $old && old=:$2:$4:$5:$6 &&
474578741aaSmrg       set X $new && new=:$2:$4:$5:$6 &&
475578741aaSmrg       $posix_glob set +f &&
476578741aaSmrg
477578741aaSmrg       test "$old" = "$new" &&
478578741aaSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
479578741aaSmrg    then
480578741aaSmrg      rm -f "$dsttmp"
481578741aaSmrg    else
482578741aaSmrg      # Rename the file to the real destination.
483578741aaSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
484578741aaSmrg
485578741aaSmrg      # The rename failed, perhaps because mv can't rename something else
486578741aaSmrg      # to itself, or perhaps because mv is so ancient that it does not
487578741aaSmrg      # support -f.
488578741aaSmrg      {
489578741aaSmrg	# Now remove or move aside any old file at destination location.
490578741aaSmrg	# We try this two ways since rm can't unlink itself on some
491578741aaSmrg	# systems and the destination file might be busy for other
492578741aaSmrg	# reasons.  In this case, the final cleanup might fail but the new
493578741aaSmrg	# file should still install successfully.
494578741aaSmrg	{
495578741aaSmrg	  test ! -f "$dst" ||
496578741aaSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
497578741aaSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
498578741aaSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
499578741aaSmrg	  } ||
500578741aaSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
501578741aaSmrg	    (exit 1); exit 1
502578741aaSmrg	  }
503578741aaSmrg	} &&
504578741aaSmrg
505578741aaSmrg	# Now rename the file to the real destination.
506578741aaSmrg	$doit $mvcmd "$dsttmp" "$dst"
507578741aaSmrg      }
508578741aaSmrg    fi || exit 1
509145b7b3cSmrg
510145b7b3cSmrg    trap '' 0
511145b7b3cSmrg  fi
512145b7b3cSmrgdone
513145b7b3cSmrg
514145b7b3cSmrg# Local variables:
515145b7b3cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
516145b7b3cSmrg# time-stamp-start: "scriptversion="
517145b7b3cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
518578741aaSmrg# time-stamp-time-zone: "UTC"
519578741aaSmrg# time-stamp-end: "; # UTC"
520145b7b3cSmrg# End:
521