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