install-sh revision c44a0236
1a850946eSmrg#!/bin/sh
2a850946eSmrg# install - install a program, script, or datafile
370f7c90cSmrg
4c44a0236Smrgscriptversion=2013-12-25.23; # UTC
570f7c90cSmrg
6a850946eSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a850946eSmrg# later released in X11R6 (xc/config/util/install.sh) with the
8a850946eSmrg# following copyright and license.
9a850946eSmrg#
10a850946eSmrg# Copyright (C) 1994 X Consortium
11a850946eSmrg#
12a850946eSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a850946eSmrg# of this software and associated documentation files (the "Software"), to
14a850946eSmrg# deal in the Software without restriction, including without limitation the
15a850946eSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a850946eSmrg# sell copies of the Software, and to permit persons to whom the Software is
17a850946eSmrg# furnished to do so, subject to the following conditions:
18a850946eSmrg#
19a850946eSmrg# The above copyright notice and this permission notice shall be included in
20a850946eSmrg# all copies or substantial portions of the Software.
21a850946eSmrg#
22a850946eSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a850946eSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a850946eSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a850946eSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a850946eSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a850946eSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a850946eSmrg#
29a850946eSmrg# Except as contained in this notice, the name of the X Consortium shall not
30a850946eSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a850946eSmrg# ings in this Software without prior written authorization from the X Consor-
32a850946eSmrg# tium.
33a850946eSmrg#
34a850946eSmrg#
35a850946eSmrg# FSF changes to this file are in the public domain.
36a850946eSmrg#
37a850946eSmrg# Calling this script install-sh is preferred over install.sh, to prevent
389511053fSmrg# 'make' implicit rules from creating a file called install from it
39a850946eSmrg# when there is no Makefile.
40a850946eSmrg#
41a850946eSmrg# This script is compatible with the BSD install script, but was written
4270f7c90cSmrg# from scratch.
43a850946eSmrg
44c44a0236Smrgtab='	'
4570f7c90cSmrgnl='
4670f7c90cSmrg'
47c44a0236SmrgIFS=" $tab$nl"
48a850946eSmrg
49c44a0236Smrg# Set DOITPROG to "echo" to test this script.
50a850946eSmrg
5170f7c90cSmrgdoit=${DOITPROG-}
52c44a0236Smrgdoit_exec=${doit:-exec}
53a850946eSmrg
5470f7c90cSmrg# Put in absolute file names if you don't have them in your path;
5570f7c90cSmrg# or use environment vars.
5670f7c90cSmrg
5770f7c90cSmrgchgrpprog=${CHGRPPROG-chgrp}
5870f7c90cSmrgchmodprog=${CHMODPROG-chmod}
5970f7c90cSmrgchownprog=${CHOWNPROG-chown}
6070f7c90cSmrgcmpprog=${CMPPROG-cmp}
6170f7c90cSmrgcpprog=${CPPROG-cp}
6270f7c90cSmrgmkdirprog=${MKDIRPROG-mkdir}
6370f7c90cSmrgmvprog=${MVPROG-mv}
6470f7c90cSmrgrmprog=${RMPROG-rm}
6570f7c90cSmrgstripprog=${STRIPPROG-strip}
6670f7c90cSmrg
6770f7c90cSmrgposix_mkdir=
6870f7c90cSmrg
6970f7c90cSmrg# Desired mode of installed file.
7070f7c90cSmrgmode=0755
7170f7c90cSmrg
7270f7c90cSmrgchgrpcmd=
7370f7c90cSmrgchmodcmd=$chmodprog
7470f7c90cSmrgchowncmd=
7570f7c90cSmrgmvcmd=$mvprog
7670f7c90cSmrgrmcmd="$rmprog -f"
7770f7c90cSmrgstripcmd=
78a850946eSmrg
7970f7c90cSmrgsrc=
8070f7c90cSmrgdst=
8170f7c90cSmrgdir_arg=
8270f7c90cSmrgdst_arg=
83a850946eSmrg
8470f7c90cSmrgcopy_on_change=false
85c44a0236Smrgis_target_a_directory=possibly
86a850946eSmrg
8770f7c90cSmrgusage="\
8870f7c90cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8970f7c90cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9070f7c90cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9170f7c90cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92a850946eSmrg
9370f7c90cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
9470f7c90cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9570f7c90cSmrgIn the 4th, create DIRECTORIES.
96a850946eSmrg
9770f7c90cSmrgOptions:
9870f7c90cSmrg     --help     display this help and exit.
9970f7c90cSmrg     --version  display version info and exit.
100a850946eSmrg
10170f7c90cSmrg  -c            (ignored)
10270f7c90cSmrg  -C            install only if different (preserve the last data modification time)
10370f7c90cSmrg  -d            create directories instead of installing files.
10470f7c90cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
10570f7c90cSmrg  -m MODE       $chmodprog installed files to MODE.
10670f7c90cSmrg  -o USER       $chownprog installed files to USER.
10770f7c90cSmrg  -s            $stripprog installed files.
10870f7c90cSmrg  -t DIRECTORY  install into DIRECTORY.
10970f7c90cSmrg  -T            report an error if DSTFILE is a directory.
110a850946eSmrg
11170f7c90cSmrgEnvironment variables override the default commands:
11270f7c90cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11370f7c90cSmrg  RMPROG STRIPPROG
11470f7c90cSmrg"
115a850946eSmrg
11670f7c90cSmrgwhile test $# -ne 0; do
11770f7c90cSmrg  case $1 in
11870f7c90cSmrg    -c) ;;
119a850946eSmrg
12070f7c90cSmrg    -C) copy_on_change=true;;
121a850946eSmrg
12270f7c90cSmrg    -d) dir_arg=true;;
123a850946eSmrg
12470f7c90cSmrg    -g) chgrpcmd="$chgrpprog $2"
125c44a0236Smrg        shift;;
126a850946eSmrg
12770f7c90cSmrg    --help) echo "$usage"; exit $?;;
128a850946eSmrg
12970f7c90cSmrg    -m) mode=$2
130c44a0236Smrg        case $mode in
131c44a0236Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132c44a0236Smrg            echo "$0: invalid mode: $mode" >&2
133c44a0236Smrg            exit 1;;
134c44a0236Smrg        esac
135c44a0236Smrg        shift;;
136a850946eSmrg
13770f7c90cSmrg    -o) chowncmd="$chownprog $2"
138c44a0236Smrg        shift;;
139a850946eSmrg
14070f7c90cSmrg    -s) stripcmd=$stripprog;;
141a850946eSmrg
142c44a0236Smrg    -t)
143c44a0236Smrg        is_target_a_directory=always
144c44a0236Smrg        dst_arg=$2
145c44a0236Smrg        # Protect names problematic for 'test' and other utilities.
146c44a0236Smrg        case $dst_arg in
147c44a0236Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148c44a0236Smrg        esac
149c44a0236Smrg        shift;;
150a850946eSmrg
151c44a0236Smrg    -T) is_target_a_directory=never;;
152a850946eSmrg
15370f7c90cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154a850946eSmrg
155c44a0236Smrg    --) shift
156c44a0236Smrg        break;;
157a850946eSmrg
158c44a0236Smrg    -*) echo "$0: invalid option: $1" >&2
159c44a0236Smrg        exit 1;;
160a850946eSmrg
16170f7c90cSmrg    *)  break;;
16270f7c90cSmrg  esac
16370f7c90cSmrg  shift
16470f7c90cSmrgdone
165a850946eSmrg
166c44a0236Smrg# We allow the use of options -d and -T together, by making -d
167c44a0236Smrg# take the precedence; this is for compatibility with GNU install.
168c44a0236Smrg
169c44a0236Smrgif test -n "$dir_arg"; then
170c44a0236Smrg  if test -n "$dst_arg"; then
171c44a0236Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172c44a0236Smrg    exit 1
173c44a0236Smrg  fi
174c44a0236Smrgfi
175c44a0236Smrg
17670f7c90cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17770f7c90cSmrg  # When -d is used, all remaining arguments are directories to create.
17870f7c90cSmrg  # When -t is used, the destination is already specified.
17970f7c90cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18070f7c90cSmrg  for arg
18170f7c90cSmrg  do
18270f7c90cSmrg    if test -n "$dst_arg"; then
18370f7c90cSmrg      # $@ is not empty: it contains at least $arg.
18470f7c90cSmrg      set fnord "$@" "$dst_arg"
18570f7c90cSmrg      shift # fnord
18670f7c90cSmrg    fi
18770f7c90cSmrg    shift # arg
18870f7c90cSmrg    dst_arg=$arg
1899511053fSmrg    # Protect names problematic for 'test' and other utilities.
1909511053fSmrg    case $dst_arg in
1919511053fSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1929511053fSmrg    esac
19370f7c90cSmrg  done
19470f7c90cSmrgfi
195a850946eSmrg
19670f7c90cSmrgif test $# -eq 0; then
19770f7c90cSmrg  if test -z "$dir_arg"; then
19870f7c90cSmrg    echo "$0: no input file specified." >&2
19970f7c90cSmrg    exit 1
20070f7c90cSmrg  fi
2019511053fSmrg  # It's OK to call 'install-sh -d' without argument.
20270f7c90cSmrg  # This can happen when creating conditional directories.
20370f7c90cSmrg  exit 0
20470f7c90cSmrgfi
205a850946eSmrg
206c44a0236Smrgif test -z "$dir_arg"; then
207c44a0236Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208c44a0236Smrg    if test ! -d "$dst_arg"; then
209c44a0236Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210c44a0236Smrg      exit 1
211c44a0236Smrg    fi
212c44a0236Smrg  fi
213c44a0236Smrgfi
214c44a0236Smrg
21570f7c90cSmrgif test -z "$dir_arg"; then
2169511053fSmrg  do_exit='(exit $ret); exit $ret'
2179511053fSmrg  trap "ret=129; $do_exit" 1
2189511053fSmrg  trap "ret=130; $do_exit" 2
2199511053fSmrg  trap "ret=141; $do_exit" 13
2209511053fSmrg  trap "ret=143; $do_exit" 15
22170f7c90cSmrg
22270f7c90cSmrg  # Set umask so as not to create temps with too-generous modes.
22370f7c90cSmrg  # However, 'strip' requires both read and write access to temps.
22470f7c90cSmrg  case $mode in
22570f7c90cSmrg    # Optimize common cases.
22670f7c90cSmrg    *644) cp_umask=133;;
22770f7c90cSmrg    *755) cp_umask=22;;
22870f7c90cSmrg
22970f7c90cSmrg    *[0-7])
23070f7c90cSmrg      if test -z "$stripcmd"; then
231c44a0236Smrg        u_plus_rw=
23270f7c90cSmrg      else
233c44a0236Smrg        u_plus_rw='% 200'
23470f7c90cSmrg      fi
23570f7c90cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23670f7c90cSmrg    *)
23770f7c90cSmrg      if test -z "$stripcmd"; then
238c44a0236Smrg        u_plus_rw=
23970f7c90cSmrg      else
240c44a0236Smrg        u_plus_rw=,u+rw
24170f7c90cSmrg      fi
24270f7c90cSmrg      cp_umask=$mode$u_plus_rw;;
24370f7c90cSmrg  esac
24470f7c90cSmrgfi
245a850946eSmrg
24670f7c90cSmrgfor src
24770f7c90cSmrgdo
2489511053fSmrg  # Protect names problematic for 'test' and other utilities.
24970f7c90cSmrg  case $src in
2509511053fSmrg    -* | [=\(\)!]) src=./$src;;
25170f7c90cSmrg  esac
25270f7c90cSmrg
25370f7c90cSmrg  if test -n "$dir_arg"; then
25470f7c90cSmrg    dst=$src
25570f7c90cSmrg    dstdir=$dst
25670f7c90cSmrg    test -d "$dstdir"
25770f7c90cSmrg    dstdir_status=$?
25870f7c90cSmrg  else
25970f7c90cSmrg
26070f7c90cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26170f7c90cSmrg    # might cause directories to be created, which would be especially bad
26270f7c90cSmrg    # if $src (and thus $dsttmp) contains '*'.
26370f7c90cSmrg    if test ! -f "$src" && test ! -d "$src"; then
26470f7c90cSmrg      echo "$0: $src does not exist." >&2
26570f7c90cSmrg      exit 1
26670f7c90cSmrg    fi
26770f7c90cSmrg
26870f7c90cSmrg    if test -z "$dst_arg"; then
26970f7c90cSmrg      echo "$0: no destination specified." >&2
27070f7c90cSmrg      exit 1
27170f7c90cSmrg    fi
27270f7c90cSmrg    dst=$dst_arg
273a850946eSmrg
27470f7c90cSmrg    # If destination is a directory, append the input filename; won't work
27570f7c90cSmrg    # if double slashes aren't ignored.
27670f7c90cSmrg    if test -d "$dst"; then
277c44a0236Smrg      if test "$is_target_a_directory" = never; then
278c44a0236Smrg        echo "$0: $dst_arg: Is a directory" >&2
279c44a0236Smrg        exit 1
28070f7c90cSmrg      fi
28170f7c90cSmrg      dstdir=$dst
28270f7c90cSmrg      dst=$dstdir/`basename "$src"`
28370f7c90cSmrg      dstdir_status=0
28470f7c90cSmrg    else
285c44a0236Smrg      dstdir=`dirname "$dst"`
28670f7c90cSmrg      test -d "$dstdir"
28770f7c90cSmrg      dstdir_status=$?
28870f7c90cSmrg    fi
28970f7c90cSmrg  fi
29070f7c90cSmrg
29170f7c90cSmrg  obsolete_mkdir_used=false
29270f7c90cSmrg
29370f7c90cSmrg  if test $dstdir_status != 0; then
29470f7c90cSmrg    case $posix_mkdir in
29570f7c90cSmrg      '')
296c44a0236Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297c44a0236Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298c44a0236Smrg        umask=`umask`
299c44a0236Smrg        case $stripcmd.$umask in
300c44a0236Smrg          # Optimize common cases.
301c44a0236Smrg          *[2367][2367]) mkdir_umask=$umask;;
302c44a0236Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303c44a0236Smrg
304c44a0236Smrg          *[0-7])
305c44a0236Smrg            mkdir_umask=`expr $umask + 22 \
306c44a0236Smrg              - $umask % 100 % 40 + $umask % 20 \
307c44a0236Smrg              - $umask % 10 % 4 + $umask % 2
308c44a0236Smrg            `;;
309c44a0236Smrg          *) mkdir_umask=$umask,go-w;;
310c44a0236Smrg        esac
311c44a0236Smrg
312c44a0236Smrg        # With -d, create the new directory with the user-specified mode.
313c44a0236Smrg        # Otherwise, rely on $mkdir_umask.
314c44a0236Smrg        if test -n "$dir_arg"; then
315c44a0236Smrg          mkdir_mode=-m$mode
316c44a0236Smrg        else
317c44a0236Smrg          mkdir_mode=
318c44a0236Smrg        fi
319c44a0236Smrg
320c44a0236Smrg        posix_mkdir=false
321c44a0236Smrg        case $umask in
322c44a0236Smrg          *[123567][0-7][0-7])
323c44a0236Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324c44a0236Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325c44a0236Smrg            ;;
326c44a0236Smrg          *)
327c44a0236Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328c44a0236Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329c44a0236Smrg
330c44a0236Smrg            if (umask $mkdir_umask &&
331c44a0236Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332c44a0236Smrg            then
333c44a0236Smrg              if test -z "$dir_arg" || {
334c44a0236Smrg                   # Check for POSIX incompatibilities with -m.
335c44a0236Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336c44a0236Smrg                   # other-writable bit of parent directory when it shouldn't.
337c44a0236Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338c44a0236Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339c44a0236Smrg                   case $ls_ld_tmpdir in
340c44a0236Smrg                     d????-?r-*) different_mode=700;;
341c44a0236Smrg                     d????-?--*) different_mode=755;;
342c44a0236Smrg                     *) false;;
343c44a0236Smrg                   esac &&
344c44a0236Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345c44a0236Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346c44a0236Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347c44a0236Smrg                   }
348c44a0236Smrg                 }
349c44a0236Smrg              then posix_mkdir=:
350c44a0236Smrg              fi
351c44a0236Smrg              rmdir "$tmpdir/d" "$tmpdir"
352c44a0236Smrg            else
353c44a0236Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354c44a0236Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355c44a0236Smrg            fi
356c44a0236Smrg            trap '' 0;;
357c44a0236Smrg        esac;;
35870f7c90cSmrg    esac
359a850946eSmrg
36070f7c90cSmrg    if
36170f7c90cSmrg      $posix_mkdir && (
362c44a0236Smrg        umask $mkdir_umask &&
363c44a0236Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
36470f7c90cSmrg      )
36570f7c90cSmrg    then :
36670f7c90cSmrg    else
36770f7c90cSmrg
36870f7c90cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
36970f7c90cSmrg      # or it failed possibly due to a race condition.  Create the
37070f7c90cSmrg      # directory the slow way, step by step, checking for races as we go.
37170f7c90cSmrg
37270f7c90cSmrg      case $dstdir in
373c44a0236Smrg        /*) prefix='/';;
374c44a0236Smrg        [-=\(\)!]*) prefix='./';;
375c44a0236Smrg        *)  prefix='';;
37670f7c90cSmrg      esac
37770f7c90cSmrg
37870f7c90cSmrg      oIFS=$IFS
37970f7c90cSmrg      IFS=/
380c44a0236Smrg      set -f
38170f7c90cSmrg      set fnord $dstdir
38270f7c90cSmrg      shift
383c44a0236Smrg      set +f
38470f7c90cSmrg      IFS=$oIFS
38570f7c90cSmrg
38670f7c90cSmrg      prefixes=
38770f7c90cSmrg
38870f7c90cSmrg      for d
38970f7c90cSmrg      do
390c44a0236Smrg        test X"$d" = X && continue
391c44a0236Smrg
392c44a0236Smrg        prefix=$prefix$d
393c44a0236Smrg        if test -d "$prefix"; then
394c44a0236Smrg          prefixes=
395c44a0236Smrg        else
396c44a0236Smrg          if $posix_mkdir; then
397c44a0236Smrg            (umask=$mkdir_umask &&
398c44a0236Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399c44a0236Smrg            # Don't fail if two instances are running concurrently.
400c44a0236Smrg            test -d "$prefix" || exit 1
401c44a0236Smrg          else
402c44a0236Smrg            case $prefix in
403c44a0236Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404c44a0236Smrg              *) qprefix=$prefix;;
405c44a0236Smrg            esac
406c44a0236Smrg            prefixes="$prefixes '$qprefix'"
407c44a0236Smrg          fi
408c44a0236Smrg        fi
409c44a0236Smrg        prefix=$prefix/
41070f7c90cSmrg      done
41170f7c90cSmrg
41270f7c90cSmrg      if test -n "$prefixes"; then
413c44a0236Smrg        # Don't fail if two instances are running concurrently.
414c44a0236Smrg        (umask $mkdir_umask &&
415c44a0236Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416c44a0236Smrg          test -d "$dstdir" || exit 1
417c44a0236Smrg        obsolete_mkdir_used=true
41870f7c90cSmrg      fi
41970f7c90cSmrg    fi
42070f7c90cSmrg  fi
42170f7c90cSmrg
42270f7c90cSmrg  if test -n "$dir_arg"; then
42370f7c90cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
42470f7c90cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
42570f7c90cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
42670f7c90cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42770f7c90cSmrg  else
42870f7c90cSmrg
42970f7c90cSmrg    # Make a couple of temp file names in the proper directory.
43070f7c90cSmrg    dsttmp=$dstdir/_inst.$$_
43170f7c90cSmrg    rmtmp=$dstdir/_rm.$$_
43270f7c90cSmrg
43370f7c90cSmrg    # Trap to clean up those temp files at exit.
43470f7c90cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43570f7c90cSmrg
43670f7c90cSmrg    # Copy the file name to the temp name.
43770f7c90cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43870f7c90cSmrg
43970f7c90cSmrg    # and set any options; do chmod last to preserve setuid bits.
44070f7c90cSmrg    #
44170f7c90cSmrg    # If any of these fail, we abort the whole thing.  If we want to
44270f7c90cSmrg    # ignore errors from any of these, just make sure not to ignore
44370f7c90cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44470f7c90cSmrg    #
44570f7c90cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
44670f7c90cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
44770f7c90cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
44870f7c90cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
44970f7c90cSmrg
45070f7c90cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
45170f7c90cSmrg    if $copy_on_change &&
452c44a0236Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453c44a0236Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454c44a0236Smrg       set -f &&
45570f7c90cSmrg       set X $old && old=:$2:$4:$5:$6 &&
45670f7c90cSmrg       set X $new && new=:$2:$4:$5:$6 &&
457c44a0236Smrg       set +f &&
45870f7c90cSmrg       test "$old" = "$new" &&
45970f7c90cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46070f7c90cSmrg    then
46170f7c90cSmrg      rm -f "$dsttmp"
46270f7c90cSmrg    else
46370f7c90cSmrg      # Rename the file to the real destination.
46470f7c90cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
46570f7c90cSmrg
46670f7c90cSmrg      # The rename failed, perhaps because mv can't rename something else
46770f7c90cSmrg      # to itself, or perhaps because mv is so ancient that it does not
46870f7c90cSmrg      # support -f.
46970f7c90cSmrg      {
470c44a0236Smrg        # Now remove or move aside any old file at destination location.
471c44a0236Smrg        # We try this two ways since rm can't unlink itself on some
472c44a0236Smrg        # systems and the destination file might be busy for other
473c44a0236Smrg        # reasons.  In this case, the final cleanup might fail but the new
474c44a0236Smrg        # file should still install successfully.
475c44a0236Smrg        {
476c44a0236Smrg          test ! -f "$dst" ||
477c44a0236Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478c44a0236Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479c44a0236Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480c44a0236Smrg          } ||
481c44a0236Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482c44a0236Smrg            (exit 1); exit 1
483c44a0236Smrg          }
484c44a0236Smrg        } &&
485c44a0236Smrg
486c44a0236Smrg        # Now rename the file to the real destination.
487c44a0236Smrg        $doit $mvcmd "$dsttmp" "$dst"
48870f7c90cSmrg      }
48970f7c90cSmrg    fi || exit 1
49070f7c90cSmrg
49170f7c90cSmrg    trap '' 0
49270f7c90cSmrg  fi
49370f7c90cSmrgdone
494a850946eSmrg
49570f7c90cSmrg# Local variables:
49670f7c90cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49770f7c90cSmrg# time-stamp-start: "scriptversion="
49870f7c90cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
49970f7c90cSmrg# time-stamp-time-zone: "UTC"
50070f7c90cSmrg# time-stamp-end: "; # UTC"
50170f7c90cSmrg# End:
502