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