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