install-sh revision 8d8e295f
1d4401354Smrg#!/bin/sh
2d4401354Smrg# install - install a program, script, or datafile
3d4401354Smrg
48d8e295fSmrgscriptversion=2018-03-11.20; # UTC
5d4401354Smrg
6d4401354Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7d4401354Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8d4401354Smrg# following copyright and license.
9d4401354Smrg#
10d4401354Smrg# Copyright (C) 1994 X Consortium
11d4401354Smrg#
12d4401354Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13d4401354Smrg# of this software and associated documentation files (the "Software"), to
14d4401354Smrg# deal in the Software without restriction, including without limitation the
15d4401354Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16d4401354Smrg# sell copies of the Software, and to permit persons to whom the Software is
17d4401354Smrg# furnished to do so, subject to the following conditions:
18d4401354Smrg#
19d4401354Smrg# The above copyright notice and this permission notice shall be included in
20d4401354Smrg# all copies or substantial portions of the Software.
21d4401354Smrg#
22d4401354Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23d4401354Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24d4401354Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25d4401354Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26d4401354Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27d4401354Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28d4401354Smrg#
29d4401354Smrg# Except as contained in this notice, the name of the X Consortium shall not
30d4401354Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31d4401354Smrg# ings in this Software without prior written authorization from the X Consor-
32d4401354Smrg# tium.
33d4401354Smrg#
34d4401354Smrg#
35d4401354Smrg# FSF changes to this file are in the public domain.
36d4401354Smrg#
37d4401354Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38f591e195Smrg# 'make' implicit rules from creating a file called install from it
39d4401354Smrg# when there is no Makefile.
40d4401354Smrg#
41d4401354Smrg# This script is compatible with the BSD install script, but was written
42d4401354Smrg# from scratch.
43d4401354Smrg
44891601f5Smrgtab='	'
45d4401354Smrgnl='
46d4401354Smrg'
47891601f5SmrgIFS=" $tab$nl"
48d4401354Smrg
49891601f5Smrg# Set DOITPROG to "echo" to test this script.
50d4401354Smrg
51d4401354Smrgdoit=${DOITPROG-}
52891601f5Smrgdoit_exec=${doit:-exec}
53d4401354Smrg
54d4401354Smrg# Put in absolute file names if you don't have them in your path;
55d4401354Smrg# or use environment vars.
56d4401354Smrg
57d4401354Smrgchgrpprog=${CHGRPPROG-chgrp}
58d4401354Smrgchmodprog=${CHMODPROG-chmod}
59d4401354Smrgchownprog=${CHOWNPROG-chown}
60d4401354Smrgcmpprog=${CMPPROG-cmp}
61d4401354Smrgcpprog=${CPPROG-cp}
62d4401354Smrgmkdirprog=${MKDIRPROG-mkdir}
63d4401354Smrgmvprog=${MVPROG-mv}
64d4401354Smrgrmprog=${RMPROG-rm}
65d4401354Smrgstripprog=${STRIPPROG-strip}
66d4401354Smrg
67d4401354Smrgposix_mkdir=
68d4401354Smrg
69d4401354Smrg# Desired mode of installed file.
70d4401354Smrgmode=0755
71d4401354Smrg
72d4401354Smrgchgrpcmd=
73d4401354Smrgchmodcmd=$chmodprog
74d4401354Smrgchowncmd=
75d4401354Smrgmvcmd=$mvprog
76d4401354Smrgrmcmd="$rmprog -f"
77d4401354Smrgstripcmd=
78d4401354Smrg
79d4401354Smrgsrc=
80d4401354Smrgdst=
81d4401354Smrgdir_arg=
82d4401354Smrgdst_arg=
83d4401354Smrg
84d4401354Smrgcopy_on_change=false
85891601f5Smrgis_target_a_directory=possibly
86d4401354Smrg
87d4401354Smrgusage="\
88d4401354SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89d4401354Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90d4401354Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91d4401354Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92d4401354Smrg
93d4401354SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94d4401354SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95d4401354SmrgIn the 4th, create DIRECTORIES.
96d4401354Smrg
97d4401354SmrgOptions:
98d4401354Smrg     --help     display this help and exit.
99d4401354Smrg     --version  display version info and exit.
100d4401354Smrg
101d4401354Smrg  -c            (ignored)
102d4401354Smrg  -C            install only if different (preserve the last data modification time)
103d4401354Smrg  -d            create directories instead of installing files.
104d4401354Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105d4401354Smrg  -m MODE       $chmodprog installed files to MODE.
106d4401354Smrg  -o USER       $chownprog installed files to USER.
107d4401354Smrg  -s            $stripprog installed files.
108d4401354Smrg  -t DIRECTORY  install into DIRECTORY.
109d4401354Smrg  -T            report an error if DSTFILE is a directory.
110d4401354Smrg
111d4401354SmrgEnvironment variables override the default commands:
112d4401354Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113d4401354Smrg  RMPROG STRIPPROG
114d4401354Smrg"
115d4401354Smrg
116d4401354Smrgwhile test $# -ne 0; do
117d4401354Smrg  case $1 in
118d4401354Smrg    -c) ;;
119d4401354Smrg
120d4401354Smrg    -C) copy_on_change=true;;
121d4401354Smrg
122d4401354Smrg    -d) dir_arg=true;;
123d4401354Smrg
124d4401354Smrg    -g) chgrpcmd="$chgrpprog $2"
125891601f5Smrg        shift;;
126d4401354Smrg
127d4401354Smrg    --help) echo "$usage"; exit $?;;
128d4401354Smrg
129d4401354Smrg    -m) mode=$2
130891601f5Smrg        case $mode in
131891601f5Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132891601f5Smrg            echo "$0: invalid mode: $mode" >&2
133891601f5Smrg            exit 1;;
134891601f5Smrg        esac
135891601f5Smrg        shift;;
136d4401354Smrg
137d4401354Smrg    -o) chowncmd="$chownprog $2"
138891601f5Smrg        shift;;
139d4401354Smrg
140d4401354Smrg    -s) stripcmd=$stripprog;;
141d4401354Smrg
142891601f5Smrg    -t)
143891601f5Smrg        is_target_a_directory=always
144891601f5Smrg        dst_arg=$2
145891601f5Smrg        # Protect names problematic for 'test' and other utilities.
146891601f5Smrg        case $dst_arg in
147891601f5Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148891601f5Smrg        esac
149891601f5Smrg        shift;;
150d4401354Smrg
151891601f5Smrg    -T) is_target_a_directory=never;;
152d4401354Smrg
153d4401354Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154d4401354Smrg
155891601f5Smrg    --) shift
156891601f5Smrg        break;;
157d4401354Smrg
158891601f5Smrg    -*) echo "$0: invalid option: $1" >&2
159891601f5Smrg        exit 1;;
160d4401354Smrg
161d4401354Smrg    *)  break;;
162d4401354Smrg  esac
163d4401354Smrg  shift
164d4401354Smrgdone
165d4401354Smrg
166891601f5Smrg# We allow the use of options -d and -T together, by making -d
167891601f5Smrg# take the precedence; this is for compatibility with GNU install.
168891601f5Smrg
169891601f5Smrgif test -n "$dir_arg"; then
170891601f5Smrg  if test -n "$dst_arg"; then
171891601f5Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172891601f5Smrg    exit 1
173891601f5Smrg  fi
174891601f5Smrgfi
175891601f5Smrg
176d4401354Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177d4401354Smrg  # When -d is used, all remaining arguments are directories to create.
178d4401354Smrg  # When -t is used, the destination is already specified.
179d4401354Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180d4401354Smrg  for arg
181d4401354Smrg  do
182d4401354Smrg    if test -n "$dst_arg"; then
183d4401354Smrg      # $@ is not empty: it contains at least $arg.
184d4401354Smrg      set fnord "$@" "$dst_arg"
185d4401354Smrg      shift # fnord
186d4401354Smrg    fi
187d4401354Smrg    shift # arg
188d4401354Smrg    dst_arg=$arg
189f591e195Smrg    # Protect names problematic for 'test' and other utilities.
190986c8b3dSmrg    case $dst_arg in
191986c8b3dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192986c8b3dSmrg    esac
193d4401354Smrg  done
194d4401354Smrgfi
195d4401354Smrg
196d4401354Smrgif test $# -eq 0; then
197d4401354Smrg  if test -z "$dir_arg"; then
198d4401354Smrg    echo "$0: no input file specified." >&2
199d4401354Smrg    exit 1
200d4401354Smrg  fi
201f591e195Smrg  # It's OK to call 'install-sh -d' without argument.
202d4401354Smrg  # This can happen when creating conditional directories.
203d4401354Smrg  exit 0
204d4401354Smrgfi
205d4401354Smrg
206891601f5Smrgif test -z "$dir_arg"; then
207891601f5Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208891601f5Smrg    if test ! -d "$dst_arg"; then
209891601f5Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210891601f5Smrg      exit 1
211891601f5Smrg    fi
212891601f5Smrg  fi
213891601f5Smrgfi
214891601f5Smrg
215d4401354Smrgif test -z "$dir_arg"; then
216986c8b3dSmrg  do_exit='(exit $ret); exit $ret'
217986c8b3dSmrg  trap "ret=129; $do_exit" 1
218986c8b3dSmrg  trap "ret=130; $do_exit" 2
219986c8b3dSmrg  trap "ret=141; $do_exit" 13
220986c8b3dSmrg  trap "ret=143; $do_exit" 15
221d4401354Smrg
222d4401354Smrg  # Set umask so as not to create temps with too-generous modes.
223d4401354Smrg  # However, 'strip' requires both read and write access to temps.
224d4401354Smrg  case $mode in
225d4401354Smrg    # Optimize common cases.
226d4401354Smrg    *644) cp_umask=133;;
227d4401354Smrg    *755) cp_umask=22;;
228d4401354Smrg
229d4401354Smrg    *[0-7])
230d4401354Smrg      if test -z "$stripcmd"; then
231891601f5Smrg        u_plus_rw=
232d4401354Smrg      else
233891601f5Smrg        u_plus_rw='% 200'
234d4401354Smrg      fi
235d4401354Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236d4401354Smrg    *)
237d4401354Smrg      if test -z "$stripcmd"; then
238891601f5Smrg        u_plus_rw=
239d4401354Smrg      else
240891601f5Smrg        u_plus_rw=,u+rw
241d4401354Smrg      fi
242d4401354Smrg      cp_umask=$mode$u_plus_rw;;
243d4401354Smrg  esac
244d4401354Smrgfi
245d4401354Smrg
246d4401354Smrgfor src
247d4401354Smrgdo
248f591e195Smrg  # Protect names problematic for 'test' and other utilities.
249d4401354Smrg  case $src in
250986c8b3dSmrg    -* | [=\(\)!]) src=./$src;;
251d4401354Smrg  esac
252d4401354Smrg
253d4401354Smrg  if test -n "$dir_arg"; then
254d4401354Smrg    dst=$src
255d4401354Smrg    dstdir=$dst
256d4401354Smrg    test -d "$dstdir"
257d4401354Smrg    dstdir_status=$?
258d4401354Smrg  else
259d4401354Smrg
260d4401354Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261d4401354Smrg    # might cause directories to be created, which would be especially bad
262d4401354Smrg    # if $src (and thus $dsttmp) contains '*'.
263d4401354Smrg    if test ! -f "$src" && test ! -d "$src"; then
264d4401354Smrg      echo "$0: $src does not exist." >&2
265d4401354Smrg      exit 1
266d4401354Smrg    fi
267d4401354Smrg
268d4401354Smrg    if test -z "$dst_arg"; then
269d4401354Smrg      echo "$0: no destination specified." >&2
270d4401354Smrg      exit 1
271d4401354Smrg    fi
272d4401354Smrg    dst=$dst_arg
273d4401354Smrg
2748d8e295fSmrg    # If destination is a directory, append the input filename.
275d4401354Smrg    if test -d "$dst"; then
276891601f5Smrg      if test "$is_target_a_directory" = never; then
277891601f5Smrg        echo "$0: $dst_arg: Is a directory" >&2
278891601f5Smrg        exit 1
279d4401354Smrg      fi
280d4401354Smrg      dstdir=$dst
2818d8e295fSmrg      dstbase=`basename "$src"`
2828d8e295fSmrg      case $dst in
2838d8e295fSmrg	*/) dst=$dst$dstbase;;
2848d8e295fSmrg	*)  dst=$dst/$dstbase;;
2858d8e295fSmrg      esac
286d4401354Smrg      dstdir_status=0
287d4401354Smrg    else
288891601f5Smrg      dstdir=`dirname "$dst"`
289d4401354Smrg      test -d "$dstdir"
290d4401354Smrg      dstdir_status=$?
291d4401354Smrg    fi
292d4401354Smrg  fi
293d4401354Smrg
2948d8e295fSmrg  case $dstdir in
2958d8e295fSmrg    */) dstdirslash=$dstdir;;
2968d8e295fSmrg    *)  dstdirslash=$dstdir/;;
2978d8e295fSmrg  esac
2988d8e295fSmrg
299d4401354Smrg  obsolete_mkdir_used=false
300d4401354Smrg
301d4401354Smrg  if test $dstdir_status != 0; then
302d4401354Smrg    case $posix_mkdir in
303d4401354Smrg      '')
304891601f5Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
305891601f5Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
306891601f5Smrg        umask=`umask`
307891601f5Smrg        case $stripcmd.$umask in
308891601f5Smrg          # Optimize common cases.
309891601f5Smrg          *[2367][2367]) mkdir_umask=$umask;;
310891601f5Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311891601f5Smrg
312891601f5Smrg          *[0-7])
313891601f5Smrg            mkdir_umask=`expr $umask + 22 \
314891601f5Smrg              - $umask % 100 % 40 + $umask % 20 \
315891601f5Smrg              - $umask % 10 % 4 + $umask % 2
316891601f5Smrg            `;;
317891601f5Smrg          *) mkdir_umask=$umask,go-w;;
318891601f5Smrg        esac
319891601f5Smrg
320891601f5Smrg        # With -d, create the new directory with the user-specified mode.
321891601f5Smrg        # Otherwise, rely on $mkdir_umask.
322891601f5Smrg        if test -n "$dir_arg"; then
323891601f5Smrg          mkdir_mode=-m$mode
324891601f5Smrg        else
325891601f5Smrg          mkdir_mode=
326891601f5Smrg        fi
327891601f5Smrg
328891601f5Smrg        posix_mkdir=false
329891601f5Smrg        case $umask in
330891601f5Smrg          *[123567][0-7][0-7])
331891601f5Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
332891601f5Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333891601f5Smrg            ;;
334891601f5Smrg          *)
3358d8e295fSmrg            # Note that $RANDOM variable is not portable (e.g. dash);  Use it
3368d8e295fSmrg            # here however when possible just to lower collision chance.
337891601f5Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
338891601f5Smrg
3398d8e295fSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
3408d8e295fSmrg
3418d8e295fSmrg            # Because "mkdir -p" follows existing symlinks and we likely work
3428d8e295fSmrg            # directly in world-writeable /tmp, make sure that the '$tmpdir'
3438d8e295fSmrg            # directory is successfully created first before we actually test
3448d8e295fSmrg            # 'mkdir -p' feature.
345891601f5Smrg            if (umask $mkdir_umask &&
3468d8e295fSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
3478d8e295fSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
348891601f5Smrg            then
349891601f5Smrg              if test -z "$dir_arg" || {
350891601f5Smrg                   # Check for POSIX incompatibilities with -m.
351891601f5Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
352891601f5Smrg                   # other-writable bit of parent directory when it shouldn't.
353891601f5Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3548d8e295fSmrg                   test_tmpdir="$tmpdir/a"
3558d8e295fSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
356891601f5Smrg                   case $ls_ld_tmpdir in
357891601f5Smrg                     d????-?r-*) different_mode=700;;
358891601f5Smrg                     d????-?--*) different_mode=755;;
359891601f5Smrg                     *) false;;
360891601f5Smrg                   esac &&
3618d8e295fSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3628d8e295fSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
363891601f5Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
364891601f5Smrg                   }
365891601f5Smrg                 }
366891601f5Smrg              then posix_mkdir=:
367891601f5Smrg              fi
3688d8e295fSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
369891601f5Smrg            else
370891601f5Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3718d8e295fSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
372891601f5Smrg            fi
373891601f5Smrg            trap '' 0;;
374891601f5Smrg        esac;;
375d4401354Smrg    esac
376d4401354Smrg
377d4401354Smrg    if
378d4401354Smrg      $posix_mkdir && (
379891601f5Smrg        umask $mkdir_umask &&
380891601f5Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
381d4401354Smrg      )
382d4401354Smrg    then :
383d4401354Smrg    else
384d4401354Smrg
385d4401354Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
386d4401354Smrg      # or it failed possibly due to a race condition.  Create the
387d4401354Smrg      # directory the slow way, step by step, checking for races as we go.
388d4401354Smrg
389d4401354Smrg      case $dstdir in
390891601f5Smrg        /*) prefix='/';;
391891601f5Smrg        [-=\(\)!]*) prefix='./';;
392891601f5Smrg        *)  prefix='';;
393d4401354Smrg      esac
394d4401354Smrg
395d4401354Smrg      oIFS=$IFS
396d4401354Smrg      IFS=/
397891601f5Smrg      set -f
398d4401354Smrg      set fnord $dstdir
399d4401354Smrg      shift
400891601f5Smrg      set +f
401d4401354Smrg      IFS=$oIFS
402d4401354Smrg
403d4401354Smrg      prefixes=
404d4401354Smrg
405d4401354Smrg      for d
406d4401354Smrg      do
407891601f5Smrg        test X"$d" = X && continue
408891601f5Smrg
409891601f5Smrg        prefix=$prefix$d
410891601f5Smrg        if test -d "$prefix"; then
411891601f5Smrg          prefixes=
412891601f5Smrg        else
413891601f5Smrg          if $posix_mkdir; then
414891601f5Smrg            (umask=$mkdir_umask &&
415891601f5Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416891601f5Smrg            # Don't fail if two instances are running concurrently.
417891601f5Smrg            test -d "$prefix" || exit 1
418891601f5Smrg          else
419891601f5Smrg            case $prefix in
420891601f5Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421891601f5Smrg              *) qprefix=$prefix;;
422891601f5Smrg            esac
423891601f5Smrg            prefixes="$prefixes '$qprefix'"
424891601f5Smrg          fi
425891601f5Smrg        fi
426891601f5Smrg        prefix=$prefix/
427d4401354Smrg      done
428d4401354Smrg
429d4401354Smrg      if test -n "$prefixes"; then
430891601f5Smrg        # Don't fail if two instances are running concurrently.
431891601f5Smrg        (umask $mkdir_umask &&
432891601f5Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
433891601f5Smrg          test -d "$dstdir" || exit 1
434891601f5Smrg        obsolete_mkdir_used=true
435d4401354Smrg      fi
436d4401354Smrg    fi
437d4401354Smrg  fi
438d4401354Smrg
439d4401354Smrg  if test -n "$dir_arg"; then
440d4401354Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
441d4401354Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
442d4401354Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
443d4401354Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444d4401354Smrg  else
445d4401354Smrg
446d4401354Smrg    # Make a couple of temp file names in the proper directory.
4478d8e295fSmrg    dsttmp=${dstdirslash}_inst.$$_
4488d8e295fSmrg    rmtmp=${dstdirslash}_rm.$$_
449d4401354Smrg
450d4401354Smrg    # Trap to clean up those temp files at exit.
451d4401354Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452d4401354Smrg
453d4401354Smrg    # Copy the file name to the temp name.
454d4401354Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
455d4401354Smrg
456d4401354Smrg    # and set any options; do chmod last to preserve setuid bits.
457d4401354Smrg    #
458d4401354Smrg    # If any of these fail, we abort the whole thing.  If we want to
459d4401354Smrg    # ignore errors from any of these, just make sure not to ignore
460d4401354Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
461d4401354Smrg    #
462d4401354Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
463d4401354Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
464d4401354Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
465d4401354Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466d4401354Smrg
467d4401354Smrg    # If -C, don't bother to copy if it wouldn't change the file.
468d4401354Smrg    if $copy_on_change &&
469891601f5Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
470891601f5Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
471891601f5Smrg       set -f &&
472d4401354Smrg       set X $old && old=:$2:$4:$5:$6 &&
473d4401354Smrg       set X $new && new=:$2:$4:$5:$6 &&
474891601f5Smrg       set +f &&
475d4401354Smrg       test "$old" = "$new" &&
476d4401354Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
477d4401354Smrg    then
478d4401354Smrg      rm -f "$dsttmp"
479d4401354Smrg    else
480d4401354Smrg      # Rename the file to the real destination.
481d4401354Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
482d4401354Smrg
483d4401354Smrg      # The rename failed, perhaps because mv can't rename something else
484d4401354Smrg      # to itself, or perhaps because mv is so ancient that it does not
485d4401354Smrg      # support -f.
486d4401354Smrg      {
487891601f5Smrg        # Now remove or move aside any old file at destination location.
488891601f5Smrg        # We try this two ways since rm can't unlink itself on some
489891601f5Smrg        # systems and the destination file might be busy for other
490891601f5Smrg        # reasons.  In this case, the final cleanup might fail but the new
491891601f5Smrg        # file should still install successfully.
492891601f5Smrg        {
493891601f5Smrg          test ! -f "$dst" ||
494891601f5Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
495891601f5Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
496891601f5Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
497891601f5Smrg          } ||
498891601f5Smrg          { echo "$0: cannot unlink or rename $dst" >&2
499891601f5Smrg            (exit 1); exit 1
500891601f5Smrg          }
501891601f5Smrg        } &&
502891601f5Smrg
503891601f5Smrg        # Now rename the file to the real destination.
504891601f5Smrg        $doit $mvcmd "$dsttmp" "$dst"
505d4401354Smrg      }
506d4401354Smrg    fi || exit 1
507d4401354Smrg
508d4401354Smrg    trap '' 0
509d4401354Smrg  fi
510d4401354Smrgdone
511d4401354Smrg
512d4401354Smrg# Local variables:
5138d8e295fSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
514d4401354Smrg# time-stamp-start: "scriptversion="
515d4401354Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
516a27842ffSmrg# time-stamp-time-zone: "UTC0"
517d4401354Smrg# time-stamp-end: "; # UTC"
518d4401354Smrg# End:
519