install-sh revision d9dcd5ae
1191cded7Smrg#!/bin/sh
2191cded7Smrg# install - install a program, script, or datafile
3191cded7Smrg
4d9dcd5aeSmrgscriptversion=2011-11-20.07; # UTC
5191cded7Smrg
6191cded7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7191cded7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8191cded7Smrg# following copyright and license.
9191cded7Smrg#
10191cded7Smrg# Copyright (C) 1994 X Consortium
11191cded7Smrg#
12191cded7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13191cded7Smrg# of this software and associated documentation files (the "Software"), to
14191cded7Smrg# deal in the Software without restriction, including without limitation the
15191cded7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16191cded7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17191cded7Smrg# furnished to do so, subject to the following conditions:
18191cded7Smrg#
19191cded7Smrg# The above copyright notice and this permission notice shall be included in
20191cded7Smrg# all copies or substantial portions of the Software.
21191cded7Smrg#
22191cded7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23191cded7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24191cded7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25191cded7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26191cded7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27191cded7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28191cded7Smrg#
29191cded7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30191cded7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31191cded7Smrg# ings in this Software without prior written authorization from the X Consor-
32191cded7Smrg# tium.
33191cded7Smrg#
34191cded7Smrg#
35191cded7Smrg# FSF changes to this file are in the public domain.
36191cded7Smrg#
37191cded7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38d9dcd5aeSmrg# 'make' implicit rules from creating a file called install from it
39191cded7Smrg# when there is no Makefile.
40191cded7Smrg#
41191cded7Smrg# This script is compatible with the BSD install script, but was written
42b290cf36Smrg# from scratch.
43b290cf36Smrg
44b290cf36Smrgnl='
45b290cf36Smrg'
46b290cf36SmrgIFS=" ""	$nl"
47191cded7Smrg
48191cded7Smrg# set DOITPROG to echo to test this script
49191cded7Smrg
50191cded7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51b290cf36Smrgdoit=${DOITPROG-}
52b290cf36Smrgif test -z "$doit"; then
53b290cf36Smrg  doit_exec=exec
54b290cf36Smrgelse
55b290cf36Smrg  doit_exec=$doit
56b290cf36Smrgfi
57191cded7Smrg
58b290cf36Smrg# Put in absolute file names if you don't have them in your path;
59b290cf36Smrg# or use environment vars.
60b290cf36Smrg
61b290cf36Smrgchgrpprog=${CHGRPPROG-chgrp}
62b290cf36Smrgchmodprog=${CHMODPROG-chmod}
63b290cf36Smrgchownprog=${CHOWNPROG-chown}
64b290cf36Smrgcmpprog=${CMPPROG-cmp}
65b290cf36Smrgcpprog=${CPPROG-cp}
66b290cf36Smrgmkdirprog=${MKDIRPROG-mkdir}
67b290cf36Smrgmvprog=${MVPROG-mv}
68b290cf36Smrgrmprog=${RMPROG-rm}
69b290cf36Smrgstripprog=${STRIPPROG-strip}
70b290cf36Smrg
71b290cf36Smrgposix_glob='?'
72b290cf36Smrginitialize_posix_glob='
73b290cf36Smrg  test "$posix_glob" != "?" || {
74b290cf36Smrg    if (set -f) 2>/dev/null; then
75b290cf36Smrg      posix_glob=
76b290cf36Smrg    else
77b290cf36Smrg      posix_glob=:
78b290cf36Smrg    fi
79b290cf36Smrg  }
80b290cf36Smrg'
81191cded7Smrg
82b290cf36Smrgposix_mkdir=
83b290cf36Smrg
84b290cf36Smrg# Desired mode of installed file.
85b290cf36Smrgmode=0755
86191cded7Smrg
87191cded7Smrgchgrpcmd=
88b290cf36Smrgchmodcmd=$chmodprog
89b290cf36Smrgchowncmd=
90b290cf36Smrgmvcmd=$mvprog
91191cded7Smrgrmcmd="$rmprog -f"
92b290cf36Smrgstripcmd=
93b290cf36Smrg
94191cded7Smrgsrc=
95191cded7Smrgdst=
96191cded7Smrgdir_arg=
97b290cf36Smrgdst_arg=
98b290cf36Smrg
99b290cf36Smrgcopy_on_change=false
100191cded7Smrgno_target_directory=
101191cded7Smrg
102b290cf36Smrgusage="\
103b290cf36SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104191cded7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105191cded7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106191cded7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107191cded7Smrg
108191cded7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109191cded7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110191cded7SmrgIn the 4th, create DIRECTORIES.
111191cded7Smrg
112191cded7SmrgOptions:
113b290cf36Smrg     --help     display this help and exit.
114b290cf36Smrg     --version  display version info and exit.
115b290cf36Smrg
116b290cf36Smrg  -c            (ignored)
117b290cf36Smrg  -C            install only if different (preserve the last data modification time)
118b290cf36Smrg  -d            create directories instead of installing files.
119b290cf36Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120b290cf36Smrg  -m MODE       $chmodprog installed files to MODE.
121b290cf36Smrg  -o USER       $chownprog installed files to USER.
122b290cf36Smrg  -s            $stripprog installed files.
123b290cf36Smrg  -t DIRECTORY  install into DIRECTORY.
124b290cf36Smrg  -T            report an error if DSTFILE is a directory.
125191cded7Smrg
126191cded7SmrgEnvironment variables override the default commands:
127b290cf36Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128b290cf36Smrg  RMPROG STRIPPROG
129191cded7Smrg"
130191cded7Smrg
131b290cf36Smrgwhile test $# -ne 0; do
132191cded7Smrg  case $1 in
133b290cf36Smrg    -c) ;;
134b290cf36Smrg
135b290cf36Smrg    -C) copy_on_change=true;;
136191cded7Smrg
137b290cf36Smrg    -d) dir_arg=true;;
138191cded7Smrg
139191cded7Smrg    -g) chgrpcmd="$chgrpprog $2"
140b290cf36Smrg	shift;;
141191cded7Smrg
142191cded7Smrg    --help) echo "$usage"; exit $?;;
143191cded7Smrg
144b290cf36Smrg    -m) mode=$2
145b290cf36Smrg	case $mode in
146b290cf36Smrg	  *' '* | *'	'* | *'
147b290cf36Smrg'*	  | *'*'* | *'?'* | *'['*)
148b290cf36Smrg	    echo "$0: invalid mode: $mode" >&2
149b290cf36Smrg	    exit 1;;
150b290cf36Smrg	esac
151b290cf36Smrg	shift;;
152191cded7Smrg
153191cded7Smrg    -o) chowncmd="$chownprog $2"
154b290cf36Smrg	shift;;
155191cded7Smrg
156b290cf36Smrg    -s) stripcmd=$stripprog;;
157191cded7Smrg
158b290cf36Smrg    -t) dst_arg=$2
159d9dcd5aeSmrg	# Protect names problematic for 'test' and other utilities.
160d9dcd5aeSmrg	case $dst_arg in
161d9dcd5aeSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162d9dcd5aeSmrg	esac
163b290cf36Smrg	shift;;
164191cded7Smrg
165b290cf36Smrg    -T) no_target_directory=true;;
166191cded7Smrg
167191cded7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168191cded7Smrg
169b290cf36Smrg    --)	shift
170191cded7Smrg	break;;
171b290cf36Smrg
172b290cf36Smrg    -*)	echo "$0: invalid option: $1" >&2
173b290cf36Smrg	exit 1;;
174b290cf36Smrg
175b290cf36Smrg    *)  break;;
176191cded7Smrg  esac
177b290cf36Smrg  shift
178191cded7Smrgdone
179191cded7Smrg
180b290cf36Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181b290cf36Smrg  # When -d is used, all remaining arguments are directories to create.
182b290cf36Smrg  # When -t is used, the destination is already specified.
183b290cf36Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184b290cf36Smrg  for arg
185b290cf36Smrg  do
186b290cf36Smrg    if test -n "$dst_arg"; then
187b290cf36Smrg      # $@ is not empty: it contains at least $arg.
188b290cf36Smrg      set fnord "$@" "$dst_arg"
189b290cf36Smrg      shift # fnord
190b290cf36Smrg    fi
191b290cf36Smrg    shift # arg
192b290cf36Smrg    dst_arg=$arg
193d9dcd5aeSmrg    # Protect names problematic for 'test' and other utilities.
194d9dcd5aeSmrg    case $dst_arg in
195d9dcd5aeSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196d9dcd5aeSmrg    esac
197b290cf36Smrg  done
198b290cf36Smrgfi
199b290cf36Smrg
200b290cf36Smrgif test $# -eq 0; then
201191cded7Smrg  if test -z "$dir_arg"; then
202191cded7Smrg    echo "$0: no input file specified." >&2
203191cded7Smrg    exit 1
204191cded7Smrg  fi
205d9dcd5aeSmrg  # It's OK to call 'install-sh -d' without argument.
206191cded7Smrg  # This can happen when creating conditional directories.
207191cded7Smrg  exit 0
208191cded7Smrgfi
209191cded7Smrg
210b290cf36Smrgif test -z "$dir_arg"; then
211d9dcd5aeSmrg  do_exit='(exit $ret); exit $ret'
212d9dcd5aeSmrg  trap "ret=129; $do_exit" 1
213d9dcd5aeSmrg  trap "ret=130; $do_exit" 2
214d9dcd5aeSmrg  trap "ret=141; $do_exit" 13
215d9dcd5aeSmrg  trap "ret=143; $do_exit" 15
216b290cf36Smrg
217b290cf36Smrg  # Set umask so as not to create temps with too-generous modes.
218b290cf36Smrg  # However, 'strip' requires both read and write access to temps.
219b290cf36Smrg  case $mode in
220b290cf36Smrg    # Optimize common cases.
221b290cf36Smrg    *644) cp_umask=133;;
222b290cf36Smrg    *755) cp_umask=22;;
223b290cf36Smrg
224b290cf36Smrg    *[0-7])
225b290cf36Smrg      if test -z "$stripcmd"; then
226b290cf36Smrg	u_plus_rw=
227b290cf36Smrg      else
228b290cf36Smrg	u_plus_rw='% 200'
229b290cf36Smrg      fi
230b290cf36Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231b290cf36Smrg    *)
232b290cf36Smrg      if test -z "$stripcmd"; then
233b290cf36Smrg	u_plus_rw=
234b290cf36Smrg      else
235b290cf36Smrg	u_plus_rw=,u+rw
236b290cf36Smrg      fi
237b290cf36Smrg      cp_umask=$mode$u_plus_rw;;
238b290cf36Smrg  esac
239b290cf36Smrgfi
240b290cf36Smrg
241191cded7Smrgfor src
242191cded7Smrgdo
243d9dcd5aeSmrg  # Protect names problematic for 'test' and other utilities.
244191cded7Smrg  case $src in
245d9dcd5aeSmrg    -* | [=\(\)!]) src=./$src;;
246191cded7Smrg  esac
247191cded7Smrg
248191cded7Smrg  if test -n "$dir_arg"; then
249191cded7Smrg    dst=$src
250b290cf36Smrg    dstdir=$dst
251b290cf36Smrg    test -d "$dstdir"
252b290cf36Smrg    dstdir_status=$?
253191cded7Smrg  else
254b290cf36Smrg
255191cded7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256191cded7Smrg    # might cause directories to be created, which would be especially bad
257191cded7Smrg    # if $src (and thus $dsttmp) contains '*'.
258191cded7Smrg    if test ! -f "$src" && test ! -d "$src"; then
259191cded7Smrg      echo "$0: $src does not exist." >&2
260191cded7Smrg      exit 1
261191cded7Smrg    fi
262191cded7Smrg
263b290cf36Smrg    if test -z "$dst_arg"; then
264191cded7Smrg      echo "$0: no destination specified." >&2
265191cded7Smrg      exit 1
266191cded7Smrg    fi
267b290cf36Smrg    dst=$dst_arg
268191cded7Smrg
269191cded7Smrg    # If destination is a directory, append the input filename; won't work
270191cded7Smrg    # if double slashes aren't ignored.
271191cded7Smrg    if test -d "$dst"; then
272191cded7Smrg      if test -n "$no_target_directory"; then
273b290cf36Smrg	echo "$0: $dst_arg: Is a directory" >&2
274191cded7Smrg	exit 1
275191cded7Smrg      fi
276b290cf36Smrg      dstdir=$dst
277b290cf36Smrg      dst=$dstdir/`basename "$src"`
278b290cf36Smrg      dstdir_status=0
279b290cf36Smrg    else
280b290cf36Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281b290cf36Smrg      dstdir=`
282b290cf36Smrg	(dirname "$dst") 2>/dev/null ||
283b290cf36Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284b290cf36Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285b290cf36Smrg	     X"$dst" : 'X\(//\)$' \| \
286b290cf36Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287b290cf36Smrg	echo X"$dst" |
288b290cf36Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289b290cf36Smrg		   s//\1/
290b290cf36Smrg		   q
291b290cf36Smrg		 }
292b290cf36Smrg		 /^X\(\/\/\)[^/].*/{
293b290cf36Smrg		   s//\1/
294b290cf36Smrg		   q
295b290cf36Smrg		 }
296b290cf36Smrg		 /^X\(\/\/\)$/{
297b290cf36Smrg		   s//\1/
298b290cf36Smrg		   q
299b290cf36Smrg		 }
300b290cf36Smrg		 /^X\(\/\).*/{
301b290cf36Smrg		   s//\1/
302b290cf36Smrg		   q
303b290cf36Smrg		 }
304b290cf36Smrg		 s/.*/./; q'
305b290cf36Smrg      `
306b290cf36Smrg
307b290cf36Smrg      test -d "$dstdir"
308b290cf36Smrg      dstdir_status=$?
309191cded7Smrg    fi
310191cded7Smrg  fi
311191cded7Smrg
312b290cf36Smrg  obsolete_mkdir_used=false
313b290cf36Smrg
314b290cf36Smrg  if test $dstdir_status != 0; then
315b290cf36Smrg    case $posix_mkdir in
316b290cf36Smrg      '')
317b290cf36Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318b290cf36Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319b290cf36Smrg	umask=`umask`
320b290cf36Smrg	case $stripcmd.$umask in
321b290cf36Smrg	  # Optimize common cases.
322b290cf36Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323b290cf36Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324b290cf36Smrg
325b290cf36Smrg	  *[0-7])
326b290cf36Smrg	    mkdir_umask=`expr $umask + 22 \
327b290cf36Smrg	      - $umask % 100 % 40 + $umask % 20 \
328b290cf36Smrg	      - $umask % 10 % 4 + $umask % 2
329b290cf36Smrg	    `;;
330b290cf36Smrg	  *) mkdir_umask=$umask,go-w;;
331b290cf36Smrg	esac
332b290cf36Smrg
333b290cf36Smrg	# With -d, create the new directory with the user-specified mode.
334b290cf36Smrg	# Otherwise, rely on $mkdir_umask.
335b290cf36Smrg	if test -n "$dir_arg"; then
336b290cf36Smrg	  mkdir_mode=-m$mode
337b290cf36Smrg	else
338b290cf36Smrg	  mkdir_mode=
339b290cf36Smrg	fi
340b290cf36Smrg
341b290cf36Smrg	posix_mkdir=false
342b290cf36Smrg	case $umask in
343b290cf36Smrg	  *[123567][0-7][0-7])
344b290cf36Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345b290cf36Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346b290cf36Smrg	    ;;
347b290cf36Smrg	  *)
348b290cf36Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349b290cf36Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350b290cf36Smrg
351b290cf36Smrg	    if (umask $mkdir_umask &&
352b290cf36Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353b290cf36Smrg	    then
354b290cf36Smrg	      if test -z "$dir_arg" || {
355b290cf36Smrg		   # Check for POSIX incompatibilities with -m.
356b290cf36Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357d9dcd5aeSmrg		   # other-writable bit of parent directory when it shouldn't.
358b290cf36Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359b290cf36Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360b290cf36Smrg		   case $ls_ld_tmpdir in
361b290cf36Smrg		     d????-?r-*) different_mode=700;;
362b290cf36Smrg		     d????-?--*) different_mode=755;;
363b290cf36Smrg		     *) false;;
364b290cf36Smrg		   esac &&
365b290cf36Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366b290cf36Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367b290cf36Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368b290cf36Smrg		   }
369b290cf36Smrg		 }
370b290cf36Smrg	      then posix_mkdir=:
371b290cf36Smrg	      fi
372b290cf36Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373b290cf36Smrg	    else
374b290cf36Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375b290cf36Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376b290cf36Smrg	    fi
377b290cf36Smrg	    trap '' 0;;
378b290cf36Smrg	esac;;
379b290cf36Smrg    esac
380191cded7Smrg
381b290cf36Smrg    if
382b290cf36Smrg      $posix_mkdir && (
383b290cf36Smrg	umask $mkdir_umask &&
384b290cf36Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385b290cf36Smrg      )
386b290cf36Smrg    then :
387b290cf36Smrg    else
388191cded7Smrg
389b290cf36Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390b290cf36Smrg      # or it failed possibly due to a race condition.  Create the
391b290cf36Smrg      # directory the slow way, step by step, checking for races as we go.
392191cded7Smrg
393b290cf36Smrg      case $dstdir in
394b290cf36Smrg	/*) prefix='/';;
395d9dcd5aeSmrg	[-=\(\)!]*) prefix='./';;
396b290cf36Smrg	*)  prefix='';;
397b290cf36Smrg      esac
398191cded7Smrg
399b290cf36Smrg      eval "$initialize_posix_glob"
400191cded7Smrg
401b290cf36Smrg      oIFS=$IFS
402b290cf36Smrg      IFS=/
403b290cf36Smrg      $posix_glob set -f
404b290cf36Smrg      set fnord $dstdir
405191cded7Smrg      shift
406b290cf36Smrg      $posix_glob set +f
407b290cf36Smrg      IFS=$oIFS
408b290cf36Smrg
409b290cf36Smrg      prefixes=
410b290cf36Smrg
411b290cf36Smrg      for d
412b290cf36Smrg      do
413d9dcd5aeSmrg	test X"$d" = X && continue
414b290cf36Smrg
415b290cf36Smrg	prefix=$prefix$d
416b290cf36Smrg	if test -d "$prefix"; then
417b290cf36Smrg	  prefixes=
418b290cf36Smrg	else
419b290cf36Smrg	  if $posix_mkdir; then
420b290cf36Smrg	    (umask=$mkdir_umask &&
421b290cf36Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422b290cf36Smrg	    # Don't fail if two instances are running concurrently.
423b290cf36Smrg	    test -d "$prefix" || exit 1
424b290cf36Smrg	  else
425b290cf36Smrg	    case $prefix in
426b290cf36Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427b290cf36Smrg	      *) qprefix=$prefix;;
428b290cf36Smrg	    esac
429b290cf36Smrg	    prefixes="$prefixes '$qprefix'"
430b290cf36Smrg	  fi
431b290cf36Smrg	fi
432b290cf36Smrg	prefix=$prefix/
433b290cf36Smrg      done
434b290cf36Smrg
435b290cf36Smrg      if test -n "$prefixes"; then
436b290cf36Smrg	# Don't fail if two instances are running concurrently.
437b290cf36Smrg	(umask $mkdir_umask &&
438b290cf36Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439b290cf36Smrg	  test -d "$dstdir" || exit 1
440b290cf36Smrg	obsolete_mkdir_used=true
441191cded7Smrg      fi
442b290cf36Smrg    fi
443191cded7Smrg  fi
444191cded7Smrg
445191cded7Smrg  if test -n "$dir_arg"; then
446b290cf36Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447b290cf36Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448b290cf36Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449b290cf36Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450191cded7Smrg  else
451191cded7Smrg
452191cded7Smrg    # Make a couple of temp file names in the proper directory.
453191cded7Smrg    dsttmp=$dstdir/_inst.$$_
454191cded7Smrg    rmtmp=$dstdir/_rm.$$_
455191cded7Smrg
456191cded7Smrg    # Trap to clean up those temp files at exit.
457191cded7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458191cded7Smrg
459191cded7Smrg    # Copy the file name to the temp name.
460b290cf36Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461191cded7Smrg
462191cded7Smrg    # and set any options; do chmod last to preserve setuid bits.
463191cded7Smrg    #
464191cded7Smrg    # If any of these fail, we abort the whole thing.  If we want to
465191cded7Smrg    # ignore errors from any of these, just make sure not to ignore
466191cded7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467191cded7Smrg    #
468b290cf36Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469b290cf36Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470b290cf36Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471b290cf36Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472b290cf36Smrg
473b290cf36Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474b290cf36Smrg    if $copy_on_change &&
475b290cf36Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476b290cf36Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477b290cf36Smrg
478b290cf36Smrg       eval "$initialize_posix_glob" &&
479b290cf36Smrg       $posix_glob set -f &&
480b290cf36Smrg       set X $old && old=:$2:$4:$5:$6 &&
481b290cf36Smrg       set X $new && new=:$2:$4:$5:$6 &&
482b290cf36Smrg       $posix_glob set +f &&
483b290cf36Smrg
484b290cf36Smrg       test "$old" = "$new" &&
485b290cf36Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486b290cf36Smrg    then
487b290cf36Smrg      rm -f "$dsttmp"
488b290cf36Smrg    else
489b290cf36Smrg      # Rename the file to the real destination.
490b290cf36Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491b290cf36Smrg
492b290cf36Smrg      # The rename failed, perhaps because mv can't rename something else
493b290cf36Smrg      # to itself, or perhaps because mv is so ancient that it does not
494b290cf36Smrg      # support -f.
495b290cf36Smrg      {
496b290cf36Smrg	# Now remove or move aside any old file at destination location.
497b290cf36Smrg	# We try this two ways since rm can't unlink itself on some
498b290cf36Smrg	# systems and the destination file might be busy for other
499b290cf36Smrg	# reasons.  In this case, the final cleanup might fail but the new
500b290cf36Smrg	# file should still install successfully.
501b290cf36Smrg	{
502b290cf36Smrg	  test ! -f "$dst" ||
503b290cf36Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504b290cf36Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505b290cf36Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506b290cf36Smrg	  } ||
507b290cf36Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508b290cf36Smrg	    (exit 1); exit 1
509b290cf36Smrg	  }
510b290cf36Smrg	} &&
511b290cf36Smrg
512b290cf36Smrg	# Now rename the file to the real destination.
513b290cf36Smrg	$doit $mvcmd "$dsttmp" "$dst"
514b290cf36Smrg      }
515b290cf36Smrg    fi || exit 1
516b290cf36Smrg
517b290cf36Smrg    trap '' 0
518b290cf36Smrg  fi
519191cded7Smrgdone
520191cded7Smrg
521191cded7Smrg# Local variables:
522191cded7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523191cded7Smrg# time-stamp-start: "scriptversion="
524191cded7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525ba064abeSmrg# time-stamp-time-zone: "UTC"
526ba064abeSmrg# time-stamp-end: "; # UTC"
527191cded7Smrg# End:
528