install-sh revision 18b08247
1a99f82b8Smrg#!/bin/sh
2a99f82b8Smrg# install - install a program, script, or datafile
32d62a77cSmrg
418b08247Smrgscriptversion=2013-12-25.23; # UTC
52d62a77cSmrg
6a99f82b8Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7a99f82b8Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8a99f82b8Smrg# following copyright and license.
9a99f82b8Smrg#
10a99f82b8Smrg# Copyright (C) 1994 X Consortium
11a99f82b8Smrg#
12a99f82b8Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13a99f82b8Smrg# of this software and associated documentation files (the "Software"), to
14a99f82b8Smrg# deal in the Software without restriction, including without limitation the
15a99f82b8Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16a99f82b8Smrg# sell copies of the Software, and to permit persons to whom the Software is
17a99f82b8Smrg# furnished to do so, subject to the following conditions:
18a99f82b8Smrg#
19a99f82b8Smrg# The above copyright notice and this permission notice shall be included in
20a99f82b8Smrg# all copies or substantial portions of the Software.
21a99f82b8Smrg#
22a99f82b8Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23a99f82b8Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24a99f82b8Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25a99f82b8Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26a99f82b8Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27a99f82b8Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28a99f82b8Smrg#
29a99f82b8Smrg# Except as contained in this notice, the name of the X Consortium shall not
30a99f82b8Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31a99f82b8Smrg# ings in this Software without prior written authorization from the X Consor-
32a99f82b8Smrg# tium.
33a99f82b8Smrg#
34a99f82b8Smrg#
35a99f82b8Smrg# FSF changes to this file are in the public domain.
36a99f82b8Smrg#
37a99f82b8Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3818b08247Smrg# 'make' implicit rules from creating a file called install from it
39a99f82b8Smrg# when there is no Makefile.
40a99f82b8Smrg#
41a99f82b8Smrg# This script is compatible with the BSD install script, but was written
422d62a77cSmrg# from scratch.
43a99f82b8Smrg
4418b08247Smrgtab='	'
452d62a77cSmrgnl='
462d62a77cSmrg'
4718b08247SmrgIFS=" $tab$nl"
48a99f82b8Smrg
4918b08247Smrg# Set DOITPROG to "echo" to test this script.
50a99f82b8Smrg
512d62a77cSmrgdoit=${DOITPROG-}
5218b08247Smrgdoit_exec=${doit:-exec}
53a99f82b8Smrg
542d62a77cSmrg# Put in absolute file names if you don't have them in your path;
552d62a77cSmrg# or use environment vars.
562d62a77cSmrg
572d62a77cSmrgchgrpprog=${CHGRPPROG-chgrp}
582d62a77cSmrgchmodprog=${CHMODPROG-chmod}
592d62a77cSmrgchownprog=${CHOWNPROG-chown}
602d62a77cSmrgcmpprog=${CMPPROG-cmp}
612d62a77cSmrgcpprog=${CPPROG-cp}
622d62a77cSmrgmkdirprog=${MKDIRPROG-mkdir}
632d62a77cSmrgmvprog=${MVPROG-mv}
642d62a77cSmrgrmprog=${RMPROG-rm}
652d62a77cSmrgstripprog=${STRIPPROG-strip}
662d62a77cSmrg
672d62a77cSmrgposix_mkdir=
682d62a77cSmrg
692d62a77cSmrg# Desired mode of installed file.
702d62a77cSmrgmode=0755
712d62a77cSmrg
722d62a77cSmrgchgrpcmd=
732d62a77cSmrgchmodcmd=$chmodprog
742d62a77cSmrgchowncmd=
752d62a77cSmrgmvcmd=$mvprog
762d62a77cSmrgrmcmd="$rmprog -f"
772d62a77cSmrgstripcmd=
78a99f82b8Smrg
792d62a77cSmrgsrc=
802d62a77cSmrgdst=
812d62a77cSmrgdir_arg=
822d62a77cSmrgdst_arg=
83a99f82b8Smrg
842d62a77cSmrgcopy_on_change=false
8518b08247Smrgis_target_a_directory=possibly
86a99f82b8Smrg
872d62a77cSmrgusage="\
882d62a77cSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
892d62a77cSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
902d62a77cSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
912d62a77cSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92a99f82b8Smrg
932d62a77cSmrgIn the 1st form, copy SRCFILE to DSTFILE.
942d62a77cSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
952d62a77cSmrgIn the 4th, create DIRECTORIES.
96a99f82b8Smrg
972d62a77cSmrgOptions:
982d62a77cSmrg     --help     display this help and exit.
992d62a77cSmrg     --version  display version info and exit.
100a99f82b8Smrg
1012d62a77cSmrg  -c            (ignored)
1022d62a77cSmrg  -C            install only if different (preserve the last data modification time)
1032d62a77cSmrg  -d            create directories instead of installing files.
1042d62a77cSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1052d62a77cSmrg  -m MODE       $chmodprog installed files to MODE.
1062d62a77cSmrg  -o USER       $chownprog installed files to USER.
1072d62a77cSmrg  -s            $stripprog installed files.
1082d62a77cSmrg  -t DIRECTORY  install into DIRECTORY.
1092d62a77cSmrg  -T            report an error if DSTFILE is a directory.
110a99f82b8Smrg
1112d62a77cSmrgEnvironment variables override the default commands:
1122d62a77cSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1132d62a77cSmrg  RMPROG STRIPPROG
1142d62a77cSmrg"
115a99f82b8Smrg
1162d62a77cSmrgwhile test $# -ne 0; do
1172d62a77cSmrg  case $1 in
1182d62a77cSmrg    -c) ;;
119a99f82b8Smrg
1202d62a77cSmrg    -C) copy_on_change=true;;
121a99f82b8Smrg
1222d62a77cSmrg    -d) dir_arg=true;;
123a99f82b8Smrg
1242d62a77cSmrg    -g) chgrpcmd="$chgrpprog $2"
12518b08247Smrg        shift;;
126a99f82b8Smrg
1272d62a77cSmrg    --help) echo "$usage"; exit $?;;
128a99f82b8Smrg
1292d62a77cSmrg    -m) mode=$2
13018b08247Smrg        case $mode in
13118b08247Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13218b08247Smrg            echo "$0: invalid mode: $mode" >&2
13318b08247Smrg            exit 1;;
13418b08247Smrg        esac
13518b08247Smrg        shift;;
136a99f82b8Smrg
1372d62a77cSmrg    -o) chowncmd="$chownprog $2"
13818b08247Smrg        shift;;
139a99f82b8Smrg
1402d62a77cSmrg    -s) stripcmd=$stripprog;;
141a99f82b8Smrg
14218b08247Smrg    -t)
14318b08247Smrg        is_target_a_directory=always
14418b08247Smrg        dst_arg=$2
14518b08247Smrg        # Protect names problematic for 'test' and other utilities.
14618b08247Smrg        case $dst_arg in
14718b08247Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14818b08247Smrg        esac
14918b08247Smrg        shift;;
150a99f82b8Smrg
15118b08247Smrg    -T) is_target_a_directory=never;;
152a99f82b8Smrg
1532d62a77cSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154a99f82b8Smrg
15518b08247Smrg    --) shift
15618b08247Smrg        break;;
157a99f82b8Smrg
15818b08247Smrg    -*) echo "$0: invalid option: $1" >&2
15918b08247Smrg        exit 1;;
160a99f82b8Smrg
1612d62a77cSmrg    *)  break;;
1622d62a77cSmrg  esac
1632d62a77cSmrg  shift
1642d62a77cSmrgdone
165a99f82b8Smrg
16618b08247Smrg# We allow the use of options -d and -T together, by making -d
16718b08247Smrg# take the precedence; this is for compatibility with GNU install.
16818b08247Smrg
16918b08247Smrgif test -n "$dir_arg"; then
17018b08247Smrg  if test -n "$dst_arg"; then
17118b08247Smrg    echo "$0: target directory not allowed when installing a directory." >&2
17218b08247Smrg    exit 1
17318b08247Smrg  fi
17418b08247Smrgfi
17518b08247Smrg
1762d62a77cSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1772d62a77cSmrg  # When -d is used, all remaining arguments are directories to create.
1782d62a77cSmrg  # When -t is used, the destination is already specified.
1792d62a77cSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1802d62a77cSmrg  for arg
1812d62a77cSmrg  do
1822d62a77cSmrg    if test -n "$dst_arg"; then
1832d62a77cSmrg      # $@ is not empty: it contains at least $arg.
1842d62a77cSmrg      set fnord "$@" "$dst_arg"
1852d62a77cSmrg      shift # fnord
1862d62a77cSmrg    fi
1872d62a77cSmrg    shift # arg
1882d62a77cSmrg    dst_arg=$arg
18918b08247Smrg    # Protect names problematic for 'test' and other utilities.
19018b08247Smrg    case $dst_arg in
19118b08247Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19218b08247Smrg    esac
1932d62a77cSmrg  done
1942d62a77cSmrgfi
195a99f82b8Smrg
1962d62a77cSmrgif test $# -eq 0; then
1972d62a77cSmrg  if test -z "$dir_arg"; then
1982d62a77cSmrg    echo "$0: no input file specified." >&2
1992d62a77cSmrg    exit 1
2002d62a77cSmrg  fi
20118b08247Smrg  # It's OK to call 'install-sh -d' without argument.
2022d62a77cSmrg  # This can happen when creating conditional directories.
2032d62a77cSmrg  exit 0
2042d62a77cSmrgfi
205a99f82b8Smrg
2062d62a77cSmrgif test -z "$dir_arg"; then
20718b08247Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20818b08247Smrg    if test ! -d "$dst_arg"; then
20918b08247Smrg      echo "$0: $dst_arg: Is not a directory." >&2
21018b08247Smrg      exit 1
21118b08247Smrg    fi
21218b08247Smrg  fi
21318b08247Smrgfi
21418b08247Smrg
21518b08247Smrgif test -z "$dir_arg"; then
21618b08247Smrg  do_exit='(exit $ret); exit $ret'
21718b08247Smrg  trap "ret=129; $do_exit" 1
21818b08247Smrg  trap "ret=130; $do_exit" 2
21918b08247Smrg  trap "ret=141; $do_exit" 13
22018b08247Smrg  trap "ret=143; $do_exit" 15
2212d62a77cSmrg
2222d62a77cSmrg  # Set umask so as not to create temps with too-generous modes.
2232d62a77cSmrg  # However, 'strip' requires both read and write access to temps.
2242d62a77cSmrg  case $mode in
2252d62a77cSmrg    # Optimize common cases.
2262d62a77cSmrg    *644) cp_umask=133;;
2272d62a77cSmrg    *755) cp_umask=22;;
2282d62a77cSmrg
2292d62a77cSmrg    *[0-7])
2302d62a77cSmrg      if test -z "$stripcmd"; then
23118b08247Smrg        u_plus_rw=
2322d62a77cSmrg      else
23318b08247Smrg        u_plus_rw='% 200'
2342d62a77cSmrg      fi
2352d62a77cSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2362d62a77cSmrg    *)
2372d62a77cSmrg      if test -z "$stripcmd"; then
23818b08247Smrg        u_plus_rw=
2392d62a77cSmrg      else
24018b08247Smrg        u_plus_rw=,u+rw
2412d62a77cSmrg      fi
2422d62a77cSmrg      cp_umask=$mode$u_plus_rw;;
2432d62a77cSmrg  esac
2442d62a77cSmrgfi
245a99f82b8Smrg
2462d62a77cSmrgfor src
2472d62a77cSmrgdo
24818b08247Smrg  # Protect names problematic for 'test' and other utilities.
2492d62a77cSmrg  case $src in
25018b08247Smrg    -* | [=\(\)!]) src=./$src;;
2512d62a77cSmrg  esac
2522d62a77cSmrg
2532d62a77cSmrg  if test -n "$dir_arg"; then
2542d62a77cSmrg    dst=$src
2552d62a77cSmrg    dstdir=$dst
2562d62a77cSmrg    test -d "$dstdir"
2572d62a77cSmrg    dstdir_status=$?
2582d62a77cSmrg  else
2592d62a77cSmrg
2602d62a77cSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2612d62a77cSmrg    # might cause directories to be created, which would be especially bad
2622d62a77cSmrg    # if $src (and thus $dsttmp) contains '*'.
2632d62a77cSmrg    if test ! -f "$src" && test ! -d "$src"; then
2642d62a77cSmrg      echo "$0: $src does not exist." >&2
2652d62a77cSmrg      exit 1
2662d62a77cSmrg    fi
2672d62a77cSmrg
2682d62a77cSmrg    if test -z "$dst_arg"; then
2692d62a77cSmrg      echo "$0: no destination specified." >&2
2702d62a77cSmrg      exit 1
2712d62a77cSmrg    fi
2722d62a77cSmrg    dst=$dst_arg
273a99f82b8Smrg
2742d62a77cSmrg    # If destination is a directory, append the input filename; won't work
2752d62a77cSmrg    # if double slashes aren't ignored.
2762d62a77cSmrg    if test -d "$dst"; then
27718b08247Smrg      if test "$is_target_a_directory" = never; then
27818b08247Smrg        echo "$0: $dst_arg: Is a directory" >&2
27918b08247Smrg        exit 1
2802d62a77cSmrg      fi
2812d62a77cSmrg      dstdir=$dst
2822d62a77cSmrg      dst=$dstdir/`basename "$src"`
2832d62a77cSmrg      dstdir_status=0
2842d62a77cSmrg    else
28518b08247Smrg      dstdir=`dirname "$dst"`
2862d62a77cSmrg      test -d "$dstdir"
2872d62a77cSmrg      dstdir_status=$?
2882d62a77cSmrg    fi
2892d62a77cSmrg  fi
2902d62a77cSmrg
2912d62a77cSmrg  obsolete_mkdir_used=false
2922d62a77cSmrg
2932d62a77cSmrg  if test $dstdir_status != 0; then
2942d62a77cSmrg    case $posix_mkdir in
2952d62a77cSmrg      '')
29618b08247Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
29718b08247Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
29818b08247Smrg        umask=`umask`
29918b08247Smrg        case $stripcmd.$umask in
30018b08247Smrg          # Optimize common cases.
30118b08247Smrg          *[2367][2367]) mkdir_umask=$umask;;
30218b08247Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30318b08247Smrg
30418b08247Smrg          *[0-7])
30518b08247Smrg            mkdir_umask=`expr $umask + 22 \
30618b08247Smrg              - $umask % 100 % 40 + $umask % 20 \
30718b08247Smrg              - $umask % 10 % 4 + $umask % 2
30818b08247Smrg            `;;
30918b08247Smrg          *) mkdir_umask=$umask,go-w;;
31018b08247Smrg        esac
31118b08247Smrg
31218b08247Smrg        # With -d, create the new directory with the user-specified mode.
31318b08247Smrg        # Otherwise, rely on $mkdir_umask.
31418b08247Smrg        if test -n "$dir_arg"; then
31518b08247Smrg          mkdir_mode=-m$mode
31618b08247Smrg        else
31718b08247Smrg          mkdir_mode=
31818b08247Smrg        fi
31918b08247Smrg
32018b08247Smrg        posix_mkdir=false
32118b08247Smrg        case $umask in
32218b08247Smrg          *[123567][0-7][0-7])
32318b08247Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32418b08247Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32518b08247Smrg            ;;
32618b08247Smrg          *)
32718b08247Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32818b08247Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32918b08247Smrg
33018b08247Smrg            if (umask $mkdir_umask &&
33118b08247Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33218b08247Smrg            then
33318b08247Smrg              if test -z "$dir_arg" || {
33418b08247Smrg                   # Check for POSIX incompatibilities with -m.
33518b08247Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33618b08247Smrg                   # other-writable bit of parent directory when it shouldn't.
33718b08247Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33818b08247Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33918b08247Smrg                   case $ls_ld_tmpdir in
34018b08247Smrg                     d????-?r-*) different_mode=700;;
34118b08247Smrg                     d????-?--*) different_mode=755;;
34218b08247Smrg                     *) false;;
34318b08247Smrg                   esac &&
34418b08247Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34518b08247Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34618b08247Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34718b08247Smrg                   }
34818b08247Smrg                 }
34918b08247Smrg              then posix_mkdir=:
35018b08247Smrg              fi
35118b08247Smrg              rmdir "$tmpdir/d" "$tmpdir"
35218b08247Smrg            else
35318b08247Smrg              # Remove any dirs left behind by ancient mkdir implementations.
35418b08247Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35518b08247Smrg            fi
35618b08247Smrg            trap '' 0;;
35718b08247Smrg        esac;;
3582d62a77cSmrg    esac
359a99f82b8Smrg
3602d62a77cSmrg    if
3612d62a77cSmrg      $posix_mkdir && (
36218b08247Smrg        umask $mkdir_umask &&
36318b08247Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3642d62a77cSmrg      )
3652d62a77cSmrg    then :
3662d62a77cSmrg    else
3672d62a77cSmrg
3682d62a77cSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3692d62a77cSmrg      # or it failed possibly due to a race condition.  Create the
3702d62a77cSmrg      # directory the slow way, step by step, checking for races as we go.
3712d62a77cSmrg
3722d62a77cSmrg      case $dstdir in
37318b08247Smrg        /*) prefix='/';;
37418b08247Smrg        [-=\(\)!]*) prefix='./';;
37518b08247Smrg        *)  prefix='';;
3762d62a77cSmrg      esac
3772d62a77cSmrg
3782d62a77cSmrg      oIFS=$IFS
3792d62a77cSmrg      IFS=/
38018b08247Smrg      set -f
3812d62a77cSmrg      set fnord $dstdir
3822d62a77cSmrg      shift
38318b08247Smrg      set +f
3842d62a77cSmrg      IFS=$oIFS
3852d62a77cSmrg
3862d62a77cSmrg      prefixes=
3872d62a77cSmrg
3882d62a77cSmrg      for d
3892d62a77cSmrg      do
39018b08247Smrg        test X"$d" = X && continue
39118b08247Smrg
39218b08247Smrg        prefix=$prefix$d
39318b08247Smrg        if test -d "$prefix"; then
39418b08247Smrg          prefixes=
39518b08247Smrg        else
39618b08247Smrg          if $posix_mkdir; then
39718b08247Smrg            (umask=$mkdir_umask &&
39818b08247Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39918b08247Smrg            # Don't fail if two instances are running concurrently.
40018b08247Smrg            test -d "$prefix" || exit 1
40118b08247Smrg          else
40218b08247Smrg            case $prefix in
40318b08247Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40418b08247Smrg              *) qprefix=$prefix;;
40518b08247Smrg            esac
40618b08247Smrg            prefixes="$prefixes '$qprefix'"
40718b08247Smrg          fi
40818b08247Smrg        fi
40918b08247Smrg        prefix=$prefix/
4102d62a77cSmrg      done
4112d62a77cSmrg
4122d62a77cSmrg      if test -n "$prefixes"; then
41318b08247Smrg        # Don't fail if two instances are running concurrently.
41418b08247Smrg        (umask $mkdir_umask &&
41518b08247Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41618b08247Smrg          test -d "$dstdir" || exit 1
41718b08247Smrg        obsolete_mkdir_used=true
4182d62a77cSmrg      fi
4192d62a77cSmrg    fi
4202d62a77cSmrg  fi
4212d62a77cSmrg
4222d62a77cSmrg  if test -n "$dir_arg"; then
4232d62a77cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4242d62a77cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4252d62a77cSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4262d62a77cSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4272d62a77cSmrg  else
4282d62a77cSmrg
4292d62a77cSmrg    # Make a couple of temp file names in the proper directory.
4302d62a77cSmrg    dsttmp=$dstdir/_inst.$$_
4312d62a77cSmrg    rmtmp=$dstdir/_rm.$$_
4322d62a77cSmrg
4332d62a77cSmrg    # Trap to clean up those temp files at exit.
4342d62a77cSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4352d62a77cSmrg
4362d62a77cSmrg    # Copy the file name to the temp name.
4372d62a77cSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4382d62a77cSmrg
4392d62a77cSmrg    # and set any options; do chmod last to preserve setuid bits.
4402d62a77cSmrg    #
4412d62a77cSmrg    # If any of these fail, we abort the whole thing.  If we want to
4422d62a77cSmrg    # ignore errors from any of these, just make sure not to ignore
4432d62a77cSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4442d62a77cSmrg    #
4452d62a77cSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4462d62a77cSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4472d62a77cSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4482d62a77cSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4492d62a77cSmrg
4502d62a77cSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4512d62a77cSmrg    if $copy_on_change &&
45218b08247Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45318b08247Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45418b08247Smrg       set -f &&
4552d62a77cSmrg       set X $old && old=:$2:$4:$5:$6 &&
4562d62a77cSmrg       set X $new && new=:$2:$4:$5:$6 &&
45718b08247Smrg       set +f &&
4582d62a77cSmrg       test "$old" = "$new" &&
4592d62a77cSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4602d62a77cSmrg    then
4612d62a77cSmrg      rm -f "$dsttmp"
4622d62a77cSmrg    else
4632d62a77cSmrg      # Rename the file to the real destination.
4642d62a77cSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4652d62a77cSmrg
4662d62a77cSmrg      # The rename failed, perhaps because mv can't rename something else
4672d62a77cSmrg      # to itself, or perhaps because mv is so ancient that it does not
4682d62a77cSmrg      # support -f.
4692d62a77cSmrg      {
47018b08247Smrg        # Now remove or move aside any old file at destination location.
47118b08247Smrg        # We try this two ways since rm can't unlink itself on some
47218b08247Smrg        # systems and the destination file might be busy for other
47318b08247Smrg        # reasons.  In this case, the final cleanup might fail but the new
47418b08247Smrg        # file should still install successfully.
47518b08247Smrg        {
47618b08247Smrg          test ! -f "$dst" ||
47718b08247Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47818b08247Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47918b08247Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48018b08247Smrg          } ||
48118b08247Smrg          { echo "$0: cannot unlink or rename $dst" >&2
48218b08247Smrg            (exit 1); exit 1
48318b08247Smrg          }
48418b08247Smrg        } &&
48518b08247Smrg
48618b08247Smrg        # Now rename the file to the real destination.
48718b08247Smrg        $doit $mvcmd "$dsttmp" "$dst"
4882d62a77cSmrg      }
4892d62a77cSmrg    fi || exit 1
4902d62a77cSmrg
4912d62a77cSmrg    trap '' 0
4922d62a77cSmrg  fi
4932d62a77cSmrgdone
494a99f82b8Smrg
4952d62a77cSmrg# Local variables:
4962d62a77cSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4972d62a77cSmrg# time-stamp-start: "scriptversion="
4982d62a77cSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4992d62a77cSmrg# time-stamp-time-zone: "UTC"
5002d62a77cSmrg# time-stamp-end: "; # UTC"
5012d62a77cSmrg# End:
502