install-sh revision a85aecdf
1fd7d9bd3Smrg#!/bin/sh
2fd7d9bd3Smrg# install - install a program, script, or datafile
3350952b9Smrg
4a85aecdfSmrgscriptversion=2011-01-19.21; # UTC
5350952b9Smrg
6fd7d9bd3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7fd7d9bd3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8fd7d9bd3Smrg# following copyright and license.
9fd7d9bd3Smrg#
10fd7d9bd3Smrg# Copyright (C) 1994 X Consortium
11fd7d9bd3Smrg#
12fd7d9bd3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13fd7d9bd3Smrg# of this software and associated documentation files (the "Software"), to
14fd7d9bd3Smrg# deal in the Software without restriction, including without limitation the
15fd7d9bd3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16fd7d9bd3Smrg# sell copies of the Software, and to permit persons to whom the Software is
17fd7d9bd3Smrg# furnished to do so, subject to the following conditions:
18fd7d9bd3Smrg#
19fd7d9bd3Smrg# The above copyright notice and this permission notice shall be included in
20fd7d9bd3Smrg# all copies or substantial portions of the Software.
21fd7d9bd3Smrg#
22fd7d9bd3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23fd7d9bd3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24fd7d9bd3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25fd7d9bd3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26fd7d9bd3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27fd7d9bd3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28fd7d9bd3Smrg#
29fd7d9bd3Smrg# Except as contained in this notice, the name of the X Consortium shall not
30fd7d9bd3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31fd7d9bd3Smrg# ings in this Software without prior written authorization from the X Consor-
32fd7d9bd3Smrg# tium.
33fd7d9bd3Smrg#
34fd7d9bd3Smrg#
35fd7d9bd3Smrg# FSF changes to this file are in the public domain.
36fd7d9bd3Smrg#
37fd7d9bd3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38fd7d9bd3Smrg# `make' implicit rules from creating a file called install from it
39fd7d9bd3Smrg# when there is no Makefile.
40fd7d9bd3Smrg#
41fd7d9bd3Smrg# This script is compatible with the BSD install script, but was written
42350952b9Smrg# from scratch.
43fd7d9bd3Smrg
44350952b9Smrgnl='
45350952b9Smrg'
46350952b9SmrgIFS=" ""	$nl"
47fd7d9bd3Smrg
48fd7d9bd3Smrg# set DOITPROG to echo to test this script
49fd7d9bd3Smrg
50fd7d9bd3Smrg# Don't use :- since 4.3BSD and earlier shells don't like it.
51350952b9Smrgdoit=${DOITPROG-}
52350952b9Smrgif test -z "$doit"; then
53350952b9Smrg  doit_exec=exec
54fd7d9bd3Smrgelse
55350952b9Smrg  doit_exec=$doit
56fd7d9bd3Smrgfi
57fd7d9bd3Smrg
58350952b9Smrg# Put in absolute file names if you don't have them in your path;
59350952b9Smrg# or use environment vars.
60350952b9Smrg
61350952b9Smrgchgrpprog=${CHGRPPROG-chgrp}
62350952b9Smrgchmodprog=${CHMODPROG-chmod}
63350952b9Smrgchownprog=${CHOWNPROG-chown}
64350952b9Smrgcmpprog=${CMPPROG-cmp}
65350952b9Smrgcpprog=${CPPROG-cp}
66350952b9Smrgmkdirprog=${MKDIRPROG-mkdir}
67350952b9Smrgmvprog=${MVPROG-mv}
68350952b9Smrgrmprog=${RMPROG-rm}
69350952b9Smrgstripprog=${STRIPPROG-strip}
70350952b9Smrg
71350952b9Smrgposix_glob='?'
72350952b9Smrginitialize_posix_glob='
73350952b9Smrg  test "$posix_glob" != "?" || {
74350952b9Smrg    if (set -f) 2>/dev/null; then
75350952b9Smrg      posix_glob=
76350952b9Smrg    else
77350952b9Smrg      posix_glob=:
78350952b9Smrg    fi
79350952b9Smrg  }
80350952b9Smrg'
81350952b9Smrg
82350952b9Smrgposix_mkdir=
83350952b9Smrg
84350952b9Smrg# Desired mode of installed file.
85350952b9Smrgmode=0755
86350952b9Smrg
87350952b9Smrgchgrpcmd=
88350952b9Smrgchmodcmd=$chmodprog
89350952b9Smrgchowncmd=
90350952b9Smrgmvcmd=$mvprog
91350952b9Smrgrmcmd="$rmprog -f"
92350952b9Smrgstripcmd=
93fd7d9bd3Smrg
94350952b9Smrgsrc=
95350952b9Smrgdst=
96350952b9Smrgdir_arg=
97350952b9Smrgdst_arg=
98fd7d9bd3Smrg
99350952b9Smrgcopy_on_change=false
100350952b9Smrgno_target_directory=
101fd7d9bd3Smrg
102350952b9Smrgusage="\
103350952b9SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
104350952b9Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
105350952b9Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
106350952b9Smrg   or: $0 [OPTION]... -d DIRECTORIES...
107fd7d9bd3Smrg
108350952b9SmrgIn the 1st form, copy SRCFILE to DSTFILE.
109350952b9SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
110350952b9SmrgIn the 4th, create DIRECTORIES.
111fd7d9bd3Smrg
112350952b9SmrgOptions:
113350952b9Smrg     --help     display this help and exit.
114350952b9Smrg     --version  display version info and exit.
115fd7d9bd3Smrg
116350952b9Smrg  -c            (ignored)
117350952b9Smrg  -C            install only if different (preserve the last data modification time)
118350952b9Smrg  -d            create directories instead of installing files.
119350952b9Smrg  -g GROUP      $chgrpprog installed files to GROUP.
120350952b9Smrg  -m MODE       $chmodprog installed files to MODE.
121350952b9Smrg  -o USER       $chownprog installed files to USER.
122350952b9Smrg  -s            $stripprog installed files.
123350952b9Smrg  -t DIRECTORY  install into DIRECTORY.
124350952b9Smrg  -T            report an error if DSTFILE is a directory.
125fd7d9bd3Smrg
126350952b9SmrgEnvironment variables override the default commands:
127350952b9Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
128350952b9Smrg  RMPROG STRIPPROG
129350952b9Smrg"
130fd7d9bd3Smrg
131350952b9Smrgwhile test $# -ne 0; do
132350952b9Smrg  case $1 in
133350952b9Smrg    -c) ;;
134fd7d9bd3Smrg
135350952b9Smrg    -C) copy_on_change=true;;
136fd7d9bd3Smrg
137350952b9Smrg    -d) dir_arg=true;;
138fd7d9bd3Smrg
139350952b9Smrg    -g) chgrpcmd="$chgrpprog $2"
140350952b9Smrg	shift;;
141fd7d9bd3Smrg
142350952b9Smrg    --help) echo "$usage"; exit $?;;
143fd7d9bd3Smrg
144350952b9Smrg    -m) mode=$2
145350952b9Smrg	case $mode in
146350952b9Smrg	  *' '* | *'	'* | *'
147350952b9Smrg'*	  | *'*'* | *'?'* | *'['*)
148350952b9Smrg	    echo "$0: invalid mode: $mode" >&2
149350952b9Smrg	    exit 1;;
150350952b9Smrg	esac
151350952b9Smrg	shift;;
152fd7d9bd3Smrg
153350952b9Smrg    -o) chowncmd="$chownprog $2"
154350952b9Smrg	shift;;
155fd7d9bd3Smrg
156350952b9Smrg    -s) stripcmd=$stripprog;;
157fd7d9bd3Smrg
158350952b9Smrg    -t) dst_arg=$2
159a85aecdfSmrg	# Protect names problematic for `test' and other utilities.
160a85aecdfSmrg	case $dst_arg in
161a85aecdfSmrg	  -* | [=\(\)!]) dst_arg=./$dst_arg;;
162a85aecdfSmrg	esac
163350952b9Smrg	shift;;
164fd7d9bd3Smrg
165350952b9Smrg    -T) no_target_directory=true;;
166fd7d9bd3Smrg
167350952b9Smrg    --version) echo "$0 $scriptversion"; exit $?;;
168fd7d9bd3Smrg
169350952b9Smrg    --)	shift
170350952b9Smrg	break;;
171fd7d9bd3Smrg
172350952b9Smrg    -*)	echo "$0: invalid option: $1" >&2
173350952b9Smrg	exit 1;;
174fd7d9bd3Smrg
175350952b9Smrg    *)  break;;
176350952b9Smrg  esac
177350952b9Smrg  shift
178350952b9Smrgdone
179fd7d9bd3Smrg
180350952b9Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
181350952b9Smrg  # When -d is used, all remaining arguments are directories to create.
182350952b9Smrg  # When -t is used, the destination is already specified.
183350952b9Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
184350952b9Smrg  for arg
185350952b9Smrg  do
186350952b9Smrg    if test -n "$dst_arg"; then
187350952b9Smrg      # $@ is not empty: it contains at least $arg.
188350952b9Smrg      set fnord "$@" "$dst_arg"
189350952b9Smrg      shift # fnord
190350952b9Smrg    fi
191350952b9Smrg    shift # arg
192350952b9Smrg    dst_arg=$arg
193a85aecdfSmrg    # Protect names problematic for `test' and other utilities.
194a85aecdfSmrg    case $dst_arg in
195a85aecdfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
196a85aecdfSmrg    esac
197350952b9Smrg  done
198350952b9Smrgfi
199fd7d9bd3Smrg
200350952b9Smrgif test $# -eq 0; then
201350952b9Smrg  if test -z "$dir_arg"; then
202350952b9Smrg    echo "$0: no input file specified." >&2
203350952b9Smrg    exit 1
204350952b9Smrg  fi
205350952b9Smrg  # It's OK to call `install-sh -d' without argument.
206350952b9Smrg  # This can happen when creating conditional directories.
207350952b9Smrg  exit 0
208350952b9Smrgfi
209fd7d9bd3Smrg
210350952b9Smrgif test -z "$dir_arg"; then
211a85aecdfSmrg  do_exit='(exit $ret); exit $ret'
212a85aecdfSmrg  trap "ret=129; $do_exit" 1
213a85aecdfSmrg  trap "ret=130; $do_exit" 2
214a85aecdfSmrg  trap "ret=141; $do_exit" 13
215a85aecdfSmrg  trap "ret=143; $do_exit" 15
216350952b9Smrg
217350952b9Smrg  # Set umask so as not to create temps with too-generous modes.
218350952b9Smrg  # However, 'strip' requires both read and write access to temps.
219350952b9Smrg  case $mode in
220350952b9Smrg    # Optimize common cases.
221350952b9Smrg    *644) cp_umask=133;;
222350952b9Smrg    *755) cp_umask=22;;
223350952b9Smrg
224350952b9Smrg    *[0-7])
225350952b9Smrg      if test -z "$stripcmd"; then
226350952b9Smrg	u_plus_rw=
227350952b9Smrg      else
228350952b9Smrg	u_plus_rw='% 200'
229350952b9Smrg      fi
230350952b9Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
231350952b9Smrg    *)
232350952b9Smrg      if test -z "$stripcmd"; then
233350952b9Smrg	u_plus_rw=
234350952b9Smrg      else
235350952b9Smrg	u_plus_rw=,u+rw
236350952b9Smrg      fi
237350952b9Smrg      cp_umask=$mode$u_plus_rw;;
238350952b9Smrg  esac
239350952b9Smrgfi
240fd7d9bd3Smrg
241350952b9Smrgfor src
242350952b9Smrgdo
243a85aecdfSmrg  # Protect names problematic for `test' and other utilities.
244350952b9Smrg  case $src in
245a85aecdfSmrg    -* | [=\(\)!]) src=./$src;;
246350952b9Smrg  esac
247350952b9Smrg
248350952b9Smrg  if test -n "$dir_arg"; then
249350952b9Smrg    dst=$src
250350952b9Smrg    dstdir=$dst
251350952b9Smrg    test -d "$dstdir"
252350952b9Smrg    dstdir_status=$?
253350952b9Smrg  else
254350952b9Smrg
255350952b9Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
256350952b9Smrg    # might cause directories to be created, which would be especially bad
257350952b9Smrg    # if $src (and thus $dsttmp) contains '*'.
258350952b9Smrg    if test ! -f "$src" && test ! -d "$src"; then
259350952b9Smrg      echo "$0: $src does not exist." >&2
260350952b9Smrg      exit 1
261350952b9Smrg    fi
262350952b9Smrg
263350952b9Smrg    if test -z "$dst_arg"; then
264350952b9Smrg      echo "$0: no destination specified." >&2
265350952b9Smrg      exit 1
266350952b9Smrg    fi
267350952b9Smrg    dst=$dst_arg
268fd7d9bd3Smrg
269350952b9Smrg    # If destination is a directory, append the input filename; won't work
270350952b9Smrg    # if double slashes aren't ignored.
271350952b9Smrg    if test -d "$dst"; then
272350952b9Smrg      if test -n "$no_target_directory"; then
273350952b9Smrg	echo "$0: $dst_arg: Is a directory" >&2
274350952b9Smrg	exit 1
275350952b9Smrg      fi
276350952b9Smrg      dstdir=$dst
277350952b9Smrg      dst=$dstdir/`basename "$src"`
278350952b9Smrg      dstdir_status=0
279350952b9Smrg    else
280350952b9Smrg      # Prefer dirname, but fall back on a substitute if dirname fails.
281350952b9Smrg      dstdir=`
282350952b9Smrg	(dirname "$dst") 2>/dev/null ||
283350952b9Smrg	expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
284350952b9Smrg	     X"$dst" : 'X\(//\)[^/]' \| \
285350952b9Smrg	     X"$dst" : 'X\(//\)$' \| \
286350952b9Smrg	     X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
287350952b9Smrg	echo X"$dst" |
288350952b9Smrg	    sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
289350952b9Smrg		   s//\1/
290350952b9Smrg		   q
291350952b9Smrg		 }
292350952b9Smrg		 /^X\(\/\/\)[^/].*/{
293350952b9Smrg		   s//\1/
294350952b9Smrg		   q
295350952b9Smrg		 }
296350952b9Smrg		 /^X\(\/\/\)$/{
297350952b9Smrg		   s//\1/
298350952b9Smrg		   q
299350952b9Smrg		 }
300350952b9Smrg		 /^X\(\/\).*/{
301350952b9Smrg		   s//\1/
302350952b9Smrg		   q
303350952b9Smrg		 }
304350952b9Smrg		 s/.*/./; q'
305350952b9Smrg      `
306350952b9Smrg
307350952b9Smrg      test -d "$dstdir"
308350952b9Smrg      dstdir_status=$?
309350952b9Smrg    fi
310350952b9Smrg  fi
311350952b9Smrg
312350952b9Smrg  obsolete_mkdir_used=false
313350952b9Smrg
314350952b9Smrg  if test $dstdir_status != 0; then
315350952b9Smrg    case $posix_mkdir in
316350952b9Smrg      '')
317350952b9Smrg	# Create intermediate dirs using mode 755 as modified by the umask.
318350952b9Smrg	# This is like FreeBSD 'install' as of 1997-10-28.
319350952b9Smrg	umask=`umask`
320350952b9Smrg	case $stripcmd.$umask in
321350952b9Smrg	  # Optimize common cases.
322350952b9Smrg	  *[2367][2367]) mkdir_umask=$umask;;
323350952b9Smrg	  .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
324350952b9Smrg
325350952b9Smrg	  *[0-7])
326350952b9Smrg	    mkdir_umask=`expr $umask + 22 \
327350952b9Smrg	      - $umask % 100 % 40 + $umask % 20 \
328350952b9Smrg	      - $umask % 10 % 4 + $umask % 2
329350952b9Smrg	    `;;
330350952b9Smrg	  *) mkdir_umask=$umask,go-w;;
331350952b9Smrg	esac
332350952b9Smrg
333350952b9Smrg	# With -d, create the new directory with the user-specified mode.
334350952b9Smrg	# Otherwise, rely on $mkdir_umask.
335350952b9Smrg	if test -n "$dir_arg"; then
336350952b9Smrg	  mkdir_mode=-m$mode
337fd7d9bd3Smrg	else
338350952b9Smrg	  mkdir_mode=
339fd7d9bd3Smrg	fi
340fd7d9bd3Smrg
341350952b9Smrg	posix_mkdir=false
342350952b9Smrg	case $umask in
343350952b9Smrg	  *[123567][0-7][0-7])
344350952b9Smrg	    # POSIX mkdir -p sets u+wx bits regardless of umask, which
345350952b9Smrg	    # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
346350952b9Smrg	    ;;
347350952b9Smrg	  *)
348350952b9Smrg	    tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
349350952b9Smrg	    trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
350350952b9Smrg
351350952b9Smrg	    if (umask $mkdir_umask &&
352350952b9Smrg		exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
353350952b9Smrg	    then
354350952b9Smrg	      if test -z "$dir_arg" || {
355350952b9Smrg		   # Check for POSIX incompatibilities with -m.
356350952b9Smrg		   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
357350952b9Smrg		   # other-writeable bit of parent directory when it shouldn't.
358350952b9Smrg		   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
359350952b9Smrg		   ls_ld_tmpdir=`ls -ld "$tmpdir"`
360350952b9Smrg		   case $ls_ld_tmpdir in
361350952b9Smrg		     d????-?r-*) different_mode=700;;
362350952b9Smrg		     d????-?--*) different_mode=755;;
363350952b9Smrg		     *) false;;
364350952b9Smrg		   esac &&
365350952b9Smrg		   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
366350952b9Smrg		     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
367350952b9Smrg		     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368350952b9Smrg		   }
369350952b9Smrg		 }
370350952b9Smrg	      then posix_mkdir=:
371350952b9Smrg	      fi
372350952b9Smrg	      rmdir "$tmpdir/d" "$tmpdir"
373350952b9Smrg	    else
374350952b9Smrg	      # Remove any dirs left behind by ancient mkdir implementations.
375350952b9Smrg	      rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
376350952b9Smrg	    fi
377350952b9Smrg	    trap '' 0;;
378350952b9Smrg	esac;;
379350952b9Smrg    esac
380fd7d9bd3Smrg
381350952b9Smrg    if
382350952b9Smrg      $posix_mkdir && (
383350952b9Smrg	umask $mkdir_umask &&
384350952b9Smrg	$doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385350952b9Smrg      )
386350952b9Smrg    then :
387350952b9Smrg    else
388350952b9Smrg
389350952b9Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
390350952b9Smrg      # or it failed possibly due to a race condition.  Create the
391350952b9Smrg      # directory the slow way, step by step, checking for races as we go.
392350952b9Smrg
393350952b9Smrg      case $dstdir in
394350952b9Smrg	/*) prefix='/';;
395a85aecdfSmrg	[-=\(\)!]*) prefix='./';;
396350952b9Smrg	*)  prefix='';;
397350952b9Smrg      esac
398350952b9Smrg
399350952b9Smrg      eval "$initialize_posix_glob"
400350952b9Smrg
401350952b9Smrg      oIFS=$IFS
402350952b9Smrg      IFS=/
403350952b9Smrg      $posix_glob set -f
404350952b9Smrg      set fnord $dstdir
405350952b9Smrg      shift
406350952b9Smrg      $posix_glob set +f
407350952b9Smrg      IFS=$oIFS
408350952b9Smrg
409350952b9Smrg      prefixes=
410350952b9Smrg
411350952b9Smrg      for d
412350952b9Smrg      do
413a85aecdfSmrg	test X"$d" = X && continue
414350952b9Smrg
415350952b9Smrg	prefix=$prefix$d
416350952b9Smrg	if test -d "$prefix"; then
417350952b9Smrg	  prefixes=
418350952b9Smrg	else
419350952b9Smrg	  if $posix_mkdir; then
420350952b9Smrg	    (umask=$mkdir_umask &&
421350952b9Smrg	     $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
422350952b9Smrg	    # Don't fail if two instances are running concurrently.
423350952b9Smrg	    test -d "$prefix" || exit 1
424350952b9Smrg	  else
425350952b9Smrg	    case $prefix in
426350952b9Smrg	      *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
427350952b9Smrg	      *) qprefix=$prefix;;
428350952b9Smrg	    esac
429350952b9Smrg	    prefixes="$prefixes '$qprefix'"
430350952b9Smrg	  fi
431350952b9Smrg	fi
432350952b9Smrg	prefix=$prefix/
433350952b9Smrg      done
434350952b9Smrg
435350952b9Smrg      if test -n "$prefixes"; then
436350952b9Smrg	# Don't fail if two instances are running concurrently.
437350952b9Smrg	(umask $mkdir_umask &&
438350952b9Smrg	 eval "\$doit_exec \$mkdirprog $prefixes") ||
439350952b9Smrg	  test -d "$dstdir" || exit 1
440350952b9Smrg	obsolete_mkdir_used=true
441350952b9Smrg      fi
442350952b9Smrg    fi
443350952b9Smrg  fi
444350952b9Smrg
445350952b9Smrg  if test -n "$dir_arg"; then
446350952b9Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
447350952b9Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
448350952b9Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
449350952b9Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
450350952b9Smrg  else
451350952b9Smrg
452350952b9Smrg    # Make a couple of temp file names in the proper directory.
453350952b9Smrg    dsttmp=$dstdir/_inst.$$_
454350952b9Smrg    rmtmp=$dstdir/_rm.$$_
455350952b9Smrg
456350952b9Smrg    # Trap to clean up those temp files at exit.
457350952b9Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
458350952b9Smrg
459350952b9Smrg    # Copy the file name to the temp name.
460350952b9Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
461350952b9Smrg
462350952b9Smrg    # and set any options; do chmod last to preserve setuid bits.
463350952b9Smrg    #
464350952b9Smrg    # If any of these fail, we abort the whole thing.  If we want to
465350952b9Smrg    # ignore errors from any of these, just make sure not to ignore
466350952b9Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
467350952b9Smrg    #
468350952b9Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
469350952b9Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
470350952b9Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
471350952b9Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
472350952b9Smrg
473350952b9Smrg    # If -C, don't bother to copy if it wouldn't change the file.
474350952b9Smrg    if $copy_on_change &&
475350952b9Smrg       old=`LC_ALL=C ls -dlL "$dst"	2>/dev/null` &&
476350952b9Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"	2>/dev/null` &&
477350952b9Smrg
478350952b9Smrg       eval "$initialize_posix_glob" &&
479350952b9Smrg       $posix_glob set -f &&
480350952b9Smrg       set X $old && old=:$2:$4:$5:$6 &&
481350952b9Smrg       set X $new && new=:$2:$4:$5:$6 &&
482350952b9Smrg       $posix_glob set +f &&
483350952b9Smrg
484350952b9Smrg       test "$old" = "$new" &&
485350952b9Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
486350952b9Smrg    then
487350952b9Smrg      rm -f "$dsttmp"
488350952b9Smrg    else
489350952b9Smrg      # Rename the file to the real destination.
490350952b9Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
491350952b9Smrg
492350952b9Smrg      # The rename failed, perhaps because mv can't rename something else
493350952b9Smrg      # to itself, or perhaps because mv is so ancient that it does not
494350952b9Smrg      # support -f.
495350952b9Smrg      {
496350952b9Smrg	# Now remove or move aside any old file at destination location.
497350952b9Smrg	# We try this two ways since rm can't unlink itself on some
498350952b9Smrg	# systems and the destination file might be busy for other
499350952b9Smrg	# reasons.  In this case, the final cleanup might fail but the new
500350952b9Smrg	# file should still install successfully.
501350952b9Smrg	{
502350952b9Smrg	  test ! -f "$dst" ||
503350952b9Smrg	  $doit $rmcmd -f "$dst" 2>/dev/null ||
504350952b9Smrg	  { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
505350952b9Smrg	    { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
506350952b9Smrg	  } ||
507350952b9Smrg	  { echo "$0: cannot unlink or rename $dst" >&2
508350952b9Smrg	    (exit 1); exit 1
509350952b9Smrg	  }
510350952b9Smrg	} &&
511350952b9Smrg
512350952b9Smrg	# Now rename the file to the real destination.
513350952b9Smrg	$doit $mvcmd "$dsttmp" "$dst"
514350952b9Smrg      }
515350952b9Smrg    fi || exit 1
516350952b9Smrg
517350952b9Smrg    trap '' 0
518350952b9Smrg  fi
519350952b9Smrgdone
520fd7d9bd3Smrg
521350952b9Smrg# Local variables:
522350952b9Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
523350952b9Smrg# time-stamp-start: "scriptversion="
524350952b9Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
525350952b9Smrg# time-stamp-time-zone: "UTC"
526350952b9Smrg# time-stamp-end: "; # UTC"
527350952b9Smrg# End:
528