install-sh revision 300346ae
1126a8a12Smrg#!/bin/sh
2126a8a12Smrg# install - install a program, script, or datafile
3126a8a12Smrg
4300346aeSmrgscriptversion=2014-09-12.12; # UTC
5126a8a12Smrg
6126a8a12Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7126a8a12Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8126a8a12Smrg# following copyright and license.
9126a8a12Smrg#
10126a8a12Smrg# Copyright (C) 1994 X Consortium
11126a8a12Smrg#
12126a8a12Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13126a8a12Smrg# of this software and associated documentation files (the "Software"), to
14126a8a12Smrg# deal in the Software without restriction, including without limitation the
15126a8a12Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16126a8a12Smrg# sell copies of the Software, and to permit persons to whom the Software is
17126a8a12Smrg# furnished to do so, subject to the following conditions:
18126a8a12Smrg#
19126a8a12Smrg# The above copyright notice and this permission notice shall be included in
20126a8a12Smrg# all copies or substantial portions of the Software.
21126a8a12Smrg#
22126a8a12Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23126a8a12Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24126a8a12Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25126a8a12Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26126a8a12Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27126a8a12Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28126a8a12Smrg#
29126a8a12Smrg# Except as contained in this notice, the name of the X Consortium shall not
30126a8a12Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31126a8a12Smrg# ings in this Software without prior written authorization from the X Consor-
32126a8a12Smrg# tium.
33126a8a12Smrg#
34126a8a12Smrg#
35126a8a12Smrg# FSF changes to this file are in the public domain.
36126a8a12Smrg#
37126a8a12Smrg# Calling this script install-sh is preferred over install.sh, to prevent
380a6b08f8Smrg# 'make' implicit rules from creating a file called install from it
39126a8a12Smrg# when there is no Makefile.
40126a8a12Smrg#
41126a8a12Smrg# This script is compatible with the BSD install script, but was written
42126a8a12Smrg# from scratch.
43126a8a12Smrg
44300346aeSmrgtab='	'
45126a8a12Smrgnl='
46126a8a12Smrg'
47300346aeSmrgIFS=" $tab$nl"
48126a8a12Smrg
49300346aeSmrg# Set DOITPROG to "echo" to test this script.
50126a8a12Smrg
51d656433aSmrgdoit=${DOITPROG-}
52300346aeSmrgdoit_exec=${doit:-exec}
53126a8a12Smrg
54126a8a12Smrg# Put in absolute file names if you don't have them in your path;
55126a8a12Smrg# or use environment vars.
56126a8a12Smrg
57d656433aSmrgchgrpprog=${CHGRPPROG-chgrp}
58d656433aSmrgchmodprog=${CHMODPROG-chmod}
59d656433aSmrgchownprog=${CHOWNPROG-chown}
60d656433aSmrgcmpprog=${CMPPROG-cmp}
61d656433aSmrgcpprog=${CPPROG-cp}
62d656433aSmrgmkdirprog=${MKDIRPROG-mkdir}
63d656433aSmrgmvprog=${MVPROG-mv}
64d656433aSmrgrmprog=${RMPROG-rm}
65d656433aSmrgstripprog=${STRIPPROG-strip}
66d656433aSmrg
67126a8a12Smrgposix_mkdir=
68126a8a12Smrg
69126a8a12Smrg# Desired mode of installed file.
70126a8a12Smrgmode=0755
71126a8a12Smrg
72d656433aSmrgchgrpcmd=
73126a8a12Smrgchmodcmd=$chmodprog
74126a8a12Smrgchowncmd=
75d656433aSmrgmvcmd=$mvprog
76126a8a12Smrgrmcmd="$rmprog -f"
77d656433aSmrgstripcmd=
78d656433aSmrg
79126a8a12Smrgsrc=
80126a8a12Smrgdst=
81126a8a12Smrgdir_arg=
82d656433aSmrgdst_arg=
83d656433aSmrg
84d656433aSmrgcopy_on_change=false
85300346aeSmrgis_target_a_directory=possibly
86126a8a12Smrg
87d656433aSmrgusage="\
88d656433aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89126a8a12Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90126a8a12Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91126a8a12Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92126a8a12Smrg
93126a8a12SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94126a8a12SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95126a8a12SmrgIn the 4th, create DIRECTORIES.
96126a8a12Smrg
97126a8a12SmrgOptions:
98d656433aSmrg     --help     display this help and exit.
99d656433aSmrg     --version  display version info and exit.
100d656433aSmrg
101d656433aSmrg  -c            (ignored)
102d656433aSmrg  -C            install only if different (preserve the last data modification time)
103d656433aSmrg  -d            create directories instead of installing files.
104d656433aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105d656433aSmrg  -m MODE       $chmodprog installed files to MODE.
106d656433aSmrg  -o USER       $chownprog installed files to USER.
107d656433aSmrg  -s            $stripprog installed files.
108d656433aSmrg  -t DIRECTORY  install into DIRECTORY.
109d656433aSmrg  -T            report an error if DSTFILE is a directory.
110126a8a12Smrg
111126a8a12SmrgEnvironment variables override the default commands:
112d656433aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113d656433aSmrg  RMPROG STRIPPROG
114126a8a12Smrg"
115126a8a12Smrg
116126a8a12Smrgwhile test $# -ne 0; do
117126a8a12Smrg  case $1 in
118d656433aSmrg    -c) ;;
119d656433aSmrg
120d656433aSmrg    -C) copy_on_change=true;;
121126a8a12Smrg
122d656433aSmrg    -d) dir_arg=true;;
123126a8a12Smrg
124126a8a12Smrg    -g) chgrpcmd="$chgrpprog $2"
125300346aeSmrg        shift;;
126126a8a12Smrg
127126a8a12Smrg    --help) echo "$usage"; exit $?;;
128126a8a12Smrg
129126a8a12Smrg    -m) mode=$2
130300346aeSmrg        case $mode in
131300346aeSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132300346aeSmrg            echo "$0: invalid mode: $mode" >&2
133300346aeSmrg            exit 1;;
134300346aeSmrg        esac
135300346aeSmrg        shift;;
136126a8a12Smrg
137126a8a12Smrg    -o) chowncmd="$chownprog $2"
138300346aeSmrg        shift;;
139126a8a12Smrg
140d656433aSmrg    -s) stripcmd=$stripprog;;
141126a8a12Smrg
142300346aeSmrg    -t)
143300346aeSmrg        is_target_a_directory=always
144300346aeSmrg        dst_arg=$2
145300346aeSmrg        # Protect names problematic for 'test' and other utilities.
146300346aeSmrg        case $dst_arg in
147300346aeSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148300346aeSmrg        esac
149300346aeSmrg        shift;;
150126a8a12Smrg
151300346aeSmrg    -T) is_target_a_directory=never;;
152126a8a12Smrg
153126a8a12Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154126a8a12Smrg
155300346aeSmrg    --) shift
156300346aeSmrg        break;;
157126a8a12Smrg
158300346aeSmrg    -*) echo "$0: invalid option: $1" >&2
159300346aeSmrg        exit 1;;
160126a8a12Smrg
161126a8a12Smrg    *)  break;;
162126a8a12Smrg  esac
163d656433aSmrg  shift
164126a8a12Smrgdone
165126a8a12Smrg
166300346aeSmrg# We allow the use of options -d and -T together, by making -d
167300346aeSmrg# take the precedence; this is for compatibility with GNU install.
168300346aeSmrg
169300346aeSmrgif test -n "$dir_arg"; then
170300346aeSmrg  if test -n "$dst_arg"; then
171300346aeSmrg    echo "$0: target directory not allowed when installing a directory." >&2
172300346aeSmrg    exit 1
173300346aeSmrg  fi
174300346aeSmrgfi
175300346aeSmrg
176d656433aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177126a8a12Smrg  # When -d is used, all remaining arguments are directories to create.
178126a8a12Smrg  # When -t is used, the destination is already specified.
179126a8a12Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180126a8a12Smrg  for arg
181126a8a12Smrg  do
182d656433aSmrg    if test -n "$dst_arg"; then
183126a8a12Smrg      # $@ is not empty: it contains at least $arg.
184d656433aSmrg      set fnord "$@" "$dst_arg"
185126a8a12Smrg      shift # fnord
186126a8a12Smrg    fi
187126a8a12Smrg    shift # arg
188d656433aSmrg    dst_arg=$arg
1890a6b08f8Smrg    # Protect names problematic for 'test' and other utilities.
1900a6b08f8Smrg    case $dst_arg in
1910a6b08f8Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1920a6b08f8Smrg    esac
193126a8a12Smrg  done
194126a8a12Smrgfi
195126a8a12Smrg
196126a8a12Smrgif test $# -eq 0; then
197126a8a12Smrg  if test -z "$dir_arg"; then
198126a8a12Smrg    echo "$0: no input file specified." >&2
199126a8a12Smrg    exit 1
200126a8a12Smrg  fi
2010a6b08f8Smrg  # It's OK to call 'install-sh -d' without argument.
202126a8a12Smrg  # This can happen when creating conditional directories.
203126a8a12Smrg  exit 0
204126a8a12Smrgfi
205126a8a12Smrg
206300346aeSmrgif test -z "$dir_arg"; then
207300346aeSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208300346aeSmrg    if test ! -d "$dst_arg"; then
209300346aeSmrg      echo "$0: $dst_arg: Is not a directory." >&2
210300346aeSmrg      exit 1
211300346aeSmrg    fi
212300346aeSmrg  fi
213300346aeSmrgfi
214300346aeSmrg
215126a8a12Smrgif test -z "$dir_arg"; then
2160a6b08f8Smrg  do_exit='(exit $ret); exit $ret'
2170a6b08f8Smrg  trap "ret=129; $do_exit" 1
2180a6b08f8Smrg  trap "ret=130; $do_exit" 2
2190a6b08f8Smrg  trap "ret=141; $do_exit" 13
2200a6b08f8Smrg  trap "ret=143; $do_exit" 15
221126a8a12Smrg
222126a8a12Smrg  # Set umask so as not to create temps with too-generous modes.
223126a8a12Smrg  # However, 'strip' requires both read and write access to temps.
224126a8a12Smrg  case $mode in
225126a8a12Smrg    # Optimize common cases.
226126a8a12Smrg    *644) cp_umask=133;;
227126a8a12Smrg    *755) cp_umask=22;;
228126a8a12Smrg
229126a8a12Smrg    *[0-7])
230126a8a12Smrg      if test -z "$stripcmd"; then
231300346aeSmrg        u_plus_rw=
232126a8a12Smrg      else
233300346aeSmrg        u_plus_rw='% 200'
234126a8a12Smrg      fi
235126a8a12Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236126a8a12Smrg    *)
237126a8a12Smrg      if test -z "$stripcmd"; then
238300346aeSmrg        u_plus_rw=
239126a8a12Smrg      else
240300346aeSmrg        u_plus_rw=,u+rw
241126a8a12Smrg      fi
242126a8a12Smrg      cp_umask=$mode$u_plus_rw;;
243126a8a12Smrg  esac
244126a8a12Smrgfi
245126a8a12Smrg
246126a8a12Smrgfor src
247126a8a12Smrgdo
2480a6b08f8Smrg  # Protect names problematic for 'test' and other utilities.
249126a8a12Smrg  case $src in
2500a6b08f8Smrg    -* | [=\(\)!]) src=./$src;;
251126a8a12Smrg  esac
252126a8a12Smrg
253126a8a12Smrg  if test -n "$dir_arg"; then
254126a8a12Smrg    dst=$src
255126a8a12Smrg    dstdir=$dst
256126a8a12Smrg    test -d "$dstdir"
257126a8a12Smrg    dstdir_status=$?
258126a8a12Smrg  else
259126a8a12Smrg
260126a8a12Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261126a8a12Smrg    # might cause directories to be created, which would be especially bad
262126a8a12Smrg    # if $src (and thus $dsttmp) contains '*'.
263126a8a12Smrg    if test ! -f "$src" && test ! -d "$src"; then
264126a8a12Smrg      echo "$0: $src does not exist." >&2
265126a8a12Smrg      exit 1
266126a8a12Smrg    fi
267126a8a12Smrg
268d656433aSmrg    if test -z "$dst_arg"; then
269126a8a12Smrg      echo "$0: no destination specified." >&2
270126a8a12Smrg      exit 1
271126a8a12Smrg    fi
272d656433aSmrg    dst=$dst_arg
273126a8a12Smrg
274126a8a12Smrg    # If destination is a directory, append the input filename; won't work
275126a8a12Smrg    # if double slashes aren't ignored.
276126a8a12Smrg    if test -d "$dst"; then
277300346aeSmrg      if test "$is_target_a_directory" = never; then
278300346aeSmrg        echo "$0: $dst_arg: Is a directory" >&2
279300346aeSmrg        exit 1
280126a8a12Smrg      fi
281126a8a12Smrg      dstdir=$dst
282126a8a12Smrg      dst=$dstdir/`basename "$src"`
283126a8a12Smrg      dstdir_status=0
284126a8a12Smrg    else
285300346aeSmrg      dstdir=`dirname "$dst"`
286126a8a12Smrg      test -d "$dstdir"
287126a8a12Smrg      dstdir_status=$?
288126a8a12Smrg    fi
289126a8a12Smrg  fi
290126a8a12Smrg
291126a8a12Smrg  obsolete_mkdir_used=false
292126a8a12Smrg
293126a8a12Smrg  if test $dstdir_status != 0; then
294126a8a12Smrg    case $posix_mkdir in
295126a8a12Smrg      '')
296300346aeSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
297300346aeSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
298300346aeSmrg        umask=`umask`
299300346aeSmrg        case $stripcmd.$umask in
300300346aeSmrg          # Optimize common cases.
301300346aeSmrg          *[2367][2367]) mkdir_umask=$umask;;
302300346aeSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303300346aeSmrg
304300346aeSmrg          *[0-7])
305300346aeSmrg            mkdir_umask=`expr $umask + 22 \
306300346aeSmrg              - $umask % 100 % 40 + $umask % 20 \
307300346aeSmrg              - $umask % 10 % 4 + $umask % 2
308300346aeSmrg            `;;
309300346aeSmrg          *) mkdir_umask=$umask,go-w;;
310300346aeSmrg        esac
311300346aeSmrg
312300346aeSmrg        # With -d, create the new directory with the user-specified mode.
313300346aeSmrg        # Otherwise, rely on $mkdir_umask.
314300346aeSmrg        if test -n "$dir_arg"; then
315300346aeSmrg          mkdir_mode=-m$mode
316300346aeSmrg        else
317300346aeSmrg          mkdir_mode=
318300346aeSmrg        fi
319300346aeSmrg
320300346aeSmrg        posix_mkdir=false
321300346aeSmrg        case $umask in
322300346aeSmrg          *[123567][0-7][0-7])
323300346aeSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324300346aeSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325300346aeSmrg            ;;
326300346aeSmrg          *)
327300346aeSmrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
328300346aeSmrg            # lower collision chance
329300346aeSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
330300346aeSmrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
331300346aeSmrg
332300346aeSmrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
333300346aeSmrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
334300346aeSmrg            # that nobody tries to guess the $tmpdir name.
335300346aeSmrg            if (umask $mkdir_umask &&
336300346aeSmrg                $mkdirprog $mkdir_mode "$tmpdir" &&
337300346aeSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
338300346aeSmrg            then
339300346aeSmrg              if test -z "$dir_arg" || {
340300346aeSmrg                   # Check for POSIX incompatibilities with -m.
341300346aeSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
342300346aeSmrg                   # other-writable bit of parent directory when it shouldn't.
343300346aeSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
344300346aeSmrg                   test_tmpdir="$tmpdir/a"
345300346aeSmrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
346300346aeSmrg                   case $ls_ld_tmpdir in
347300346aeSmrg                     d????-?r-*) different_mode=700;;
348300346aeSmrg                     d????-?--*) different_mode=755;;
349300346aeSmrg                     *) false;;
350300346aeSmrg                   esac &&
351300346aeSmrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
352300346aeSmrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
353300346aeSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
354300346aeSmrg                   }
355300346aeSmrg                 }
356300346aeSmrg              then posix_mkdir=:
357300346aeSmrg              fi
358300346aeSmrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
359300346aeSmrg            else
360300346aeSmrg              # Remove any dirs left behind by ancient mkdir implementations.
361300346aeSmrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
362300346aeSmrg            fi
363300346aeSmrg            trap '' 0;;
364300346aeSmrg        esac;;
365126a8a12Smrg    esac
366126a8a12Smrg
367126a8a12Smrg    if
368126a8a12Smrg      $posix_mkdir && (
369300346aeSmrg        umask $mkdir_umask &&
370300346aeSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
371126a8a12Smrg      )
372126a8a12Smrg    then :
373126a8a12Smrg    else
374126a8a12Smrg
375126a8a12Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
376126a8a12Smrg      # or it failed possibly due to a race condition.  Create the
377126a8a12Smrg      # directory the slow way, step by step, checking for races as we go.
378126a8a12Smrg
379126a8a12Smrg      case $dstdir in
380300346aeSmrg        /*) prefix='/';;
381300346aeSmrg        [-=\(\)!]*) prefix='./';;
382300346aeSmrg        *)  prefix='';;
383126a8a12Smrg      esac
384126a8a12Smrg
385126a8a12Smrg      oIFS=$IFS
386126a8a12Smrg      IFS=/
387300346aeSmrg      set -f
388126a8a12Smrg      set fnord $dstdir
389126a8a12Smrg      shift
390300346aeSmrg      set +f
391126a8a12Smrg      IFS=$oIFS
392126a8a12Smrg
393126a8a12Smrg      prefixes=
394126a8a12Smrg
395126a8a12Smrg      for d
396126a8a12Smrg      do
397300346aeSmrg        test X"$d" = X && continue
398300346aeSmrg
399300346aeSmrg        prefix=$prefix$d
400300346aeSmrg        if test -d "$prefix"; then
401300346aeSmrg          prefixes=
402300346aeSmrg        else
403300346aeSmrg          if $posix_mkdir; then
404300346aeSmrg            (umask=$mkdir_umask &&
405300346aeSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
406300346aeSmrg            # Don't fail if two instances are running concurrently.
407300346aeSmrg            test -d "$prefix" || exit 1
408300346aeSmrg          else
409300346aeSmrg            case $prefix in
410300346aeSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
411300346aeSmrg              *) qprefix=$prefix;;
412300346aeSmrg            esac
413300346aeSmrg            prefixes="$prefixes '$qprefix'"
414300346aeSmrg          fi
415300346aeSmrg        fi
416300346aeSmrg        prefix=$prefix/
417126a8a12Smrg      done
418126a8a12Smrg
419126a8a12Smrg      if test -n "$prefixes"; then
420300346aeSmrg        # Don't fail if two instances are running concurrently.
421300346aeSmrg        (umask $mkdir_umask &&
422300346aeSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
423300346aeSmrg          test -d "$dstdir" || exit 1
424300346aeSmrg        obsolete_mkdir_used=true
425126a8a12Smrg      fi
426126a8a12Smrg    fi
427126a8a12Smrg  fi
428126a8a12Smrg
429126a8a12Smrg  if test -n "$dir_arg"; then
430126a8a12Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
431126a8a12Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
432126a8a12Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
433126a8a12Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
434126a8a12Smrg  else
435126a8a12Smrg
436126a8a12Smrg    # Make a couple of temp file names in the proper directory.
437126a8a12Smrg    dsttmp=$dstdir/_inst.$$_
438126a8a12Smrg    rmtmp=$dstdir/_rm.$$_
439126a8a12Smrg
440126a8a12Smrg    # Trap to clean up those temp files at exit.
441126a8a12Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
442126a8a12Smrg
443126a8a12Smrg    # Copy the file name to the temp name.
444126a8a12Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
445126a8a12Smrg
446126a8a12Smrg    # and set any options; do chmod last to preserve setuid bits.
447126a8a12Smrg    #
448126a8a12Smrg    # If any of these fail, we abort the whole thing.  If we want to
449126a8a12Smrg    # ignore errors from any of these, just make sure not to ignore
450126a8a12Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
451126a8a12Smrg    #
452d656433aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
453d656433aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
454d656433aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
455d656433aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
456d656433aSmrg
457d656433aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
458d656433aSmrg    if $copy_on_change &&
459300346aeSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
460300346aeSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
461300346aeSmrg       set -f &&
462d656433aSmrg       set X $old && old=:$2:$4:$5:$6 &&
463d656433aSmrg       set X $new && new=:$2:$4:$5:$6 &&
464300346aeSmrg       set +f &&
465d656433aSmrg       test "$old" = "$new" &&
466d656433aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
467d656433aSmrg    then
468d656433aSmrg      rm -f "$dsttmp"
469d656433aSmrg    else
470d656433aSmrg      # Rename the file to the real destination.
471d656433aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
472d656433aSmrg
473d656433aSmrg      # The rename failed, perhaps because mv can't rename something else
474d656433aSmrg      # to itself, or perhaps because mv is so ancient that it does not
475d656433aSmrg      # support -f.
476d656433aSmrg      {
477300346aeSmrg        # Now remove or move aside any old file at destination location.
478300346aeSmrg        # We try this two ways since rm can't unlink itself on some
479300346aeSmrg        # systems and the destination file might be busy for other
480300346aeSmrg        # reasons.  In this case, the final cleanup might fail but the new
481300346aeSmrg        # file should still install successfully.
482300346aeSmrg        {
483300346aeSmrg          test ! -f "$dst" ||
484300346aeSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
485300346aeSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
486300346aeSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
487300346aeSmrg          } ||
488300346aeSmrg          { echo "$0: cannot unlink or rename $dst" >&2
489300346aeSmrg            (exit 1); exit 1
490300346aeSmrg          }
491300346aeSmrg        } &&
492300346aeSmrg
493300346aeSmrg        # Now rename the file to the real destination.
494300346aeSmrg        $doit $mvcmd "$dsttmp" "$dst"
495d656433aSmrg      }
496d656433aSmrg    fi || exit 1
497126a8a12Smrg
498126a8a12Smrg    trap '' 0
499126a8a12Smrg  fi
500126a8a12Smrgdone
501126a8a12Smrg
502126a8a12Smrg# Local variables:
503126a8a12Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504126a8a12Smrg# time-stamp-start: "scriptversion="
505126a8a12Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
506d656433aSmrg# time-stamp-time-zone: "UTC"
507d656433aSmrg# time-stamp-end: "; # UTC"
508126a8a12Smrg# End:
509