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