install-sh revision 8a355152
18108eb18Smrg#!/bin/sh
28108eb18Smrg# install - install a program, script, or datafile
31a650d1eSmrg
48a355152Smrgscriptversion=2013-12-25.23; # UTC
51a650d1eSmrg
68108eb18Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
78108eb18Smrg# later released in X11R6 (xc/config/util/install.sh) with the
88108eb18Smrg# following copyright and license.
98108eb18Smrg#
108108eb18Smrg# Copyright (C) 1994 X Consortium
118108eb18Smrg#
128108eb18Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
138108eb18Smrg# of this software and associated documentation files (the "Software"), to
148108eb18Smrg# deal in the Software without restriction, including without limitation the
158108eb18Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
168108eb18Smrg# sell copies of the Software, and to permit persons to whom the Software is
178108eb18Smrg# furnished to do so, subject to the following conditions:
188108eb18Smrg#
198108eb18Smrg# The above copyright notice and this permission notice shall be included in
208108eb18Smrg# all copies or substantial portions of the Software.
218108eb18Smrg#
228108eb18Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
238108eb18Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
248108eb18Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
258108eb18Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
268108eb18Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
278108eb18Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
288108eb18Smrg#
298108eb18Smrg# Except as contained in this notice, the name of the X Consortium shall not
308108eb18Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
318108eb18Smrg# ings in this Software without prior written authorization from the X Consor-
328108eb18Smrg# tium.
338108eb18Smrg#
348108eb18Smrg#
358108eb18Smrg# FSF changes to this file are in the public domain.
368108eb18Smrg#
378108eb18Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388a355152Smrg# 'make' implicit rules from creating a file called install from it
398108eb18Smrg# when there is no Makefile.
408108eb18Smrg#
418108eb18Smrg# This script is compatible with the BSD install script, but was written
421a650d1eSmrg# from scratch.
438108eb18Smrg
448a355152Smrgtab='	'
451a650d1eSmrgnl='
461a650d1eSmrg'
478a355152SmrgIFS=" $tab$nl"
488108eb18Smrg
498a355152Smrg# Set DOITPROG to "echo" to test this script.
508108eb18Smrg
511a650d1eSmrgdoit=${DOITPROG-}
528a355152Smrgdoit_exec=${doit:-exec}
538108eb18Smrg
541a650d1eSmrg# Put in absolute file names if you don't have them in your path;
551a650d1eSmrg# or use environment vars.
561a650d1eSmrg
571a650d1eSmrgchgrpprog=${CHGRPPROG-chgrp}
581a650d1eSmrgchmodprog=${CHMODPROG-chmod}
591a650d1eSmrgchownprog=${CHOWNPROG-chown}
601a650d1eSmrgcmpprog=${CMPPROG-cmp}
611a650d1eSmrgcpprog=${CPPROG-cp}
621a650d1eSmrgmkdirprog=${MKDIRPROG-mkdir}
631a650d1eSmrgmvprog=${MVPROG-mv}
641a650d1eSmrgrmprog=${RMPROG-rm}
651a650d1eSmrgstripprog=${STRIPPROG-strip}
661a650d1eSmrg
671a650d1eSmrgposix_mkdir=
681a650d1eSmrg
691a650d1eSmrg# Desired mode of installed file.
701a650d1eSmrgmode=0755
711a650d1eSmrg
721a650d1eSmrgchgrpcmd=
731a650d1eSmrgchmodcmd=$chmodprog
741a650d1eSmrgchowncmd=
751a650d1eSmrgmvcmd=$mvprog
761a650d1eSmrgrmcmd="$rmprog -f"
771a650d1eSmrgstripcmd=
788108eb18Smrg
791a650d1eSmrgsrc=
801a650d1eSmrgdst=
811a650d1eSmrgdir_arg=
821a650d1eSmrgdst_arg=
838108eb18Smrg
841a650d1eSmrgcopy_on_change=false
858a355152Smrgis_target_a_directory=possibly
868108eb18Smrg
871a650d1eSmrgusage="\
881a650d1eSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
891a650d1eSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
901a650d1eSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
911a650d1eSmrg   or: $0 [OPTION]... -d DIRECTORIES...
928108eb18Smrg
931a650d1eSmrgIn the 1st form, copy SRCFILE to DSTFILE.
941a650d1eSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
951a650d1eSmrgIn the 4th, create DIRECTORIES.
968108eb18Smrg
971a650d1eSmrgOptions:
981a650d1eSmrg     --help     display this help and exit.
991a650d1eSmrg     --version  display version info and exit.
1008108eb18Smrg
1011a650d1eSmrg  -c            (ignored)
1021a650d1eSmrg  -C            install only if different (preserve the last data modification time)
1031a650d1eSmrg  -d            create directories instead of installing files.
1041a650d1eSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1051a650d1eSmrg  -m MODE       $chmodprog installed files to MODE.
1061a650d1eSmrg  -o USER       $chownprog installed files to USER.
1071a650d1eSmrg  -s            $stripprog installed files.
1081a650d1eSmrg  -t DIRECTORY  install into DIRECTORY.
1091a650d1eSmrg  -T            report an error if DSTFILE is a directory.
1108108eb18Smrg
1111a650d1eSmrgEnvironment variables override the default commands:
1121a650d1eSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1131a650d1eSmrg  RMPROG STRIPPROG
1141a650d1eSmrg"
1158108eb18Smrg
1161a650d1eSmrgwhile test $# -ne 0; do
1171a650d1eSmrg  case $1 in
1181a650d1eSmrg    -c) ;;
1198108eb18Smrg
1201a650d1eSmrg    -C) copy_on_change=true;;
1218108eb18Smrg
1221a650d1eSmrg    -d) dir_arg=true;;
1238108eb18Smrg
1241a650d1eSmrg    -g) chgrpcmd="$chgrpprog $2"
1258a355152Smrg        shift;;
1268108eb18Smrg
1271a650d1eSmrg    --help) echo "$usage"; exit $?;;
1288108eb18Smrg
1291a650d1eSmrg    -m) mode=$2
1308a355152Smrg        case $mode in
1318a355152Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1328a355152Smrg            echo "$0: invalid mode: $mode" >&2
1338a355152Smrg            exit 1;;
1348a355152Smrg        esac
1358a355152Smrg        shift;;
1368108eb18Smrg
1371a650d1eSmrg    -o) chowncmd="$chownprog $2"
1388a355152Smrg        shift;;
1398108eb18Smrg
1401a650d1eSmrg    -s) stripcmd=$stripprog;;
1418108eb18Smrg
1428a355152Smrg    -t)
1438a355152Smrg        is_target_a_directory=always
1448a355152Smrg        dst_arg=$2
1458a355152Smrg        # Protect names problematic for 'test' and other utilities.
1468a355152Smrg        case $dst_arg in
1478a355152Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1488a355152Smrg        esac
1498a355152Smrg        shift;;
1508108eb18Smrg
1518a355152Smrg    -T) is_target_a_directory=never;;
1528108eb18Smrg
1531a650d1eSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1548108eb18Smrg
1558a355152Smrg    --) shift
1568a355152Smrg        break;;
1578108eb18Smrg
1588a355152Smrg    -*) echo "$0: invalid option: $1" >&2
1598a355152Smrg        exit 1;;
1608108eb18Smrg
1611a650d1eSmrg    *)  break;;
1621a650d1eSmrg  esac
1631a650d1eSmrg  shift
1641a650d1eSmrgdone
1658108eb18Smrg
1668a355152Smrg# We allow the use of options -d and -T together, by making -d
1678a355152Smrg# take the precedence; this is for compatibility with GNU install.
1688a355152Smrg
1698a355152Smrgif test -n "$dir_arg"; then
1708a355152Smrg  if test -n "$dst_arg"; then
1718a355152Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1728a355152Smrg    exit 1
1738a355152Smrg  fi
1748a355152Smrgfi
1758a355152Smrg
1761a650d1eSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1771a650d1eSmrg  # When -d is used, all remaining arguments are directories to create.
1781a650d1eSmrg  # When -t is used, the destination is already specified.
1791a650d1eSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1801a650d1eSmrg  for arg
1811a650d1eSmrg  do
1821a650d1eSmrg    if test -n "$dst_arg"; then
1831a650d1eSmrg      # $@ is not empty: it contains at least $arg.
1841a650d1eSmrg      set fnord "$@" "$dst_arg"
1851a650d1eSmrg      shift # fnord
1861a650d1eSmrg    fi
1871a650d1eSmrg    shift # arg
1881a650d1eSmrg    dst_arg=$arg
1898a355152Smrg    # Protect names problematic for 'test' and other utilities.
1905977a007Smrg    case $dst_arg in
1915977a007Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1925977a007Smrg    esac
1931a650d1eSmrg  done
1941a650d1eSmrgfi
1958108eb18Smrg
1961a650d1eSmrgif test $# -eq 0; then
1971a650d1eSmrg  if test -z "$dir_arg"; then
1981a650d1eSmrg    echo "$0: no input file specified." >&2
1991a650d1eSmrg    exit 1
2001a650d1eSmrg  fi
2018a355152Smrg  # It's OK to call 'install-sh -d' without argument.
2021a650d1eSmrg  # This can happen when creating conditional directories.
2031a650d1eSmrg  exit 0
2041a650d1eSmrgfi
2058108eb18Smrg
2068a355152Smrgif test -z "$dir_arg"; then
2078a355152Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2088a355152Smrg    if test ! -d "$dst_arg"; then
2098a355152Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2108a355152Smrg      exit 1
2118a355152Smrg    fi
2128a355152Smrg  fi
2138a355152Smrgfi
2148a355152Smrg
2151a650d1eSmrgif test -z "$dir_arg"; then
2165977a007Smrg  do_exit='(exit $ret); exit $ret'
2175977a007Smrg  trap "ret=129; $do_exit" 1
2185977a007Smrg  trap "ret=130; $do_exit" 2
2195977a007Smrg  trap "ret=141; $do_exit" 13
2205977a007Smrg  trap "ret=143; $do_exit" 15
2211a650d1eSmrg
2221a650d1eSmrg  # Set umask so as not to create temps with too-generous modes.
2231a650d1eSmrg  # However, 'strip' requires both read and write access to temps.
2241a650d1eSmrg  case $mode in
2251a650d1eSmrg    # Optimize common cases.
2261a650d1eSmrg    *644) cp_umask=133;;
2271a650d1eSmrg    *755) cp_umask=22;;
2281a650d1eSmrg
2291a650d1eSmrg    *[0-7])
2301a650d1eSmrg      if test -z "$stripcmd"; then
2318a355152Smrg        u_plus_rw=
2321a650d1eSmrg      else
2338a355152Smrg        u_plus_rw='% 200'
2341a650d1eSmrg      fi
2351a650d1eSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2361a650d1eSmrg    *)
2371a650d1eSmrg      if test -z "$stripcmd"; then
2388a355152Smrg        u_plus_rw=
2391a650d1eSmrg      else
2408a355152Smrg        u_plus_rw=,u+rw
2411a650d1eSmrg      fi
2421a650d1eSmrg      cp_umask=$mode$u_plus_rw;;
2431a650d1eSmrg  esac
2441a650d1eSmrgfi
2458108eb18Smrg
2461a650d1eSmrgfor src
2471a650d1eSmrgdo
2488a355152Smrg  # Protect names problematic for 'test' and other utilities.
2491a650d1eSmrg  case $src in
2505977a007Smrg    -* | [=\(\)!]) src=./$src;;
2511a650d1eSmrg  esac
2521a650d1eSmrg
2531a650d1eSmrg  if test -n "$dir_arg"; then
2541a650d1eSmrg    dst=$src
2551a650d1eSmrg    dstdir=$dst
2561a650d1eSmrg    test -d "$dstdir"
2571a650d1eSmrg    dstdir_status=$?
2581a650d1eSmrg  else
2591a650d1eSmrg
2601a650d1eSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2611a650d1eSmrg    # might cause directories to be created, which would be especially bad
2621a650d1eSmrg    # if $src (and thus $dsttmp) contains '*'.
2631a650d1eSmrg    if test ! -f "$src" && test ! -d "$src"; then
2641a650d1eSmrg      echo "$0: $src does not exist." >&2
2651a650d1eSmrg      exit 1
2661a650d1eSmrg    fi
2671a650d1eSmrg
2681a650d1eSmrg    if test -z "$dst_arg"; then
2691a650d1eSmrg      echo "$0: no destination specified." >&2
2701a650d1eSmrg      exit 1
2711a650d1eSmrg    fi
2721a650d1eSmrg    dst=$dst_arg
2738108eb18Smrg
2741a650d1eSmrg    # If destination is a directory, append the input filename; won't work
2751a650d1eSmrg    # if double slashes aren't ignored.
2761a650d1eSmrg    if test -d "$dst"; then
2778a355152Smrg      if test "$is_target_a_directory" = never; then
2788a355152Smrg        echo "$0: $dst_arg: Is a directory" >&2
2798a355152Smrg        exit 1
2801a650d1eSmrg      fi
2811a650d1eSmrg      dstdir=$dst
2821a650d1eSmrg      dst=$dstdir/`basename "$src"`
2831a650d1eSmrg      dstdir_status=0
2841a650d1eSmrg    else
2858a355152Smrg      dstdir=`dirname "$dst"`
2861a650d1eSmrg      test -d "$dstdir"
2871a650d1eSmrg      dstdir_status=$?
2881a650d1eSmrg    fi
2891a650d1eSmrg  fi
2901a650d1eSmrg
2911a650d1eSmrg  obsolete_mkdir_used=false
2921a650d1eSmrg
2931a650d1eSmrg  if test $dstdir_status != 0; then
2941a650d1eSmrg    case $posix_mkdir in
2951a650d1eSmrg      '')
2968a355152Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2978a355152Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2988a355152Smrg        umask=`umask`
2998a355152Smrg        case $stripcmd.$umask in
3008a355152Smrg          # Optimize common cases.
3018a355152Smrg          *[2367][2367]) mkdir_umask=$umask;;
3028a355152Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3038a355152Smrg
3048a355152Smrg          *[0-7])
3058a355152Smrg            mkdir_umask=`expr $umask + 22 \
3068a355152Smrg              - $umask % 100 % 40 + $umask % 20 \
3078a355152Smrg              - $umask % 10 % 4 + $umask % 2
3088a355152Smrg            `;;
3098a355152Smrg          *) mkdir_umask=$umask,go-w;;
3108a355152Smrg        esac
3118a355152Smrg
3128a355152Smrg        # With -d, create the new directory with the user-specified mode.
3138a355152Smrg        # Otherwise, rely on $mkdir_umask.
3148a355152Smrg        if test -n "$dir_arg"; then
3158a355152Smrg          mkdir_mode=-m$mode
3168a355152Smrg        else
3178a355152Smrg          mkdir_mode=
3188a355152Smrg        fi
3198a355152Smrg
3208a355152Smrg        posix_mkdir=false
3218a355152Smrg        case $umask in
3228a355152Smrg          *[123567][0-7][0-7])
3238a355152Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3248a355152Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3258a355152Smrg            ;;
3268a355152Smrg          *)
3278a355152Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3288a355152Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3298a355152Smrg
3308a355152Smrg            if (umask $mkdir_umask &&
3318a355152Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3328a355152Smrg            then
3338a355152Smrg              if test -z "$dir_arg" || {
3348a355152Smrg                   # Check for POSIX incompatibilities with -m.
3358a355152Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3368a355152Smrg                   # other-writable bit of parent directory when it shouldn't.
3378a355152Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3388a355152Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3398a355152Smrg                   case $ls_ld_tmpdir in
3408a355152Smrg                     d????-?r-*) different_mode=700;;
3418a355152Smrg                     d????-?--*) different_mode=755;;
3428a355152Smrg                     *) false;;
3438a355152Smrg                   esac &&
3448a355152Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3458a355152Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3468a355152Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3478a355152Smrg                   }
3488a355152Smrg                 }
3498a355152Smrg              then posix_mkdir=:
3508a355152Smrg              fi
3518a355152Smrg              rmdir "$tmpdir/d" "$tmpdir"
3528a355152Smrg            else
3538a355152Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3548a355152Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3558a355152Smrg            fi
3568a355152Smrg            trap '' 0;;
3578a355152Smrg        esac;;
3581a650d1eSmrg    esac
3598108eb18Smrg
3601a650d1eSmrg    if
3611a650d1eSmrg      $posix_mkdir && (
3628a355152Smrg        umask $mkdir_umask &&
3638a355152Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3641a650d1eSmrg      )
3651a650d1eSmrg    then :
3661a650d1eSmrg    else
3671a650d1eSmrg
3681a650d1eSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3691a650d1eSmrg      # or it failed possibly due to a race condition.  Create the
3701a650d1eSmrg      # directory the slow way, step by step, checking for races as we go.
3711a650d1eSmrg
3721a650d1eSmrg      case $dstdir in
3738a355152Smrg        /*) prefix='/';;
3748a355152Smrg        [-=\(\)!]*) prefix='./';;
3758a355152Smrg        *)  prefix='';;
3761a650d1eSmrg      esac
3771a650d1eSmrg
3781a650d1eSmrg      oIFS=$IFS
3791a650d1eSmrg      IFS=/
3808a355152Smrg      set -f
3811a650d1eSmrg      set fnord $dstdir
3821a650d1eSmrg      shift
3838a355152Smrg      set +f
3841a650d1eSmrg      IFS=$oIFS
3851a650d1eSmrg
3861a650d1eSmrg      prefixes=
3871a650d1eSmrg
3881a650d1eSmrg      for d
3891a650d1eSmrg      do
3908a355152Smrg        test X"$d" = X && continue
3918a355152Smrg
3928a355152Smrg        prefix=$prefix$d
3938a355152Smrg        if test -d "$prefix"; then
3948a355152Smrg          prefixes=
3958a355152Smrg        else
3968a355152Smrg          if $posix_mkdir; then
3978a355152Smrg            (umask=$mkdir_umask &&
3988a355152Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3998a355152Smrg            # Don't fail if two instances are running concurrently.
4008a355152Smrg            test -d "$prefix" || exit 1
4018a355152Smrg          else
4028a355152Smrg            case $prefix in
4038a355152Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4048a355152Smrg              *) qprefix=$prefix;;
4058a355152Smrg            esac
4068a355152Smrg            prefixes="$prefixes '$qprefix'"
4078a355152Smrg          fi
4088a355152Smrg        fi
4098a355152Smrg        prefix=$prefix/
4101a650d1eSmrg      done
4111a650d1eSmrg
4121a650d1eSmrg      if test -n "$prefixes"; then
4138a355152Smrg        # Don't fail if two instances are running concurrently.
4148a355152Smrg        (umask $mkdir_umask &&
4158a355152Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4168a355152Smrg          test -d "$dstdir" || exit 1
4178a355152Smrg        obsolete_mkdir_used=true
4181a650d1eSmrg      fi
4191a650d1eSmrg    fi
4201a650d1eSmrg  fi
4211a650d1eSmrg
4221a650d1eSmrg  if test -n "$dir_arg"; then
4231a650d1eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4241a650d1eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4251a650d1eSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4261a650d1eSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4271a650d1eSmrg  else
4281a650d1eSmrg
4291a650d1eSmrg    # Make a couple of temp file names in the proper directory.
4301a650d1eSmrg    dsttmp=$dstdir/_inst.$$_
4311a650d1eSmrg    rmtmp=$dstdir/_rm.$$_
4321a650d1eSmrg
4331a650d1eSmrg    # Trap to clean up those temp files at exit.
4341a650d1eSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4351a650d1eSmrg
4361a650d1eSmrg    # Copy the file name to the temp name.
4371a650d1eSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4381a650d1eSmrg
4391a650d1eSmrg    # and set any options; do chmod last to preserve setuid bits.
4401a650d1eSmrg    #
4411a650d1eSmrg    # If any of these fail, we abort the whole thing.  If we want to
4421a650d1eSmrg    # ignore errors from any of these, just make sure not to ignore
4431a650d1eSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4441a650d1eSmrg    #
4451a650d1eSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4461a650d1eSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4471a650d1eSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4481a650d1eSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4491a650d1eSmrg
4501a650d1eSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4511a650d1eSmrg    if $copy_on_change &&
4528a355152Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4538a355152Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4548a355152Smrg       set -f &&
4551a650d1eSmrg       set X $old && old=:$2:$4:$5:$6 &&
4561a650d1eSmrg       set X $new && new=:$2:$4:$5:$6 &&
4578a355152Smrg       set +f &&
4581a650d1eSmrg       test "$old" = "$new" &&
4591a650d1eSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4601a650d1eSmrg    then
4611a650d1eSmrg      rm -f "$dsttmp"
4621a650d1eSmrg    else
4631a650d1eSmrg      # Rename the file to the real destination.
4641a650d1eSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4651a650d1eSmrg
4661a650d1eSmrg      # The rename failed, perhaps because mv can't rename something else
4671a650d1eSmrg      # to itself, or perhaps because mv is so ancient that it does not
4681a650d1eSmrg      # support -f.
4691a650d1eSmrg      {
4708a355152Smrg        # Now remove or move aside any old file at destination location.
4718a355152Smrg        # We try this two ways since rm can't unlink itself on some
4728a355152Smrg        # systems and the destination file might be busy for other
4738a355152Smrg        # reasons.  In this case, the final cleanup might fail but the new
4748a355152Smrg        # file should still install successfully.
4758a355152Smrg        {
4768a355152Smrg          test ! -f "$dst" ||
4778a355152Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4788a355152Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4798a355152Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4808a355152Smrg          } ||
4818a355152Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4828a355152Smrg            (exit 1); exit 1
4838a355152Smrg          }
4848a355152Smrg        } &&
4858a355152Smrg
4868a355152Smrg        # Now rename the file to the real destination.
4878a355152Smrg        $doit $mvcmd "$dsttmp" "$dst"
4881a650d1eSmrg      }
4891a650d1eSmrg    fi || exit 1
4901a650d1eSmrg
4911a650d1eSmrg    trap '' 0
4921a650d1eSmrg  fi
4931a650d1eSmrgdone
4948108eb18Smrg
4951a650d1eSmrg# Local variables:
4961a650d1eSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4971a650d1eSmrg# time-stamp-start: "scriptversion="
4981a650d1eSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4991a650d1eSmrg# time-stamp-time-zone: "UTC"
5001a650d1eSmrg# time-stamp-end: "; # UTC"
5011a650d1eSmrg# End:
502