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