install-sh revision 0c7e83b2
19aa228fdSmrg#!/bin/sh
29aa228fdSmrg# install - install a program, script, or datafile
39aa228fdSmrg
40c7e83b2Smrgscriptversion=2013-12-25.23; # UTC
59aa228fdSmrg
69aa228fdSmrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79aa228fdSmrg# later released in X11R6 (xc/config/util/install.sh) with the
89aa228fdSmrg# following copyright and license.
99aa228fdSmrg#
109aa228fdSmrg# Copyright (C) 1994 X Consortium
119aa228fdSmrg#
129aa228fdSmrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139aa228fdSmrg# of this software and associated documentation files (the "Software"), to
149aa228fdSmrg# deal in the Software without restriction, including without limitation the
159aa228fdSmrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169aa228fdSmrg# sell copies of the Software, and to permit persons to whom the Software is
179aa228fdSmrg# furnished to do so, subject to the following conditions:
189aa228fdSmrg#
199aa228fdSmrg# The above copyright notice and this permission notice shall be included in
209aa228fdSmrg# all copies or substantial portions of the Software.
219aa228fdSmrg#
229aa228fdSmrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239aa228fdSmrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249aa228fdSmrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259aa228fdSmrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269aa228fdSmrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279aa228fdSmrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289aa228fdSmrg#
299aa228fdSmrg# Except as contained in this notice, the name of the X Consortium shall not
309aa228fdSmrg# be used in advertising or otherwise to promote the sale, use or other deal-
319aa228fdSmrg# ings in this Software without prior written authorization from the X Consor-
329aa228fdSmrg# tium.
339aa228fdSmrg#
349aa228fdSmrg#
359aa228fdSmrg# FSF changes to this file are in the public domain.
369aa228fdSmrg#
379aa228fdSmrg# Calling this script install-sh is preferred over install.sh, to prevent
380c7e83b2Smrg# 'make' implicit rules from creating a file called install from it
399aa228fdSmrg# when there is no Makefile.
409aa228fdSmrg#
419aa228fdSmrg# This script is compatible with the BSD install script, but was written
429aa228fdSmrg# from scratch.
439aa228fdSmrg
440c7e83b2Smrgtab='	'
459aa228fdSmrgnl='
469aa228fdSmrg'
470c7e83b2SmrgIFS=" $tab$nl"
489aa228fdSmrg
490c7e83b2Smrg# Set DOITPROG to "echo" to test this script.
509aa228fdSmrg
518f65982aSmrgdoit=${DOITPROG-}
520c7e83b2Smrgdoit_exec=${doit:-exec}
539aa228fdSmrg
549aa228fdSmrg# Put in absolute file names if you don't have them in your path;
559aa228fdSmrg# or use environment vars.
569aa228fdSmrg
578f65982aSmrgchgrpprog=${CHGRPPROG-chgrp}
588f65982aSmrgchmodprog=${CHMODPROG-chmod}
598f65982aSmrgchownprog=${CHOWNPROG-chown}
608f65982aSmrgcmpprog=${CMPPROG-cmp}
618f65982aSmrgcpprog=${CPPROG-cp}
628f65982aSmrgmkdirprog=${MKDIRPROG-mkdir}
638f65982aSmrgmvprog=${MVPROG-mv}
648f65982aSmrgrmprog=${RMPROG-rm}
658f65982aSmrgstripprog=${STRIPPROG-strip}
668f65982aSmrg
679aa228fdSmrgposix_mkdir=
689aa228fdSmrg
699aa228fdSmrg# Desired mode of installed file.
709aa228fdSmrgmode=0755
719aa228fdSmrg
728f65982aSmrgchgrpcmd=
739aa228fdSmrgchmodcmd=$chmodprog
749aa228fdSmrgchowncmd=
758f65982aSmrgmvcmd=$mvprog
769aa228fdSmrgrmcmd="$rmprog -f"
778f65982aSmrgstripcmd=
788f65982aSmrg
799aa228fdSmrgsrc=
809aa228fdSmrgdst=
819aa228fdSmrgdir_arg=
828f65982aSmrgdst_arg=
838f65982aSmrg
848f65982aSmrgcopy_on_change=false
850c7e83b2Smrgis_target_a_directory=possibly
869aa228fdSmrg
878f65982aSmrgusage="\
888f65982aSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
899aa228fdSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
909aa228fdSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
919aa228fdSmrg   or: $0 [OPTION]... -d DIRECTORIES...
929aa228fdSmrg
939aa228fdSmrgIn the 1st form, copy SRCFILE to DSTFILE.
949aa228fdSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
959aa228fdSmrgIn the 4th, create DIRECTORIES.
969aa228fdSmrg
979aa228fdSmrgOptions:
988f65982aSmrg     --help     display this help and exit.
998f65982aSmrg     --version  display version info and exit.
1008f65982aSmrg
1018f65982aSmrg  -c            (ignored)
1028f65982aSmrg  -C            install only if different (preserve the last data modification time)
1038f65982aSmrg  -d            create directories instead of installing files.
1048f65982aSmrg  -g GROUP      $chgrpprog installed files to GROUP.
1058f65982aSmrg  -m MODE       $chmodprog installed files to MODE.
1068f65982aSmrg  -o USER       $chownprog installed files to USER.
1078f65982aSmrg  -s            $stripprog installed files.
1088f65982aSmrg  -t DIRECTORY  install into DIRECTORY.
1098f65982aSmrg  -T            report an error if DSTFILE is a directory.
1109aa228fdSmrg
1119aa228fdSmrgEnvironment variables override the default commands:
1128f65982aSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1138f65982aSmrg  RMPROG STRIPPROG
1149aa228fdSmrg"
1159aa228fdSmrg
1169aa228fdSmrgwhile test $# -ne 0; do
1179aa228fdSmrg  case $1 in
1188f65982aSmrg    -c) ;;
1198f65982aSmrg
1208f65982aSmrg    -C) copy_on_change=true;;
1219aa228fdSmrg
1228f65982aSmrg    -d) dir_arg=true;;
1239aa228fdSmrg
1249aa228fdSmrg    -g) chgrpcmd="$chgrpprog $2"
1250c7e83b2Smrg        shift;;
1269aa228fdSmrg
1279aa228fdSmrg    --help) echo "$usage"; exit $?;;
1289aa228fdSmrg
1299aa228fdSmrg    -m) mode=$2
1300c7e83b2Smrg        case $mode in
1310c7e83b2Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1320c7e83b2Smrg            echo "$0: invalid mode: $mode" >&2
1330c7e83b2Smrg            exit 1;;
1340c7e83b2Smrg        esac
1350c7e83b2Smrg        shift;;
1369aa228fdSmrg
1379aa228fdSmrg    -o) chowncmd="$chownprog $2"
1380c7e83b2Smrg        shift;;
1399aa228fdSmrg
1408f65982aSmrg    -s) stripcmd=$stripprog;;
1419aa228fdSmrg
1420c7e83b2Smrg    -t)
1430c7e83b2Smrg        is_target_a_directory=always
1440c7e83b2Smrg        dst_arg=$2
1450c7e83b2Smrg        # Protect names problematic for 'test' and other utilities.
1460c7e83b2Smrg        case $dst_arg in
1470c7e83b2Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1480c7e83b2Smrg        esac
1490c7e83b2Smrg        shift;;
1509aa228fdSmrg
1510c7e83b2Smrg    -T) is_target_a_directory=never;;
1529aa228fdSmrg
1539aa228fdSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1549aa228fdSmrg
1550c7e83b2Smrg    --) shift
1560c7e83b2Smrg        break;;
1579aa228fdSmrg
1580c7e83b2Smrg    -*) echo "$0: invalid option: $1" >&2
1590c7e83b2Smrg        exit 1;;
1609aa228fdSmrg
1619aa228fdSmrg    *)  break;;
1629aa228fdSmrg  esac
1638f65982aSmrg  shift
1649aa228fdSmrgdone
1659aa228fdSmrg
1660c7e83b2Smrg# We allow the use of options -d and -T together, by making -d
1670c7e83b2Smrg# take the precedence; this is for compatibility with GNU install.
1680c7e83b2Smrg
1690c7e83b2Smrgif test -n "$dir_arg"; then
1700c7e83b2Smrg  if test -n "$dst_arg"; then
1710c7e83b2Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1720c7e83b2Smrg    exit 1
1730c7e83b2Smrg  fi
1740c7e83b2Smrgfi
1750c7e83b2Smrg
1768f65982aSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1779aa228fdSmrg  # When -d is used, all remaining arguments are directories to create.
1789aa228fdSmrg  # When -t is used, the destination is already specified.
1799aa228fdSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1809aa228fdSmrg  for arg
1819aa228fdSmrg  do
1828f65982aSmrg    if test -n "$dst_arg"; then
1839aa228fdSmrg      # $@ is not empty: it contains at least $arg.
1848f65982aSmrg      set fnord "$@" "$dst_arg"
1859aa228fdSmrg      shift # fnord
1869aa228fdSmrg    fi
1879aa228fdSmrg    shift # arg
1888f65982aSmrg    dst_arg=$arg
1890c7e83b2Smrg    # Protect names problematic for 'test' and other utilities.
19080b026c6Smrg    case $dst_arg in
19180b026c6Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19280b026c6Smrg    esac
1939aa228fdSmrg  done
1949aa228fdSmrgfi
1959aa228fdSmrg
1969aa228fdSmrgif test $# -eq 0; then
1979aa228fdSmrg  if test -z "$dir_arg"; then
1989aa228fdSmrg    echo "$0: no input file specified." >&2
1999aa228fdSmrg    exit 1
2009aa228fdSmrg  fi
2010c7e83b2Smrg  # It's OK to call 'install-sh -d' without argument.
2029aa228fdSmrg  # This can happen when creating conditional directories.
2039aa228fdSmrg  exit 0
2049aa228fdSmrgfi
2059aa228fdSmrg
2060c7e83b2Smrgif test -z "$dir_arg"; then
2070c7e83b2Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2080c7e83b2Smrg    if test ! -d "$dst_arg"; then
2090c7e83b2Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2100c7e83b2Smrg      exit 1
2110c7e83b2Smrg    fi
2120c7e83b2Smrg  fi
2130c7e83b2Smrgfi
2140c7e83b2Smrg
2159aa228fdSmrgif test -z "$dir_arg"; then
21680b026c6Smrg  do_exit='(exit $ret); exit $ret'
21780b026c6Smrg  trap "ret=129; $do_exit" 1
21880b026c6Smrg  trap "ret=130; $do_exit" 2
21980b026c6Smrg  trap "ret=141; $do_exit" 13
22080b026c6Smrg  trap "ret=143; $do_exit" 15
2219aa228fdSmrg
2229aa228fdSmrg  # Set umask so as not to create temps with too-generous modes.
2239aa228fdSmrg  # However, 'strip' requires both read and write access to temps.
2249aa228fdSmrg  case $mode in
2259aa228fdSmrg    # Optimize common cases.
2269aa228fdSmrg    *644) cp_umask=133;;
2279aa228fdSmrg    *755) cp_umask=22;;
2289aa228fdSmrg
2299aa228fdSmrg    *[0-7])
2309aa228fdSmrg      if test -z "$stripcmd"; then
2310c7e83b2Smrg        u_plus_rw=
2329aa228fdSmrg      else
2330c7e83b2Smrg        u_plus_rw='% 200'
2349aa228fdSmrg      fi
2359aa228fdSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2369aa228fdSmrg    *)
2379aa228fdSmrg      if test -z "$stripcmd"; then
2380c7e83b2Smrg        u_plus_rw=
2399aa228fdSmrg      else
2400c7e83b2Smrg        u_plus_rw=,u+rw
2419aa228fdSmrg      fi
2429aa228fdSmrg      cp_umask=$mode$u_plus_rw;;
2439aa228fdSmrg  esac
2449aa228fdSmrgfi
2459aa228fdSmrg
2469aa228fdSmrgfor src
2479aa228fdSmrgdo
2480c7e83b2Smrg  # Protect names problematic for 'test' and other utilities.
2499aa228fdSmrg  case $src in
25080b026c6Smrg    -* | [=\(\)!]) src=./$src;;
2519aa228fdSmrg  esac
2529aa228fdSmrg
2539aa228fdSmrg  if test -n "$dir_arg"; then
2549aa228fdSmrg    dst=$src
2559aa228fdSmrg    dstdir=$dst
2569aa228fdSmrg    test -d "$dstdir"
2579aa228fdSmrg    dstdir_status=$?
2589aa228fdSmrg  else
2599aa228fdSmrg
2609aa228fdSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2619aa228fdSmrg    # might cause directories to be created, which would be especially bad
2629aa228fdSmrg    # if $src (and thus $dsttmp) contains '*'.
2639aa228fdSmrg    if test ! -f "$src" && test ! -d "$src"; then
2649aa228fdSmrg      echo "$0: $src does not exist." >&2
2659aa228fdSmrg      exit 1
2669aa228fdSmrg    fi
2679aa228fdSmrg
2688f65982aSmrg    if test -z "$dst_arg"; then
2699aa228fdSmrg      echo "$0: no destination specified." >&2
2709aa228fdSmrg      exit 1
2719aa228fdSmrg    fi
2728f65982aSmrg    dst=$dst_arg
2739aa228fdSmrg
2749aa228fdSmrg    # If destination is a directory, append the input filename; won't work
2759aa228fdSmrg    # if double slashes aren't ignored.
2769aa228fdSmrg    if test -d "$dst"; then
2770c7e83b2Smrg      if test "$is_target_a_directory" = never; then
2780c7e83b2Smrg        echo "$0: $dst_arg: Is a directory" >&2
2790c7e83b2Smrg        exit 1
2809aa228fdSmrg      fi
2819aa228fdSmrg      dstdir=$dst
2829aa228fdSmrg      dst=$dstdir/`basename "$src"`
2839aa228fdSmrg      dstdir_status=0
2849aa228fdSmrg    else
2850c7e83b2Smrg      dstdir=`dirname "$dst"`
2869aa228fdSmrg      test -d "$dstdir"
2879aa228fdSmrg      dstdir_status=$?
2889aa228fdSmrg    fi
2899aa228fdSmrg  fi
2909aa228fdSmrg
2919aa228fdSmrg  obsolete_mkdir_used=false
2929aa228fdSmrg
2939aa228fdSmrg  if test $dstdir_status != 0; then
2949aa228fdSmrg    case $posix_mkdir in
2959aa228fdSmrg      '')
2960c7e83b2Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2970c7e83b2Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2980c7e83b2Smrg        umask=`umask`
2990c7e83b2Smrg        case $stripcmd.$umask in
3000c7e83b2Smrg          # Optimize common cases.
3010c7e83b2Smrg          *[2367][2367]) mkdir_umask=$umask;;
3020c7e83b2Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3030c7e83b2Smrg
3040c7e83b2Smrg          *[0-7])
3050c7e83b2Smrg            mkdir_umask=`expr $umask + 22 \
3060c7e83b2Smrg              - $umask % 100 % 40 + $umask % 20 \
3070c7e83b2Smrg              - $umask % 10 % 4 + $umask % 2
3080c7e83b2Smrg            `;;
3090c7e83b2Smrg          *) mkdir_umask=$umask,go-w;;
3100c7e83b2Smrg        esac
3110c7e83b2Smrg
3120c7e83b2Smrg        # With -d, create the new directory with the user-specified mode.
3130c7e83b2Smrg        # Otherwise, rely on $mkdir_umask.
3140c7e83b2Smrg        if test -n "$dir_arg"; then
3150c7e83b2Smrg          mkdir_mode=-m$mode
3160c7e83b2Smrg        else
3170c7e83b2Smrg          mkdir_mode=
3180c7e83b2Smrg        fi
3190c7e83b2Smrg
3200c7e83b2Smrg        posix_mkdir=false
3210c7e83b2Smrg        case $umask in
3220c7e83b2Smrg          *[123567][0-7][0-7])
3230c7e83b2Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3240c7e83b2Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3250c7e83b2Smrg            ;;
3260c7e83b2Smrg          *)
3270c7e83b2Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3280c7e83b2Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3290c7e83b2Smrg
3300c7e83b2Smrg            if (umask $mkdir_umask &&
3310c7e83b2Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3320c7e83b2Smrg            then
3330c7e83b2Smrg              if test -z "$dir_arg" || {
3340c7e83b2Smrg                   # Check for POSIX incompatibilities with -m.
3350c7e83b2Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3360c7e83b2Smrg                   # other-writable bit of parent directory when it shouldn't.
3370c7e83b2Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3380c7e83b2Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3390c7e83b2Smrg                   case $ls_ld_tmpdir in
3400c7e83b2Smrg                     d????-?r-*) different_mode=700;;
3410c7e83b2Smrg                     d????-?--*) different_mode=755;;
3420c7e83b2Smrg                     *) false;;
3430c7e83b2Smrg                   esac &&
3440c7e83b2Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3450c7e83b2Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3460c7e83b2Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3470c7e83b2Smrg                   }
3480c7e83b2Smrg                 }
3490c7e83b2Smrg              then posix_mkdir=:
3500c7e83b2Smrg              fi
3510c7e83b2Smrg              rmdir "$tmpdir/d" "$tmpdir"
3520c7e83b2Smrg            else
3530c7e83b2Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3540c7e83b2Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3550c7e83b2Smrg            fi
3560c7e83b2Smrg            trap '' 0;;
3570c7e83b2Smrg        esac;;
3589aa228fdSmrg    esac
3599aa228fdSmrg
3609aa228fdSmrg    if
3619aa228fdSmrg      $posix_mkdir && (
3620c7e83b2Smrg        umask $mkdir_umask &&
3630c7e83b2Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3649aa228fdSmrg      )
3659aa228fdSmrg    then :
3669aa228fdSmrg    else
3679aa228fdSmrg
3689aa228fdSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3699aa228fdSmrg      # or it failed possibly due to a race condition.  Create the
3709aa228fdSmrg      # directory the slow way, step by step, checking for races as we go.
3719aa228fdSmrg
3729aa228fdSmrg      case $dstdir in
3730c7e83b2Smrg        /*) prefix='/';;
3740c7e83b2Smrg        [-=\(\)!]*) prefix='./';;
3750c7e83b2Smrg        *)  prefix='';;
3769aa228fdSmrg      esac
3779aa228fdSmrg
3789aa228fdSmrg      oIFS=$IFS
3799aa228fdSmrg      IFS=/
3800c7e83b2Smrg      set -f
3819aa228fdSmrg      set fnord $dstdir
3829aa228fdSmrg      shift
3830c7e83b2Smrg      set +f
3849aa228fdSmrg      IFS=$oIFS
3859aa228fdSmrg
3869aa228fdSmrg      prefixes=
3879aa228fdSmrg
3889aa228fdSmrg      for d
3899aa228fdSmrg      do
3900c7e83b2Smrg        test X"$d" = X && continue
3910c7e83b2Smrg
3920c7e83b2Smrg        prefix=$prefix$d
3930c7e83b2Smrg        if test -d "$prefix"; then
3940c7e83b2Smrg          prefixes=
3950c7e83b2Smrg        else
3960c7e83b2Smrg          if $posix_mkdir; then
3970c7e83b2Smrg            (umask=$mkdir_umask &&
3980c7e83b2Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3990c7e83b2Smrg            # Don't fail if two instances are running concurrently.
4000c7e83b2Smrg            test -d "$prefix" || exit 1
4010c7e83b2Smrg          else
4020c7e83b2Smrg            case $prefix in
4030c7e83b2Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4040c7e83b2Smrg              *) qprefix=$prefix;;
4050c7e83b2Smrg            esac
4060c7e83b2Smrg            prefixes="$prefixes '$qprefix'"
4070c7e83b2Smrg          fi
4080c7e83b2Smrg        fi
4090c7e83b2Smrg        prefix=$prefix/
4109aa228fdSmrg      done
4119aa228fdSmrg
4129aa228fdSmrg      if test -n "$prefixes"; then
4130c7e83b2Smrg        # Don't fail if two instances are running concurrently.
4140c7e83b2Smrg        (umask $mkdir_umask &&
4150c7e83b2Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4160c7e83b2Smrg          test -d "$dstdir" || exit 1
4170c7e83b2Smrg        obsolete_mkdir_used=true
4189aa228fdSmrg      fi
4199aa228fdSmrg    fi
4209aa228fdSmrg  fi
4219aa228fdSmrg
4229aa228fdSmrg  if test -n "$dir_arg"; then
4239aa228fdSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4249aa228fdSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4259aa228fdSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4269aa228fdSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4279aa228fdSmrg  else
4289aa228fdSmrg
4299aa228fdSmrg    # Make a couple of temp file names in the proper directory.
4309aa228fdSmrg    dsttmp=$dstdir/_inst.$$_
4319aa228fdSmrg    rmtmp=$dstdir/_rm.$$_
4329aa228fdSmrg
4339aa228fdSmrg    # Trap to clean up those temp files at exit.
4349aa228fdSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4359aa228fdSmrg
4369aa228fdSmrg    # Copy the file name to the temp name.
4379aa228fdSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4389aa228fdSmrg
4399aa228fdSmrg    # and set any options; do chmod last to preserve setuid bits.
4409aa228fdSmrg    #
4419aa228fdSmrg    # If any of these fail, we abort the whole thing.  If we want to
4429aa228fdSmrg    # ignore errors from any of these, just make sure not to ignore
4439aa228fdSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4449aa228fdSmrg    #
4458f65982aSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4468f65982aSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4478f65982aSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4488f65982aSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4498f65982aSmrg
4508f65982aSmrg    # If -C, don't bother to copy if it wouldn't change the file.
4518f65982aSmrg    if $copy_on_change &&
4520c7e83b2Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4530c7e83b2Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4540c7e83b2Smrg       set -f &&
4558f65982aSmrg       set X $old && old=:$2:$4:$5:$6 &&
4568f65982aSmrg       set X $new && new=:$2:$4:$5:$6 &&
4570c7e83b2Smrg       set +f &&
4588f65982aSmrg       test "$old" = "$new" &&
4598f65982aSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4608f65982aSmrg    then
4618f65982aSmrg      rm -f "$dsttmp"
4628f65982aSmrg    else
4638f65982aSmrg      # Rename the file to the real destination.
4648f65982aSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4658f65982aSmrg
4668f65982aSmrg      # The rename failed, perhaps because mv can't rename something else
4678f65982aSmrg      # to itself, or perhaps because mv is so ancient that it does not
4688f65982aSmrg      # support -f.
4698f65982aSmrg      {
4700c7e83b2Smrg        # Now remove or move aside any old file at destination location.
4710c7e83b2Smrg        # We try this two ways since rm can't unlink itself on some
4720c7e83b2Smrg        # systems and the destination file might be busy for other
4730c7e83b2Smrg        # reasons.  In this case, the final cleanup might fail but the new
4740c7e83b2Smrg        # file should still install successfully.
4750c7e83b2Smrg        {
4760c7e83b2Smrg          test ! -f "$dst" ||
4770c7e83b2Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4780c7e83b2Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4790c7e83b2Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4800c7e83b2Smrg          } ||
4810c7e83b2Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4820c7e83b2Smrg            (exit 1); exit 1
4830c7e83b2Smrg          }
4840c7e83b2Smrg        } &&
4850c7e83b2Smrg
4860c7e83b2Smrg        # Now rename the file to the real destination.
4870c7e83b2Smrg        $doit $mvcmd "$dsttmp" "$dst"
4888f65982aSmrg      }
4898f65982aSmrg    fi || exit 1
4909aa228fdSmrg
4919aa228fdSmrg    trap '' 0
4929aa228fdSmrg  fi
4939aa228fdSmrgdone
4949aa228fdSmrg
4959aa228fdSmrg# Local variables:
4969aa228fdSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4979aa228fdSmrg# time-stamp-start: "scriptversion="
4989aa228fdSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4998f65982aSmrg# time-stamp-time-zone: "UTC"
5008f65982aSmrg# time-stamp-end: "; # UTC"
5019aa228fdSmrg# End:
502