install-sh revision a73597f9
1659607e0Smrg#!/bin/sh
2659607e0Smrg# install - install a program, script, or datafile
3659607e0Smrg
4a73597f9Smrgscriptversion=2011-11-20.07; # UTC
5659607e0Smrg
6659607e0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7659607e0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8659607e0Smrg# following copyright and license.
9659607e0Smrg#
10659607e0Smrg# Copyright (C) 1994 X Consortium
11659607e0Smrg#
12659607e0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13659607e0Smrg# of this software and associated documentation files (the "Software"), to
14659607e0Smrg# deal in the Software without restriction, including without limitation the
15659607e0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16659607e0Smrg# sell copies of the Software, and to permit persons to whom the Software is
17659607e0Smrg# furnished to do so, subject to the following conditions:
18659607e0Smrg#
19659607e0Smrg# The above copyright notice and this permission notice shall be included in
20659607e0Smrg# all copies or substantial portions of the Software.
21659607e0Smrg#
22659607e0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23659607e0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24659607e0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25659607e0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26659607e0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27659607e0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28659607e0Smrg#
29659607e0Smrg# Except as contained in this notice, the name of the X Consortium shall not
30659607e0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31659607e0Smrg# ings in this Software without prior written authorization from the X Consor-
32659607e0Smrg# tium.
33659607e0Smrg#
34659607e0Smrg#
35659607e0Smrg# FSF changes to this file are in the public domain.
36659607e0Smrg#
37659607e0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38a73597f9Smrg# 'make' implicit rules from creating a file called install from it
39659607e0Smrg# when there is no Makefile.
40659607e0Smrg#
41659607e0Smrg# This script is compatible with the BSD install script, but was written
42659607e0Smrg# from scratch.
43659607e0Smrg
44659607e0Smrgnl='
45659607e0Smrg'
46659607e0SmrgIFS=" ""	$nl"
47659607e0Smrg
48659607e0Smrg# set DOITPROG to echo to test this script
49659607e0Smrg
50659607e0Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51fc27e79cSmrgdoit=${DOITPROG-}
52659607e0Smrgif test -z "$doit"; then
53659607e0Smrg  doit_exec=exec
54659607e0Smrgelse
55659607e0Smrg  doit_exec=$doit
56659607e0Smrgfi
57659607e0Smrg
58659607e0Smrg# Put in absolute file names if you don't have them in your path;
59659607e0Smrg# or use environment vars.
60659607e0Smrg
61fc27e79cSmrgchgrpprog=${CHGRPPROG-chgrp}
62fc27e79cSmrgchmodprog=${CHMODPROG-chmod}
63fc27e79cSmrgchownprog=${CHOWNPROG-chown}
64fc27e79cSmrgcmpprog=${CMPPROG-cmp}
65fc27e79cSmrgcpprog=${CPPROG-cp}
66fc27e79cSmrgmkdirprog=${MKDIRPROG-mkdir}
67fc27e79cSmrgmvprog=${MVPROG-mv}
68fc27e79cSmrgrmprog=${RMPROG-rm}
69fc27e79cSmrgstripprog=${STRIPPROG-strip}
70fc27e79cSmrg
71fc27e79cSmrgposix_glob='?'
72fc27e79cSmrginitialize_posix_glob='
73fc27e79cSmrg  test "$posix_glob" != "?" || {
74fc27e79cSmrg    if (set -f) 2>/dev/null; then
75fc27e79cSmrg      posix_glob=
76fc27e79cSmrg    else
77fc27e79cSmrg      posix_glob=:
78fc27e79cSmrg    fi
79fc27e79cSmrg  }
80fc27e79cSmrg'
81659607e0Smrg
82659607e0Smrgposix_mkdir=
83659607e0Smrg
84659607e0Smrg# Desired mode of installed file.
85659607e0Smrgmode=0755
86659607e0Smrg
87fc27e79cSmrgchgrpcmd=
88659607e0Smrgchmodcmd=$chmodprog
89659607e0Smrgchowncmd=
90fc27e79cSmrgmvcmd=$mvprog
91659607e0Smrgrmcmd="$rmprog -f"
92fc27e79cSmrgstripcmd=
93fc27e79cSmrg
94659607e0Smrgsrc=
95659607e0Smrgdst=
96659607e0Smrgdir_arg=
97fc27e79cSmrgdst_arg=
98fc27e79cSmrg
99fc27e79cSmrgcopy_on_change=false
100659607e0Smrgno_target_directory=
101659607e0Smrg
102fc27e79cSmrgusage="\
103fc27e79cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104659607e0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105659607e0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106659607e0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107659607e0Smrg
108659607e0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109659607e0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110659607e0SmrgIn the 4th, create DIRECTORIES.
111659607e0Smrg
112659607e0SmrgOptions:
113fc27e79cSmrg     --help     display this help and exit.
114fc27e79cSmrg     --version  display version info and exit.
115fc27e79cSmrg
116fc27e79cSmrg  -c            (ignored)
117fc27e79cSmrg  -C            install only if different (preserve the last data modification time)
118fc27e79cSmrg  -d            create directories instead of installing files.
119fc27e79cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
120fc27e79cSmrg  -m MODE       $chmodprog installed files to MODE.
121fc27e79cSmrg  -o USER       $chownprog installed files to USER.
122fc27e79cSmrg  -s            $stripprog installed files.
123fc27e79cSmrg  -t DIRECTORY  install into DIRECTORY.
124fc27e79cSmrg  -T            report an error if DSTFILE is a directory.
125659607e0Smrg
126659607e0SmrgEnvironment variables override the default commands:
127fc27e79cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128fc27e79cSmrg  RMPROG STRIPPROG
129659607e0Smrg"
130659607e0Smrg
131659607e0Smrgwhile test $# -ne 0; do
132659607e0Smrg  case $1 in
133fc27e79cSmrg    -c) ;;
134fc27e79cSmrg
135fc27e79cSmrg    -C) copy_on_change=true;;
136659607e0Smrg
137fc27e79cSmrg    -d) dir_arg=true;;
138659607e0Smrg
139659607e0Smrg    -g) chgrpcmd="$chgrpprog $2"
140fc27e79cSmrg	shift;;
141659607e0Smrg
142659607e0Smrg    --help) echo "$usage"; exit $?;;
143659607e0Smrg
144659607e0Smrg    -m) mode=$2
145659607e0Smrg	case $mode in
146659607e0Smrg	  *' '* | *'	'* | *'
147659607e0Smrg'*	  | *'*'* | *'?'* | *'['*)
148659607e0Smrg	    echo "$0: invalid mode: $mode" >&2
149659607e0Smrg	    exit 1;;
150659607e0Smrg	esac
151fc27e79cSmrg	shift;;
152659607e0Smrg
153659607e0Smrg    -o) chowncmd="$chownprog $2"
154fc27e79cSmrg	shift;;
155659607e0Smrg
156fc27e79cSmrg    -s) stripcmd=$stripprog;;
157659607e0Smrg
158fc27e79cSmrg    -t) dst_arg=$2
159a73597f9Smrg	# Protect names problematic for 'test' and other utilities.
160a73597f9Smrg	case $dst_arg in
161a73597f9Smrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162a73597f9Smrg	esac
163fc27e79cSmrg	shift;;
164659607e0Smrg
165fc27e79cSmrg    -T) no_target_directory=true;;
166659607e0Smrg
167659607e0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168659607e0Smrg
169659607e0Smrg    --)	shift
170659607e0Smrg	break;;
171659607e0Smrg
172659607e0Smrg    -*)	echo "$0: invalid option: $1" >&2
173659607e0Smrg	exit 1;;
174659607e0Smrg
175659607e0Smrg    *)  break;;
176659607e0Smrg  esac
177fc27e79cSmrg  shift
178659607e0Smrgdone
179659607e0Smrg
180fc27e79cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181659607e0Smrg  # When -d is used, all remaining arguments are directories to create.
182659607e0Smrg  # When -t is used, the destination is already specified.
183659607e0Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184659607e0Smrg  for arg
185659607e0Smrg  do
186fc27e79cSmrg    if test -n "$dst_arg"; then
187659607e0Smrg      # $@ is not empty: it contains at least $arg.
188fc27e79cSmrg      set fnord "$@" "$dst_arg"
189659607e0Smrg      shift # fnord
190659607e0Smrg    fi
191659607e0Smrg    shift # arg
192fc27e79cSmrg    dst_arg=$arg
193a73597f9Smrg    # Protect names problematic for 'test' and other utilities.
194a73597f9Smrg    case $dst_arg in
195a73597f9Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196a73597f9Smrg    esac
197659607e0Smrg  done
198659607e0Smrgfi
199659607e0Smrg
200659607e0Smrgif test $# -eq 0; then
201659607e0Smrg  if test -z "$dir_arg"; then
202659607e0Smrg    echo "$0: no input file specified." >&2
203659607e0Smrg    exit 1
204659607e0Smrg  fi
205a73597f9Smrg  # It's OK to call 'install-sh -d' without argument.
206659607e0Smrg  # This can happen when creating conditional directories.
207659607e0Smrg  exit 0
208659607e0Smrgfi
209659607e0Smrg
210659607e0Smrgif test -z "$dir_arg"; then
211a73597f9Smrg  do_exit='(exit $ret); exit $ret'
212a73597f9Smrg  trap "ret=129; $do_exit" 1
213a73597f9Smrg  trap "ret=130; $do_exit" 2
214a73597f9Smrg  trap "ret=141; $do_exit" 13
215a73597f9Smrg  trap "ret=143; $do_exit" 15
216659607e0Smrg
217659607e0Smrg  # Set umask so as not to create temps with too-generous modes.
218659607e0Smrg  # However, 'strip' requires both read and write access to temps.
219659607e0Smrg  case $mode in
220659607e0Smrg    # Optimize common cases.
221659607e0Smrg    *644) cp_umask=133;;
222659607e0Smrg    *755) cp_umask=22;;
223659607e0Smrg
224659607e0Smrg    *[0-7])
225659607e0Smrg      if test -z "$stripcmd"; then
226659607e0Smrg	u_plus_rw=
227659607e0Smrg      else
228659607e0Smrg	u_plus_rw='% 200'
229659607e0Smrg      fi
230659607e0Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231659607e0Smrg    *)
232659607e0Smrg      if test -z "$stripcmd"; then
233659607e0Smrg	u_plus_rw=
234659607e0Smrg      else
235659607e0Smrg	u_plus_rw=,u+rw
236659607e0Smrg      fi
237659607e0Smrg      cp_umask=$mode$u_plus_rw;;
238659607e0Smrg  esac
239659607e0Smrgfi
240659607e0Smrg
241659607e0Smrgfor src
242659607e0Smrgdo
243a73597f9Smrg  # Protect names problematic for 'test' and other utilities.
244659607e0Smrg  case $src in
245a73597f9Smrg    -* | [=\(\)!]) src=./$src;;
246659607e0Smrg  esac
247659607e0Smrg
248659607e0Smrg  if test -n "$dir_arg"; then
249659607e0Smrg    dst=$src
250659607e0Smrg    dstdir=$dst
251659607e0Smrg    test -d "$dstdir"
252659607e0Smrg    dstdir_status=$?
253659607e0Smrg  else
254659607e0Smrg
255659607e0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256659607e0Smrg    # might cause directories to be created, which would be especially bad
257659607e0Smrg    # if $src (and thus $dsttmp) contains '*'.
258659607e0Smrg    if test ! -f "$src" && test ! -d "$src"; then
259659607e0Smrg      echo "$0: $src does not exist." >&2
260659607e0Smrg      exit 1
261659607e0Smrg    fi
262659607e0Smrg
263fc27e79cSmrg    if test -z "$dst_arg"; then
264659607e0Smrg      echo "$0: no destination specified." >&2
265659607e0Smrg      exit 1
266659607e0Smrg    fi
267fc27e79cSmrg    dst=$dst_arg
268659607e0Smrg
269659607e0Smrg    # If destination is a directory, append the input filename; won't work
270659607e0Smrg    # if double slashes aren't ignored.
271659607e0Smrg    if test -d "$dst"; then
272659607e0Smrg      if test -n "$no_target_directory"; then
273fc27e79cSmrg	echo "$0: $dst_arg: Is a directory" >&2
274659607e0Smrg	exit 1
275659607e0Smrg      fi
276659607e0Smrg      dstdir=$dst
277659607e0Smrg      dst=$dstdir/`basename "$src"`
278659607e0Smrg      dstdir_status=0
279659607e0Smrg    else
280659607e0Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281659607e0Smrg      dstdir=`
282659607e0Smrg	(dirname "$dst") 2>/dev/null ||
283659607e0Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284659607e0Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285659607e0Smrg	     X"$dst" : 'X\(//\)$' \| \
286659607e0Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287659607e0Smrg	echo X"$dst" |
288659607e0Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289659607e0Smrg		   s//\1/
290659607e0Smrg		   q
291659607e0Smrg		 }
292659607e0Smrg		 /^X\(\/\/\)[^/].*/{
293659607e0Smrg		   s//\1/
294659607e0Smrg		   q
295659607e0Smrg		 }
296659607e0Smrg		 /^X\(\/\/\)$/{
297659607e0Smrg		   s//\1/
298659607e0Smrg		   q
299659607e0Smrg		 }
300659607e0Smrg		 /^X\(\/\).*/{
301659607e0Smrg		   s//\1/
302659607e0Smrg		   q
303659607e0Smrg		 }
304659607e0Smrg		 s/.*/./; q'
305659607e0Smrg      `
306659607e0Smrg
307659607e0Smrg      test -d "$dstdir"
308659607e0Smrg      dstdir_status=$?
309659607e0Smrg    fi
310659607e0Smrg  fi
311659607e0Smrg
312659607e0Smrg  obsolete_mkdir_used=false
313659607e0Smrg
314659607e0Smrg  if test $dstdir_status != 0; then
315659607e0Smrg    case $posix_mkdir in
316659607e0Smrg      '')
317659607e0Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318659607e0Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319659607e0Smrg	umask=`umask`
320659607e0Smrg	case $stripcmd.$umask in
321659607e0Smrg	  # Optimize common cases.
322659607e0Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323659607e0Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324659607e0Smrg
325659607e0Smrg	  *[0-7])
326659607e0Smrg	    mkdir_umask=`expr $umask + 22 \
327659607e0Smrg	      - $umask % 100 % 40 + $umask % 20 \
328659607e0Smrg	      - $umask % 10 % 4 + $umask % 2
329659607e0Smrg	    `;;
330659607e0Smrg	  *) mkdir_umask=$umask,go-w;;
331659607e0Smrg	esac
332659607e0Smrg
333659607e0Smrg	# With -d, create the new directory with the user-specified mode.
334659607e0Smrg	# Otherwise, rely on $mkdir_umask.
335659607e0Smrg	if test -n "$dir_arg"; then
336659607e0Smrg	  mkdir_mode=-m$mode
337659607e0Smrg	else
338659607e0Smrg	  mkdir_mode=
339659607e0Smrg	fi
340659607e0Smrg
341659607e0Smrg	posix_mkdir=false
342659607e0Smrg	case $umask in
343659607e0Smrg	  *[123567][0-7][0-7])
344659607e0Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345659607e0Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346659607e0Smrg	    ;;
347659607e0Smrg	  *)
348659607e0Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349659607e0Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350659607e0Smrg
351659607e0Smrg	    if (umask $mkdir_umask &&
352659607e0Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353659607e0Smrg	    then
354659607e0Smrg	      if test -z "$dir_arg" || {
355659607e0Smrg		   # Check for POSIX incompatibilities with -m.
356659607e0Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357a73597f9Smrg		   # other-writable bit of parent directory when it shouldn't.
358659607e0Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359659607e0Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360659607e0Smrg		   case $ls_ld_tmpdir in
361659607e0Smrg		     d????-?r-*) different_mode=700;;
362659607e0Smrg		     d????-?--*) different_mode=755;;
363659607e0Smrg		     *) false;;
364659607e0Smrg		   esac &&
365659607e0Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366659607e0Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367659607e0Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368659607e0Smrg		   }
369659607e0Smrg		 }
370659607e0Smrg	      then posix_mkdir=:
371659607e0Smrg	      fi
372659607e0Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373659607e0Smrg	    else
374659607e0Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375659607e0Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376659607e0Smrg	    fi
377659607e0Smrg	    trap '' 0;;
378659607e0Smrg	esac;;
379659607e0Smrg    esac
380659607e0Smrg
381659607e0Smrg    if
382659607e0Smrg      $posix_mkdir && (
383659607e0Smrg	umask $mkdir_umask &&
384659607e0Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385659607e0Smrg      )
386659607e0Smrg    then :
387659607e0Smrg    else
388659607e0Smrg
389659607e0Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390659607e0Smrg      # or it failed possibly due to a race condition.  Create the
391659607e0Smrg      # directory the slow way, step by step, checking for races as we go.
392659607e0Smrg
393659607e0Smrg      case $dstdir in
394fc27e79cSmrg	/*) prefix='/';;
395a73597f9Smrg	[-=\(\)!]*) prefix='./';;
396fc27e79cSmrg	*)  prefix='';;
397659607e0Smrg      esac
398659607e0Smrg
399fc27e79cSmrg      eval "$initialize_posix_glob"
400659607e0Smrg
401659607e0Smrg      oIFS=$IFS
402659607e0Smrg      IFS=/
403fc27e79cSmrg      $posix_glob set -f
404659607e0Smrg      set fnord $dstdir
405659607e0Smrg      shift
406fc27e79cSmrg      $posix_glob set +f
407659607e0Smrg      IFS=$oIFS
408659607e0Smrg
409659607e0Smrg      prefixes=
410659607e0Smrg
411659607e0Smrg      for d
412659607e0Smrg      do
413a73597f9Smrg	test X"$d" = X && continue
414659607e0Smrg
415659607e0Smrg	prefix=$prefix$d
416659607e0Smrg	if test -d "$prefix"; then
417659607e0Smrg	  prefixes=
418659607e0Smrg	else
419659607e0Smrg	  if $posix_mkdir; then
420659607e0Smrg	    (umask=$mkdir_umask &&
421659607e0Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422659607e0Smrg	    # Don't fail if two instances are running concurrently.
423659607e0Smrg	    test -d "$prefix" || exit 1
424659607e0Smrg	  else
425659607e0Smrg	    case $prefix in
426659607e0Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427659607e0Smrg	      *) qprefix=$prefix;;
428659607e0Smrg	    esac
429659607e0Smrg	    prefixes="$prefixes '$qprefix'"
430659607e0Smrg	  fi
431659607e0Smrg	fi
432659607e0Smrg	prefix=$prefix/
433659607e0Smrg      done
434659607e0Smrg
435659607e0Smrg      if test -n "$prefixes"; then
436659607e0Smrg	# Don't fail if two instances are running concurrently.
437659607e0Smrg	(umask $mkdir_umask &&
438659607e0Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439659607e0Smrg	  test -d "$dstdir" || exit 1
440659607e0Smrg	obsolete_mkdir_used=true
441659607e0Smrg      fi
442659607e0Smrg    fi
443659607e0Smrg  fi
444659607e0Smrg
445659607e0Smrg  if test -n "$dir_arg"; then
446659607e0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447659607e0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448659607e0Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449659607e0Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450659607e0Smrg  else
451659607e0Smrg
452659607e0Smrg    # Make a couple of temp file names in the proper directory.
453659607e0Smrg    dsttmp=$dstdir/_inst.$$_
454659607e0Smrg    rmtmp=$dstdir/_rm.$$_
455659607e0Smrg
456659607e0Smrg    # Trap to clean up those temp files at exit.
457659607e0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458659607e0Smrg
459659607e0Smrg    # Copy the file name to the temp name.
460659607e0Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461659607e0Smrg
462659607e0Smrg    # and set any options; do chmod last to preserve setuid bits.
463659607e0Smrg    #
464659607e0Smrg    # If any of these fail, we abort the whole thing.  If we want to
465659607e0Smrg    # ignore errors from any of these, just make sure not to ignore
466659607e0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467659607e0Smrg    #
468fc27e79cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469fc27e79cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470fc27e79cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471fc27e79cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472fc27e79cSmrg
473fc27e79cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
474fc27e79cSmrg    if $copy_on_change &&
475fc27e79cSmrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476fc27e79cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477fc27e79cSmrg
478fc27e79cSmrg       eval "$initialize_posix_glob" &&
479fc27e79cSmrg       $posix_glob set -f &&
480fc27e79cSmrg       set X $old && old=:$2:$4:$5:$6 &&
481fc27e79cSmrg       set X $new && new=:$2:$4:$5:$6 &&
482fc27e79cSmrg       $posix_glob set +f &&
483fc27e79cSmrg
484fc27e79cSmrg       test "$old" = "$new" &&
485fc27e79cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486fc27e79cSmrg    then
487fc27e79cSmrg      rm -f "$dsttmp"
488fc27e79cSmrg    else
489fc27e79cSmrg      # Rename the file to the real destination.
490fc27e79cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491fc27e79cSmrg
492fc27e79cSmrg      # The rename failed, perhaps because mv can't rename something else
493fc27e79cSmrg      # to itself, or perhaps because mv is so ancient that it does not
494fc27e79cSmrg      # support -f.
495fc27e79cSmrg      {
496fc27e79cSmrg	# Now remove or move aside any old file at destination location.
497fc27e79cSmrg	# We try this two ways since rm can't unlink itself on some
498fc27e79cSmrg	# systems and the destination file might be busy for other
499fc27e79cSmrg	# reasons.  In this case, the final cleanup might fail but the new
500fc27e79cSmrg	# file should still install successfully.
501fc27e79cSmrg	{
502fc27e79cSmrg	  test ! -f "$dst" ||
503fc27e79cSmrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504fc27e79cSmrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505fc27e79cSmrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506fc27e79cSmrg	  } ||
507fc27e79cSmrg	  { echo "$0: cannot unlink or rename $dst" >&2
508fc27e79cSmrg	    (exit 1); exit 1
509fc27e79cSmrg	  }
510fc27e79cSmrg	} &&
511fc27e79cSmrg
512fc27e79cSmrg	# Now rename the file to the real destination.
513fc27e79cSmrg	$doit $mvcmd "$dsttmp" "$dst"
514fc27e79cSmrg      }
515fc27e79cSmrg    fi || exit 1
516659607e0Smrg
517659607e0Smrg    trap '' 0
518659607e0Smrg  fi
519659607e0Smrgdone
520659607e0Smrg
521659607e0Smrg# Local variables:
522659607e0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523659607e0Smrg# time-stamp-start: "scriptversion="
524659607e0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525b73be646Smrg# time-stamp-time-zone: "UTC"
526b73be646Smrg# time-stamp-end: "; # UTC"
527659607e0Smrg# End:
528