install-sh revision ac92798b
1a966c04fSmrg#!/bin/sh
2a966c04fSmrg# install - install a program, script, or datafile
3a966c04fSmrg
4ac92798bSmrgscriptversion=2011-11-20.07; # UTC
5a966c04fSmrg
6a966c04fSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a966c04fSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a966c04fSmrg# following copyright and license.
9a966c04fSmrg#
10a966c04fSmrg# Copyright (C) 1994 X Consortium
11a966c04fSmrg#
12a966c04fSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a966c04fSmrg# of this software and associated documentation files (the "Software"), to
14a966c04fSmrg# deal in the Software without restriction, including without limitation the
15a966c04fSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a966c04fSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a966c04fSmrg# furnished to do so, subject to the following conditions:
18a966c04fSmrg#
19a966c04fSmrg# The above copyright notice and this permission notice shall be included in
20a966c04fSmrg# all copies or substantial portions of the Software.
21a966c04fSmrg#
22a966c04fSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a966c04fSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a966c04fSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a966c04fSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a966c04fSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a966c04fSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a966c04fSmrg#
29a966c04fSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a966c04fSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a966c04fSmrg# ings in this Software without prior written authorization from the X Consor-
32a966c04fSmrg# tium.
33a966c04fSmrg#
34a966c04fSmrg#
35a966c04fSmrg# FSF changes to this file are in the public domain.
36a966c04fSmrg#
37a966c04fSmrg# Calling this script install-sh is preferred over install.sh, to prevent
38ac92798bSmrg# 'make' implicit rules from creating a file called install from it
39a966c04fSmrg# when there is no Makefile.
40a966c04fSmrg#
41a966c04fSmrg# This script is compatible with the BSD install script, but was written
42a966c04fSmrg# from scratch.
43a966c04fSmrg
44a966c04fSmrgnl='
45a966c04fSmrg'
46a966c04fSmrgIFS=" ""	$nl"
47a966c04fSmrg
48a966c04fSmrg# set DOITPROG to echo to test this script
49a966c04fSmrg
50a966c04fSmrg# Don't use :- since 4.3BSD and earlier shells don't like it.
512e2dd055Smrgdoit=${DOITPROG-}
52a966c04fSmrgif test -z "$doit"; then
53a966c04fSmrg  doit_exec=exec
54a966c04fSmrgelse
55a966c04fSmrg  doit_exec=$doit
56a966c04fSmrgfi
57a966c04fSmrg
58a966c04fSmrg# Put in absolute file names if you don't have them in your path;
59a966c04fSmrg# or use environment vars.
60a966c04fSmrg
612e2dd055Smrgchgrpprog=${CHGRPPROG-chgrp}
622e2dd055Smrgchmodprog=${CHMODPROG-chmod}
632e2dd055Smrgchownprog=${CHOWNPROG-chown}
642e2dd055Smrgcmpprog=${CMPPROG-cmp}
652e2dd055Smrgcpprog=${CPPROG-cp}
662e2dd055Smrgmkdirprog=${MKDIRPROG-mkdir}
672e2dd055Smrgmvprog=${MVPROG-mv}
682e2dd055Smrgrmprog=${RMPROG-rm}
692e2dd055Smrgstripprog=${STRIPPROG-strip}
702e2dd055Smrg
712e2dd055Smrgposix_glob='?'
722e2dd055Smrginitialize_posix_glob='
732e2dd055Smrg  test "$posix_glob" != "?" || {
742e2dd055Smrg    if (set -f) 2>/dev/null; then
752e2dd055Smrg      posix_glob=
762e2dd055Smrg    else
772e2dd055Smrg      posix_glob=:
782e2dd055Smrg    fi
792e2dd055Smrg  }
802e2dd055Smrg'
81a966c04fSmrg
82a966c04fSmrgposix_mkdir=
83a966c04fSmrg
84a966c04fSmrg# Desired mode of installed file.
85a966c04fSmrgmode=0755
86a966c04fSmrg
872e2dd055Smrgchgrpcmd=
88a966c04fSmrgchmodcmd=$chmodprog
89a966c04fSmrgchowncmd=
902e2dd055Smrgmvcmd=$mvprog
91a966c04fSmrgrmcmd="$rmprog -f"
922e2dd055Smrgstripcmd=
932e2dd055Smrg
94a966c04fSmrgsrc=
95a966c04fSmrgdst=
96a966c04fSmrgdir_arg=
972e2dd055Smrgdst_arg=
982e2dd055Smrg
992e2dd055Smrgcopy_on_change=false
100a966c04fSmrgno_target_directory=
101a966c04fSmrg
1022e2dd055Smrgusage="\
1032e2dd055SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104a966c04fSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105a966c04fSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106a966c04fSmrg   or: $0 [OPTION]... -d DIRECTORIES...
107a966c04fSmrg
108a966c04fSmrgIn the 1st form, copy SRCFILE to DSTFILE.
109a966c04fSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110a966c04fSmrgIn the 4th, create DIRECTORIES.
111a966c04fSmrg
112a966c04fSmrgOptions:
1132e2dd055Smrg     --help     display this help and exit.
1142e2dd055Smrg     --version  display version info and exit.
1152e2dd055Smrg
1162e2dd055Smrg  -c            (ignored)
1172e2dd055Smrg  -C            install only if different (preserve the last data modification time)
1182e2dd055Smrg  -d            create directories instead of installing files.
1192e2dd055Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1202e2dd055Smrg  -m MODE       $chmodprog installed files to MODE.
1212e2dd055Smrg  -o USER       $chownprog installed files to USER.
1222e2dd055Smrg  -s            $stripprog installed files.
1232e2dd055Smrg  -t DIRECTORY  install into DIRECTORY.
1242e2dd055Smrg  -T            report an error if DSTFILE is a directory.
125a966c04fSmrg
126a966c04fSmrgEnvironment variables override the default commands:
1272e2dd055Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1282e2dd055Smrg  RMPROG STRIPPROG
129a966c04fSmrg"
130a966c04fSmrg
131a966c04fSmrgwhile test $# -ne 0; do
132a966c04fSmrg  case $1 in
1332e2dd055Smrg    -c) ;;
1342e2dd055Smrg
1352e2dd055Smrg    -C) copy_on_change=true;;
136a966c04fSmrg
1372e2dd055Smrg    -d) dir_arg=true;;
138a966c04fSmrg
139a966c04fSmrg    -g) chgrpcmd="$chgrpprog $2"
1402e2dd055Smrg	shift;;
141a966c04fSmrg
142a966c04fSmrg    --help) echo "$usage"; exit $?;;
143a966c04fSmrg
144a966c04fSmrg    -m) mode=$2
145a966c04fSmrg	case $mode in
146a966c04fSmrg	  *' '* | *'	'* | *'
147a966c04fSmrg'*	  | *'*'* | *'?'* | *'['*)
148a966c04fSmrg	    echo "$0: invalid mode: $mode" >&2
149a966c04fSmrg	    exit 1;;
150a966c04fSmrg	esac
1512e2dd055Smrg	shift;;
152a966c04fSmrg
153a966c04fSmrg    -o) chowncmd="$chownprog $2"
1542e2dd055Smrg	shift;;
155a966c04fSmrg
1562e2dd055Smrg    -s) stripcmd=$stripprog;;
157a966c04fSmrg
1582e2dd055Smrg    -t) dst_arg=$2
159ac92798bSmrg	# Protect names problematic for 'test' and other utilities.
160ac92798bSmrg	case $dst_arg in
161ac92798bSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162ac92798bSmrg	esac
1632e2dd055Smrg	shift;;
164a966c04fSmrg
1652e2dd055Smrg    -T) no_target_directory=true;;
166a966c04fSmrg
167a966c04fSmrg    --version) echo "$0 $scriptversion"; exit $?;;
168a966c04fSmrg
169a966c04fSmrg    --)	shift
170a966c04fSmrg	break;;
171a966c04fSmrg
172a966c04fSmrg    -*)	echo "$0: invalid option: $1" >&2
173a966c04fSmrg	exit 1;;
174a966c04fSmrg
175a966c04fSmrg    *)  break;;
176a966c04fSmrg  esac
1772e2dd055Smrg  shift
178a966c04fSmrgdone
179a966c04fSmrg
1802e2dd055Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181a966c04fSmrg  # When -d is used, all remaining arguments are directories to create.
182a966c04fSmrg  # When -t is used, the destination is already specified.
183a966c04fSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184a966c04fSmrg  for arg
185a966c04fSmrg  do
1862e2dd055Smrg    if test -n "$dst_arg"; then
187a966c04fSmrg      # $@ is not empty: it contains at least $arg.
1882e2dd055Smrg      set fnord "$@" "$dst_arg"
189a966c04fSmrg      shift # fnord
190a966c04fSmrg    fi
191a966c04fSmrg    shift # arg
1922e2dd055Smrg    dst_arg=$arg
193ac92798bSmrg    # Protect names problematic for 'test' and other utilities.
194ac92798bSmrg    case $dst_arg in
195ac92798bSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196ac92798bSmrg    esac
197a966c04fSmrg  done
198a966c04fSmrgfi
199a966c04fSmrg
200a966c04fSmrgif test $# -eq 0; then
201a966c04fSmrg  if test -z "$dir_arg"; then
202a966c04fSmrg    echo "$0: no input file specified." >&2
203a966c04fSmrg    exit 1
204a966c04fSmrg  fi
205ac92798bSmrg  # It's OK to call 'install-sh -d' without argument.
206a966c04fSmrg  # This can happen when creating conditional directories.
207a966c04fSmrg  exit 0
208a966c04fSmrgfi
209a966c04fSmrg
210a966c04fSmrgif test -z "$dir_arg"; then
211ac92798bSmrg  do_exit='(exit $ret); exit $ret'
212ac92798bSmrg  trap "ret=129; $do_exit" 1
213ac92798bSmrg  trap "ret=130; $do_exit" 2
214ac92798bSmrg  trap "ret=141; $do_exit" 13
215ac92798bSmrg  trap "ret=143; $do_exit" 15
216a966c04fSmrg
217a966c04fSmrg  # Set umask so as not to create temps with too-generous modes.
218a966c04fSmrg  # However, 'strip' requires both read and write access to temps.
219a966c04fSmrg  case $mode in
220a966c04fSmrg    # Optimize common cases.
221a966c04fSmrg    *644) cp_umask=133;;
222a966c04fSmrg    *755) cp_umask=22;;
223a966c04fSmrg
224a966c04fSmrg    *[0-7])
225a966c04fSmrg      if test -z "$stripcmd"; then
226a966c04fSmrg	u_plus_rw=
227a966c04fSmrg      else
228a966c04fSmrg	u_plus_rw='% 200'
229a966c04fSmrg      fi
230a966c04fSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231a966c04fSmrg    *)
232a966c04fSmrg      if test -z "$stripcmd"; then
233a966c04fSmrg	u_plus_rw=
234a966c04fSmrg      else
235a966c04fSmrg	u_plus_rw=,u+rw
236a966c04fSmrg      fi
237a966c04fSmrg      cp_umask=$mode$u_plus_rw;;
238a966c04fSmrg  esac
239a966c04fSmrgfi
240a966c04fSmrg
241a966c04fSmrgfor src
242a966c04fSmrgdo
243ac92798bSmrg  # Protect names problematic for 'test' and other utilities.
244a966c04fSmrg  case $src in
245ac92798bSmrg    -* | [=\(\)!]) src=./$src;;
246a966c04fSmrg  esac
247a966c04fSmrg
248a966c04fSmrg  if test -n "$dir_arg"; then
249a966c04fSmrg    dst=$src
250a966c04fSmrg    dstdir=$dst
251a966c04fSmrg    test -d "$dstdir"
252a966c04fSmrg    dstdir_status=$?
253a966c04fSmrg  else
254a966c04fSmrg
255a966c04fSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256a966c04fSmrg    # might cause directories to be created, which would be especially bad
257a966c04fSmrg    # if $src (and thus $dsttmp) contains '*'.
258a966c04fSmrg    if test ! -f "$src" && test ! -d "$src"; then
259a966c04fSmrg      echo "$0: $src does not exist." >&2
260a966c04fSmrg      exit 1
261a966c04fSmrg    fi
262a966c04fSmrg
2632e2dd055Smrg    if test -z "$dst_arg"; then
264a966c04fSmrg      echo "$0: no destination specified." >&2
265a966c04fSmrg      exit 1
266a966c04fSmrg    fi
2672e2dd055Smrg    dst=$dst_arg
268a966c04fSmrg
269a966c04fSmrg    # If destination is a directory, append the input filename; won't work
270a966c04fSmrg    # if double slashes aren't ignored.
271a966c04fSmrg    if test -d "$dst"; then
272a966c04fSmrg      if test -n "$no_target_directory"; then
2732e2dd055Smrg	echo "$0: $dst_arg: Is a directory" >&2
274a966c04fSmrg	exit 1
275a966c04fSmrg      fi
276a966c04fSmrg      dstdir=$dst
277a966c04fSmrg      dst=$dstdir/`basename "$src"`
278a966c04fSmrg      dstdir_status=0
279a966c04fSmrg    else
280a966c04fSmrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281a966c04fSmrg      dstdir=`
282a966c04fSmrg	(dirname "$dst") 2>/dev/null ||
283a966c04fSmrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284a966c04fSmrg	     X"$dst" : 'X\(//\)[^/]' \| \
285a966c04fSmrg	     X"$dst" : 'X\(//\)$' \| \
286a966c04fSmrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287a966c04fSmrg	echo X"$dst" |
288a966c04fSmrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289a966c04fSmrg		   s//\1/
290a966c04fSmrg		   q
291a966c04fSmrg		 }
292a966c04fSmrg		 /^X\(\/\/\)[^/].*/{
293a966c04fSmrg		   s//\1/
294a966c04fSmrg		   q
295a966c04fSmrg		 }
296a966c04fSmrg		 /^X\(\/\/\)$/{
297a966c04fSmrg		   s//\1/
298a966c04fSmrg		   q
299a966c04fSmrg		 }
300a966c04fSmrg		 /^X\(\/\).*/{
301a966c04fSmrg		   s//\1/
302a966c04fSmrg		   q
303a966c04fSmrg		 }
304a966c04fSmrg		 s/.*/./; q'
305a966c04fSmrg      `
306a966c04fSmrg
307a966c04fSmrg      test -d "$dstdir"
308a966c04fSmrg      dstdir_status=$?
309a966c04fSmrg    fi
310a966c04fSmrg  fi
311a966c04fSmrg
312a966c04fSmrg  obsolete_mkdir_used=false
313a966c04fSmrg
314a966c04fSmrg  if test $dstdir_status != 0; then
315a966c04fSmrg    case $posix_mkdir in
316a966c04fSmrg      '')
317a966c04fSmrg	# Create intermediate dirs using mode 755 as modified by the umask.
318a966c04fSmrg	# This is like FreeBSD 'install' as of 1997-10-28.
319a966c04fSmrg	umask=`umask`
320a966c04fSmrg	case $stripcmd.$umask in
321a966c04fSmrg	  # Optimize common cases.
322a966c04fSmrg	  *[2367][2367]) mkdir_umask=$umask;;
323a966c04fSmrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324a966c04fSmrg
325a966c04fSmrg	  *[0-7])
326a966c04fSmrg	    mkdir_umask=`expr $umask + 22 \
327a966c04fSmrg	      - $umask % 100 % 40 + $umask % 20 \
328a966c04fSmrg	      - $umask % 10 % 4 + $umask % 2
329a966c04fSmrg	    `;;
330a966c04fSmrg	  *) mkdir_umask=$umask,go-w;;
331a966c04fSmrg	esac
332a966c04fSmrg
333a966c04fSmrg	# With -d, create the new directory with the user-specified mode.
334a966c04fSmrg	# Otherwise, rely on $mkdir_umask.
335a966c04fSmrg	if test -n "$dir_arg"; then
336a966c04fSmrg	  mkdir_mode=-m$mode
337a966c04fSmrg	else
338a966c04fSmrg	  mkdir_mode=
339a966c04fSmrg	fi
340a966c04fSmrg
341a966c04fSmrg	posix_mkdir=false
342a966c04fSmrg	case $umask in
343a966c04fSmrg	  *[123567][0-7][0-7])
344a966c04fSmrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345a966c04fSmrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346a966c04fSmrg	    ;;
347a966c04fSmrg	  *)
348a966c04fSmrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349a966c04fSmrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350a966c04fSmrg
351a966c04fSmrg	    if (umask $mkdir_umask &&
352a966c04fSmrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353a966c04fSmrg	    then
354a966c04fSmrg	      if test -z "$dir_arg" || {
355a966c04fSmrg		   # Check for POSIX incompatibilities with -m.
356a966c04fSmrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357ac92798bSmrg		   # other-writable bit of parent directory when it shouldn't.
358a966c04fSmrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359a966c04fSmrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360a966c04fSmrg		   case $ls_ld_tmpdir in
361a966c04fSmrg		     d????-?r-*) different_mode=700;;
362a966c04fSmrg		     d????-?--*) different_mode=755;;
363a966c04fSmrg		     *) false;;
364a966c04fSmrg		   esac &&
365a966c04fSmrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366a966c04fSmrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367a966c04fSmrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368a966c04fSmrg		   }
369a966c04fSmrg		 }
370a966c04fSmrg	      then posix_mkdir=:
371a966c04fSmrg	      fi
372a966c04fSmrg	      rmdir "$tmpdir/d" "$tmpdir"
373a966c04fSmrg	    else
374a966c04fSmrg	      # Remove any dirs left behind by ancient mkdir implementations.
375a966c04fSmrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376a966c04fSmrg	    fi
377a966c04fSmrg	    trap '' 0;;
378a966c04fSmrg	esac;;
379a966c04fSmrg    esac
380a966c04fSmrg
381a966c04fSmrg    if
382a966c04fSmrg      $posix_mkdir && (
383a966c04fSmrg	umask $mkdir_umask &&
384a966c04fSmrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385a966c04fSmrg      )
386a966c04fSmrg    then :
387a966c04fSmrg    else
388a966c04fSmrg
389a966c04fSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390a966c04fSmrg      # or it failed possibly due to a race condition.  Create the
391a966c04fSmrg      # directory the slow way, step by step, checking for races as we go.
392a966c04fSmrg
393a966c04fSmrg      case $dstdir in
3942e2dd055Smrg	/*) prefix='/';;
395ac92798bSmrg	[-=\(\)!]*) prefix='./';;
3962e2dd055Smrg	*)  prefix='';;
397a966c04fSmrg      esac
398a966c04fSmrg
3992e2dd055Smrg      eval "$initialize_posix_glob"
400a966c04fSmrg
401a966c04fSmrg      oIFS=$IFS
402a966c04fSmrg      IFS=/
4032e2dd055Smrg      $posix_glob set -f
404a966c04fSmrg      set fnord $dstdir
405a966c04fSmrg      shift
4062e2dd055Smrg      $posix_glob set +f
407a966c04fSmrg      IFS=$oIFS
408a966c04fSmrg
409a966c04fSmrg      prefixes=
410a966c04fSmrg
411a966c04fSmrg      for d
412a966c04fSmrg      do
413ac92798bSmrg	test X"$d" = X && continue
414a966c04fSmrg
415a966c04fSmrg	prefix=$prefix$d
416a966c04fSmrg	if test -d "$prefix"; then
417a966c04fSmrg	  prefixes=
418a966c04fSmrg	else
419a966c04fSmrg	  if $posix_mkdir; then
420a966c04fSmrg	    (umask=$mkdir_umask &&
421a966c04fSmrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422a966c04fSmrg	    # Don't fail if two instances are running concurrently.
423a966c04fSmrg	    test -d "$prefix" || exit 1
424a966c04fSmrg	  else
425a966c04fSmrg	    case $prefix in
426a966c04fSmrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427a966c04fSmrg	      *) qprefix=$prefix;;
428a966c04fSmrg	    esac
429a966c04fSmrg	    prefixes="$prefixes '$qprefix'"
430a966c04fSmrg	  fi
431a966c04fSmrg	fi
432a966c04fSmrg	prefix=$prefix/
433a966c04fSmrg      done
434a966c04fSmrg
435a966c04fSmrg      if test -n "$prefixes"; then
436a966c04fSmrg	# Don't fail if two instances are running concurrently.
437a966c04fSmrg	(umask $mkdir_umask &&
438a966c04fSmrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439a966c04fSmrg	  test -d "$dstdir" || exit 1
440a966c04fSmrg	obsolete_mkdir_used=true
441a966c04fSmrg      fi
442a966c04fSmrg    fi
443a966c04fSmrg  fi
444a966c04fSmrg
445a966c04fSmrg  if test -n "$dir_arg"; then
446a966c04fSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447a966c04fSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448a966c04fSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449a966c04fSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450a966c04fSmrg  else
451a966c04fSmrg
452a966c04fSmrg    # Make a couple of temp file names in the proper directory.
453a966c04fSmrg    dsttmp=$dstdir/_inst.$$_
454a966c04fSmrg    rmtmp=$dstdir/_rm.$$_
455a966c04fSmrg
456a966c04fSmrg    # Trap to clean up those temp files at exit.
457a966c04fSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458a966c04fSmrg
459a966c04fSmrg    # Copy the file name to the temp name.
460a966c04fSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461a966c04fSmrg
462a966c04fSmrg    # and set any options; do chmod last to preserve setuid bits.
463a966c04fSmrg    #
464a966c04fSmrg    # If any of these fail, we abort the whole thing.  If we want to
465a966c04fSmrg    # ignore errors from any of these, just make sure not to ignore
466a966c04fSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467a966c04fSmrg    #
4682e2dd055Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4692e2dd055Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4702e2dd055Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4712e2dd055Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4722e2dd055Smrg
4732e2dd055Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4742e2dd055Smrg    if $copy_on_change &&
4752e2dd055Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
4762e2dd055Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
4772e2dd055Smrg
4782e2dd055Smrg       eval "$initialize_posix_glob" &&
4792e2dd055Smrg       $posix_glob set -f &&
4802e2dd055Smrg       set X $old && old=:$2:$4:$5:$6 &&
4812e2dd055Smrg       set X $new && new=:$2:$4:$5:$6 &&
4822e2dd055Smrg       $posix_glob set +f &&
4832e2dd055Smrg
4842e2dd055Smrg       test "$old" = "$new" &&
4852e2dd055Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4862e2dd055Smrg    then
4872e2dd055Smrg      rm -f "$dsttmp"
4882e2dd055Smrg    else
4892e2dd055Smrg      # Rename the file to the real destination.
4902e2dd055Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4912e2dd055Smrg
4922e2dd055Smrg      # The rename failed, perhaps because mv can't rename something else
4932e2dd055Smrg      # to itself, or perhaps because mv is so ancient that it does not
4942e2dd055Smrg      # support -f.
4952e2dd055Smrg      {
4962e2dd055Smrg	# Now remove or move aside any old file at destination location.
4972e2dd055Smrg	# We try this two ways since rm can't unlink itself on some
4982e2dd055Smrg	# systems and the destination file might be busy for other
4992e2dd055Smrg	# reasons.  In this case, the final cleanup might fail but the new
5002e2dd055Smrg	# file should still install successfully.
5012e2dd055Smrg	{
5022e2dd055Smrg	  test ! -f "$dst" ||
5032e2dd055Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
5042e2dd055Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
5052e2dd055Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
5062e2dd055Smrg	  } ||
5072e2dd055Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
5082e2dd055Smrg	    (exit 1); exit 1
5092e2dd055Smrg	  }
5102e2dd055Smrg	} &&
5112e2dd055Smrg
5122e2dd055Smrg	# Now rename the file to the real destination.
5132e2dd055Smrg	$doit $mvcmd "$dsttmp" "$dst"
5142e2dd055Smrg      }
5152e2dd055Smrg    fi || exit 1
516a966c04fSmrg
517a966c04fSmrg    trap '' 0
518a966c04fSmrg  fi
519a966c04fSmrgdone
520a966c04fSmrg
521a966c04fSmrg# Local variables:
522a966c04fSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523a966c04fSmrg# time-stamp-start: "scriptversion="
524a966c04fSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5252e2dd055Smrg# time-stamp-time-zone: "UTC"
5262e2dd055Smrg# time-stamp-end: "; # UTC"
527a966c04fSmrg# End:
528